2017-05-10 2 views
1

Ich versuche, eine einfache doppelt verkettete Liste zu machen, habe ich (Schalter) an erster Stelle:Doppel verkettete Liste - C

int choice, data; 
    switch(choice) 
    { 
     case 1: 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsetFirst(data); 
      data =0; 
      break; 

     case 2: 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsertLast(data); 
      data =0; 
      break; 

     case 3: 
      printf("The list from the beginning to the End :\n"); 
      PrintForward(); 
      break; 

     case 4: 
      printf("The list from the end to the beginning\n"); 
      PrintBackward(); 
      break; 

     case 5: 
      printf("Enter the data you want search\n"); 
      scanf("%d",data); 
      Search(data); 
      if(Search(data)) 
      { 
      printf("%d\n",*(Search(data))); 
      } 
      else 
      {} 
      data =0; 
      break; 

     case 6: 
      printf("Enter The data you want to delete\n"); 
      scanf("%d",&data); 
      DeleteNode(data); 
      break; 

     default : 
      printf("Not Valid Entry\n"); 

Aber es hielt mich diesen Fehler in einer der Funktionen zeigt

"expected declaration or statement at end of input"

zu wissen, dass ich die Funktionen einzeln getestet und es funktionierte einwandfrei,

danach habe ich verwendet (wenn if-else) und dann worked` es

int main() 
{ 
    int choice=1 , data; 
    while (1) 
    { 
     printf("Choose from the following options\n\n"); 
     printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n"); 
     scanf("%d",&choice); 
     if(choice==1) 
     { 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsetFirst(data); 
      data =0; 
     } 
     else if (choice==2) 
     { 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsertLast(data); 
      data =0; 
     } 
     else if(choice==3) 
     { 
      printf("The list from the beginning to the End :\n"); 
      PrintForward(); 
     } 
     else if(choice==4) 
     { 
      printf("The list from the end to the beginning\n"); 
      PrintBackward(); 
     } 
     else if(choice==5) 
     { 
      printf("Enter the data you want search\n"); 
      scanf("%d",data); 
      Search(data); 
      data =0; 
     } 
     else if(choice==6) 
     { 
      printf("Enter The data you want to delete\n"); 
      scanf("%d",&data); 
      DeleteNode(data); 
     } 
     else 
     { 
      printf("Enter a Valid Choice\n"); 
     } 
    }`, 

aber es gab Fehler mit der Suchfunktion, falls das Element nicht existiert.

Hoffnung jemand kann mir helfen, danke im voraus, Frieden :) hier ist der vollständige Code mit kommentierten Abschnitten, die nicht funktionieren:

im Code
#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 

struct Node 
{ 
    int data; 
    struct Node* pnext; 
    struct Node* pprev; 
}; 
struct Node* pstart = NULL; 
struct Node* plast = NULL; 

/** Functions Prototype **/ 

struct Node* CreatNode (void); 
void InsetFirst (int data); 
void InsertLast (int data); 
void PrintForward (void); 
void PrintBackward (void); 
struct Node* Search (int data); 
void DeleteNode (int Node); 


int main() 
{ 
    int choice=1 , data; 
    while (1) 
    { 
     printf("Choose from the following options\n\n"); 
     printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n"); 
     scanf("%d",&choice); 
     if(choice==1) 
     { 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsetFirst(data); 
      data =0; 
     } 
     else if (choice==2) 
     { 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsertLast(data); 
      data =0; 
     } 
     else if(choice==3) 
     { 
      printf("The list from the beginning to the End :\n"); 
      PrintForward(); 
     } 
     else if(choice==4) 
     { 
      printf("The list from the end to the beginning\n"); 
      PrintBackward(); 
     } 
     else if(choice==5) 
     { 
      printf("Enter the data you want search\n"); 
      scanf("%d",data); 
      Search(data); 
      data =0; 
     } 
     else if(choice==6) 
     { 
      printf("Enter The data you want to delete\n"); 
      scanf("%d",&data); 
      DeleteNode(data); 
     } 
     else 
     { 
      printf("Enter a Valid Choice\n"); 
     } 
    } 

/* 
    int choice,data; 
    switch(choice) 
    { 
     case 1: 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsetFirst(data); 
      data =0; 
      break; 

     case 2: 
      printf("Enter Your Data\n"); 
      scanf("%d",&data); 
      InsertLast(data); 
      data =0; 
      break; 

     case 3: 
      printf("The list from the beginning to the End :\n"); 
      PrintForward(); 
      break; 

     case 4: 
      printf("The list from the end to the beginning\n"); 
      PrintBackward(); 
      break; 

     case 5: 
      printf("Enter the data you want search\n"); 
      scanf("%d",data); 
      Search(data); 
      if(Search(data)) 
      { 
      printf("%d\n",*(Search(data))); 
      } 
      else 
      {} 
      data =0; 
      break; 

     case 6: 
      printf("Enter The data you want to delete\n"); 
      scanf("%d",&data); 
      DeleteNode(data); 
      break; 

     default : 
      printf("Not Valid Entry\n"); 
      */ 

    return 0; 
} 

/** Function to create Node in the list **/ 

struct Node* CreatNode (void) 
{ 
    struct Node* temp; 
    temp = (struct Node*) malloc(sizeof(struct Node)); 
    if (!temp) 
    { 
     printf("\nNot Enough Memory"); 
    } 
    else 
    { 
     return temp; 
    } 
} 
/**************************************************************************************/ 


/** Function to Insert Node at the Beginning of the list **/ 

void InsetFirst (int data) 
{ 
    struct Node* temp; 
    temp = CreatNode(); 
    temp ->data = data; 
    temp ->pnext = NULL; 
    temp ->pprev = NULL; 
    if (pstart == NULL) 
    { 
     pstart = temp; 
     plast = temp; 
    } 
    else 
    { 
     temp ->pnext = pstart; 
     pstart ->pprev =temp; 
     pstart = temp; 
    } 
} 
/***********************************************************************************/ 



/** Function to Insert Node at the End of the List **/ 

void InsertLast (int data) 
{ 
    struct Node* temp; 
    temp = CreatNode(); 
    temp ->data = data; 
    temp ->pnext = NULL; 
    temp ->pprev = NULL; 
    if (pstart == NULL) 
    { 
     pstart = temp; 
     plast = temp; 
    } 
    else 
    { 
     temp ->pprev = plast; 
     plast ->pnext = temp; 
     plast = temp; 
    } 
} 
/**********************************************************************************************/ 

/** Function to Print the list From the beginning to the End **/ 

void PrintForward (void) 
{ 
    struct Node* current; 
    current = pstart; 
    printf("\nThe list From the Beginning to the End :\n"); 
    while (current) 
    { 
     printf("\n%d",current->data); 
     current = current->pnext; 
    } 
    printf("\n"); 
} 
/*********************************************************************************************/ 

void PrintBackward (void) 
{ 
    struct Node* current; 
    current = plast; 
    printf("\nThe list From End to the Beginning :\n"); 
    while (current) 
    { 
     printf("\n%d",current->data); 
     current = current->pprev; 
    } 
    printf("\n"); 
} 
/*********************************************************************************************/ 

/** Function To Find a Given Data **/ 

struct Node* Search (int data) 
{ 
    struct Node* current; 
    current = pstart; 
    if (current) 
    { 
     while(current) 
     { 
      if (current->data == data) 
      { 
       return current; 
      } 
      current = current->pnext; 
     } 
     printf("\nIt's not found\n"); 
     return NULL; 
    } 

} 
/**************************************************************************************/ 

/** Function to Delete a Given Node **/ 

void DeleteNode (int Node) 
{ 
    struct Node* state; 
    state = Search(Node); 

    if (state) 
    { 
     if ((state == pstart) && (state == plast)) 
     { 
      pstart = NULL; 
      plast = NULL; 
     } 
     else if (pstart == state) 
     { 
      pstart = state->pnext; 
      state->pnext->pprev = NULL; 
     } 
     else if (plast == state) 
     { 
      plast = state->pprev; 
      state->pprev->pnext = NULL; 
     } 
     else 
     { 
      state->pprev->pnext = state->pnext; 
      state->pnext->pprev = state->pprev; 
     } 
     free(state); 
    } 
    else 
    { 
     printf("NOT Found\n"); 
    } 
} 
+1

Zeigen Sie die Fehlermeldung. –

+1

Sind Sie sicher, dass Sie das "}" Ihres Swithc nicht vergessen haben und die Auswahl vorher initialisiert haben? – bobra

+0

Sicher haben Sie ein '}' vergessen, um den 'switch' Code zu schließen. Sie können es sowohl beim ersten Mal sehen, wenn Sie es zeigen, als auch beim zweiten Mal, wenn Sie es kommentiert haben. – SHG

Antwort

1

Es gab ein paar Probleme. Ich habe mit der Switch-Version gearbeitet und es gab auch Probleme mit der if else-Version.

case 5: 
    printf("Enter the data you want search\n"); 
    scanf("%d",data); 
    Search(data); 
    if(Search(data)) 
    { 
    printf("%d\n",*(Search(data))); 
    } 
    else 
    {} 
    data =0; 
    break; 

Wenn Sie scanf Sie einen Zeiger auf den Ort schicken müssen, wo Sie etwas gespeichert werden soll, so wird es scanf("%d", &data) sein. Auch printf des% d benötigt int-Wert als Argument aber hier:

printf("%d\n",*(Search(data)));

Sie ihm einen Knoten senden, und so wird es nicht akzeptieren. Sie müssen die Daten zu senden, die innerhalb der Knoten ist, so dass Sie dies tun:

printf("%d\n",(*(Search(data))).data);

Und Sie brauchen nicht mit einem leeren Block anderes zu haben, wenn Sie es entfernen, es wird nicht das Programm beeinflussen .

Jetzt haben wir ein Problem in der CreatNode-Funktion, die nichts zurückgibt, wenn temp null ist. Sie müssen also in der null zurück, wenn Block, falls nicht genügend Speicher vorhanden war:

struct Node* CreatNode (void) 
{ 
    struct Node* temp; 
    temp = (struct Node*) malloc(sizeof(struct Node)); 
    if (!temp) 
    { 
     printf("\nNot Enough Memory"); 
     return NULL; //YOU NEED TO RETURN NULL HERE 
    } 
    else 
    { 
     return temp; 
    } 
} 

Funktion Suchen wird nichts zurück, wenn zum Beispiel erste Strom gleich NULL, so dass Sie Rückkehr und printf Linie heraus bewegen müssen von if-Block wie folgt aus:

struct Node* Search (int data) 
{ 
    struct Node* current; 
    current = pstart; 
    if (current) 
    { 
     while(current) 
     { 
      if (current->data == data) 
      { 
       return current; 
      } 
      current = current->pnext; 
     } 
    } 
    printf("\nIt's not found\n"); 
    return NULL; 
} 

und das letzte, was, und der Grund, warum Ihr Schalter nicht funktioniert ist, weil es nicht in einer Schleife ist. Schalten Sie selbst ist keine Schleife, so müssen Sie es in eine While-Schleife, die funktioniert, bis zum Beispiel Benutzer 0 eingibt. So wird dies eine Lösung sein:

int main() 
{ 
    int choice=-1,data; 
    while(choice != 0) 
    { 
     printf("\n\nChoose from the following options\n\n"); 
     printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n"); 
     scanf("%d", &choice); 
     switch(choice) 
     { 
      case 1: 
       /*...*/ 

      case 2: 
       /*...*/ 

      case 3: 
       /*...*/ 

      case 4: 
       /*...*/ 

      case 5: 
       /*...*/ 

      case 6: 
       /*...*/ 

      case 0: 
       break; 

      default : 
       printf("Not Valid Entry\n"); 

     } 
    } 
    return 0; 
} 
+0

danke für deine Hilfe Mann, wirklich zu schätzen :) aber nach dem Anwenden Ihrer Korrekturen (die wertvoll waren) dieser Fehler immer noch "| Fehler: erwartete Erklärung oder Erklärung am Ende der Eingabe | | 290 | Warnung: Kontrolle erreicht Ende von nicht Funktion [-Wreturn] | || === Build fehlgeschlagen: 1 Fehler, 1 Warnung (en) (0 Minute (n), 2 Sekunde (n)) === | ". ... wenn Sie dabei helfen können, werde ich dankbar sein – kiko

+0

Ich hatte es voll funktionsfähig mit (if-else), aber ich muss es auch mit dem (Schalter) – kiko

+0

arbeiten lassen Nach der Korrekturen arbeitete Ihr Code gut für Ich, aber sag mir, in welcher Funktion erscheint der Fehler? – Nebeski