0
#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然後嘗試打印鏈接列表。 cout語句立即調用printList in main工作正常但由於某種原因printList函數掛起在while循環中。爲什麼我的printList函數不工作?
啊拍。我是一個業餘嘿嘿。謝謝! – Brandon 2009-12-12 02:37:58