2009-12-12 78 views
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函数不工作?

回答

3
if(p = NULL) 

这就是你的问题。它应该是

if(p == NULL) 
+0

啊拍。我是一个业余嘿嘿。谢谢! – Brandon 2009-12-12 02:37:58

相关问题