2009-12-12 9 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; 
     } 
    } 
} 

Ce programme prend l'entrée de l'utilisateur un caractère à la fois et le place dans une liste liée. printList puis tente d'imprimer la liste liée. Les instructions cout immédiatement avant l'appel à printList dans principal fonctionnent très bien mais pour une raison quelconque, la fonction printList raccroche dans la boucle while.Pourquoi ma fonction printList ne fonctionne-t-elle pas?

Répondre

3
if(p = NULL) 

C'est votre problème ici. Il devrait être

if(p == NULL) 
+0

Ah tirer. Je suis un amateur hehe. Merci! – Brandon

Questions connexes