2017-04-19 1 views
-1

Die Idee ist, einen Namen am Ende der verknüpften Liste hinzufügen zu können, wenn der Benutzer "1" eingibt und den ersten Namen in der verknüpften Liste druckt und löscht, wenn der Benutzer "0" eingibt. Dies funktioniert, wenn nur 2 Namen in der verknüpften Liste vorhanden sind, aber mehr als 2 nur den Vornamen und den Nachnamen, aber ich kann meinen Fehler nicht sehen.Kann ich keinen Fehler in meinem Code finden? C

#include<stdio.h> 
#include<stdlib.h> 
#include<conio.h> 
#define bool int 
#define TRUE 1d 
#define FALSE 0 

typedef struct node{ 
    char name[100]; 
    struct node *next; 
} Node; 

Node *head; 

void call(){ 
    if(head == NULL){ 
      printf("List is empty.\n"); 
     } 
     else{ 
      Node *temp; 
      printf("Calling %s\n", head->name); 
      if(head->next!=NULL){ 
       temp = head->next; 
       free(head); 
       head = temp; 
      } 
      else{ 
       head = NULL; 
      } 
     } 
} 

void add(char input[]){ 
     Node *temp; 
     temp = (Node*)malloc(sizeof(Node)); 
     strcpy(temp->name, input); 
     temp->next = NULL; 
     if(head == NULL){ 
      head = temp; 
     } 
     else{ 
      head->next = temp; 
     } 
} 

int main(){ 

    start:; 
    int user_menu_answer; 
    char waste; 

    printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); 
    printf("0) Call a customer\n"); 
    printf("1) Add a customer\n"); 
    printf("2) Quit\n"); 
    printf("Please input your command \n"); 
    printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"); 
    scanf("%d%c", &user_menu_answer, &waste); 

    //main menu options 
    if(user_menu_answer == 0){ 
     call(); 
     goto start; 
    } 
    else if (user_menu_answer == 1){ 
     char input[23]; 
     printf("Please give me the customers name: \n"); 
     scanf("%[^\n]", input); 
     add(input); 
     goto start; 
    } 
    else if (user_menu_answer == 2){ 
     printf("Quitting..."); 
     return 0; 
    } 
    else{ 
     printf("You did not enter a valid option, Please try again. \n"); 
     goto start; 
    } 
} 
+0

"* Die Idee der Lage sein, einen Namen zu Ende der verknüpften Liste hinzuzufügen ... *" - Wo ist die Variable, die das Ende der verknüpften Liste verfolgt? –

+1

Wenn Sie 'temp-> next' auf null setzen und dann nach head einfügen, haben Sie gerade den Rest der Liste verloren. –

+0

Ich denke, das Problem ist die gleiche Kopfvariable wird für Kopf und Schwanz beide verwendet. so fügt das Hinzufügen von Namen Knoten am Kopf hinzu -> nächste, die Schwanz sein sollte -> nächste –

Antwort

0

erster Fehler:

void call(){ 

if(head == NULL){ 
     printf("List is empty.\n"); 
    } 
    else{ 
     Node *temp; 
     printf("Calling %s\n", head->name); 
     if(head->next!=NULL){ 
      temp = head->next; 
      free(head); 
      head = temp; 
     } 
     else{ 
      free(head); //you don't free the memory if it's just one in the list 
      head = NULL; 
     } 
    } 
} 

Zweiter Fehler:

void add(char input[]){ 
    Node *AuxHead; //create an aux to the head so you can move threw the list 
    Node *temp; 
    temp = (Node*)malloc(sizeof(Node)); 
if(temp!=NULL) //verify if it got allocated 
{ 
    strcpy(temp->name, input); 
    temp->next = NULL; 
    if(head == NULL){ 
     head = temp; 
    } 
    else{ 
     AuxHead=head; //pass the address of the head 
     while(AuxHead->next!=NULL) //catch the lest node of the list 
      AuxHead=AuxHead->next; 
     AuxHead->next = temp; //make the temp, the last of the list 
    } 
} 
} 
+0

Gott segne dich, mein Herr! –

+0

Kannst du eine Antwort auf meine Antwort setzen? So wissen die Leute, dass diese Frage richtig beantwortet wurde. – BlazeChill

Verwandte Themen