链表为什么建不成功啊,不能输出?

2025-12-16 19:29:13
推荐回答(3个)
回答1:

先给你个我以前写的参考一下,
我看看你的程序~~
#include
#include
using namespace std;

template < class ELEM >
struct ListNode {
ELEM data; //存放数据
ListNode * next; //存放下一个节点的地址
};

template < class ELEM >
class CSinglyList {
public:
CSinglyList();
~CSinglyList();
ListNode< ELEM > * findIndex( const int & ) const;
CSinglyList< ELEM > & insertAfter( const ELEM &, const int & );
CSinglyList< ELEM > & removeAfter( const int & );
bool isEmpty() const;
int getLength() const;
void print() const;
private:
ListNode< ELEM > * first;
ListNode< ELEM > * last;
};

//构造,分配一个头节点
template < class ELEM >
CSinglyList< ELEM >::CSinglyList()
{
first = new ListNode< ELEM >; //分配头节点地址
assert( first != NULL ); //测试是否分配到地址
first->next = NULL; //下一个节点地址初始化
last = first; //尾指针初始化
}

//析构,释放空间
template < class ELEM >
CSinglyList< ELEM >::~CSinglyList()
{
ListNode< ELEM > * pTemp, * curNode;

pTemp = first;

//依次删除每个节点
while ( pTemp ) {
curNode = pTemp;
pTemp = pTemp->next;
delete curNode;
}
}

//查找单链表中第i个节点
template < class ELEM >
ListNode< ELEM > * CSinglyList< ELEM >::findIndex( const int & i ) const
{
if ( i == -1 )
return first;

ListNode< ELEM > * pTemp = first->next;
int curPos = 0;

while ( pTemp ) {
if ( curPos == i ) //查到第i个节点,跳出循环
break;
pTemp = pTemp->next;
++ curPos;
}

if ( curPos != i ) //没有查到,返回尾指针
return 0;

return pTemp;
}

//在第i个节点后插入一个节点
template < class ELEM >
CSinglyList< ELEM > & CSinglyList< ELEM >::insertAfter( const ELEM & value, const int & i )
{
ListNode< ELEM > * pTemp, * iPos;

iPos = findIndex( i ); //获取第i个节点的地址
if ( iPos == NULL ) { //判断是否存在i节点
cout << "This node is not exist." << endl;
return * this;
}
pTemp = new ListNode< ELEM >; //分配新节点地址
assert( pTemp != NULL ); //测试是否分配到地址
pTemp->data = value;
pTemp->next = iPos->next;
iPos->next = pTemp;
if ( pTemp->next == NULL ) //判断边界,重新定向尾指针
last = pTemp;

return * this;
}

//删除i节点
template < class ELEM >
CSinglyList< ELEM > & CSinglyList< ELEM >::removeAfter( const int & i )
{
ListNode< ELEM > * pTemp, * iPos;

iPos = findIndex( i - 1 );
if ( iPos == NULL ) { //判断是否存在i节点
cout << "This node is not exist." << endl;
return * this;
}
pTemp = iPos->next;
if ( iPos != NULL ) //若节点非空,则重新指向
iPos->next = pTemp->next;
delete pTemp;
if ( iPos->next == NULL ) //判断边界,重新定向尾指针
last = iPos;

return * this;
}

//判断链表是否为空
template < class ELEM >
bool CSinglyList< ELEM >::isEmpty() const
{
if ( first->next )
return false;

return true;
}

//计算链表长度
template < class ELEM >
int CSinglyList< ELEM >::getLength() const
{
ListNode< ELEM > * pTemp = first;
int length = 0;

while ( pTemp->next ) {
++ length;
pTemp = pTemp->next;
}

return length;
}

//打印链表
template < class ELEM >
void CSinglyList< ELEM >::print() const
{
ListNode< ELEM > *pTemp = first;

if ( pTemp->next == NULL )
cout << "The linklist is empty." << endl;

while ( pTemp->next ) {
pTemp = pTemp->next;
cout << pTemp->data << endl;
}
}

int main()
{
CSinglyList< int > MyList;
ListNode< int > * pTemp;
bool flag = true;
int choose, number, position;

while ( flag ) {
cin >> choose;

switch ( choose ) {
case 0:
flag = false;
break;

case 1:
cout << "You want to find the node: ";
cin >> position;
pTemp = MyList.findIndex( position );
if ( pTemp )
cout << "The node " << position << " is " << pTemp->data << "." << endl;
else
cout << "Cannot find this node." << endl;
break;

case 2:
cout << "You want to insert a number: ";
cin >> number;
cout << "After the node: ";
cin >>position;
cout << "After insert a " << number << " after the node " << position << ".\nThe new linklist is:" << endl;
MyList.insertAfter( number, position ).print();
break;

case 3:
cout << "You want to remove the node: ";
cin >> position;
cout << "After remove the node " << position << ".\nThe new linklist is:" << endl;
MyList.removeAfter( position ).print();
break;

case 4:
if ( MyList.isEmpty() )
cout << "The linklist is empty." << endl;
else
cout << "The linklist is not empty." << endl;
break;

case 5:
cout << "The length of the linklist is " << MyList.getLength() << "." << endl;
break;

case 6:
MyList.print();
break;
}
}

return 0;
}

回答2:

你写的这个比较混乱,首先,建议用动态分配内存给指针变量,如struct node *p=(node*)malloc(sizeof(struct node));
if(head == NULL)
{
cur->next = head; //你做的是循环链表吗,如果不是
head = cur;//建议把 cur->next = NULL;
return; //以后哪个结点的下个NEXT指针为空则
} //则是尾结点,便于遍历

看了半天,终于明白你错在哪了,你对链的遍历是基于这个结点是不是空结点,但是你整个程序里一直都没对你的结点的NEXT指针赋空指针。。
改正的方法是建立一个新的结点的时候给NEXT指针赋NULL,同时,不要在把你的尾结点NEXT域指向头结点了

回答3:

#include

struct node
{
int value;
node *next;
};
node *head = NULL;
//全局的链表头

void InsertSort(node *cur,node *&head)
{

if(head == NULL)
{
cur->next = head;
head = cur;
return;
}

node *tmpre = head,*tmpos = head;
if(tmpos->value < cur->value)
{//与第一个结点比较,因为刚开始,前驱指针tmpre与
//当前指针tmpos都指向第一个结点,故需特殊处理

cur->next = tmpos;

head=cur;
return;
}
while(tmpos != NULL)
{

if(tmpos->value < cur->value) //降序排列
break;
tmpre = tmpos;
tmpos = tmpos->next;
}

cur->next = tmpos;
tmpre->next = cur;
}

void main(int argv,char argc[])
{

int value;

cout<<"please input value:"< cin>>value;

while(value != 0)
{
node *current = new node;
current->value = value;
InsertSort(current,head);
cout<<"next value:"< cin>>value;
}
node *play = head;
while(play != NULL)
{
cout<<"value :"<value< play = play->next;

}

}

自己对照看,传递head的时候要传递引用!!这是很重要的,还有你考虑第二种情况的时候写错了句,还有,你程序有问题,你new之后应该delete