#include <iostream>
using namespace std;
struct Node
{
char item;
Node *next;
};
void inputChar ( Node * );
void printList (Node *);
char c;
int main()
{
Node *head;
head = NULL;
c = getchar();
if ( c != '.' )
{
head = new Node;
head->item = c;
inputChar(head);
}
cout << head->item << endl;
cout << head->next->item << endl;
printList(head);
return 0;
}
void inputChar(Node *p)
{
c = getchar();
while ( c != '.' )
{
p->next = new Node;
p->next->item = c;
p = p->next;
c = getchar();
}
p->next = new Node; // dot signals end of list
p->next->item = c;
}
void printList(Node *p)
{
if(p = NULL)
cout << "empty" <<endl;
else
{
while (p->item != '.')
{
cout << p->item << endl;
p = p->next;
}
}
}
该程序一次从用户输入一个字符并将其放入链接列表中。 printList 然后尝试打印链表。紧接在 main 中调用 printList 之前的cout语句工作正常但由于某种原因 printList 函数在while循环中挂起。
答案 0 :(得分:3)
if(p = NULL)
那就是你的问题。它应该是
if(p == NULL)