2016-07-19 16 views
-1
#include <stdio.h> 
#include <stdlib.h> 

typedef struct nodeNum 
{ 
    int num; 
    struct nodeNum *next; 
} t_nodeNum; 


// Functions declaration ---------------------------- 
int menu(); // display menu and return choice 

t_nodeNum* addition(t_nodeNum *node, int n); 

void print_list(t_nodeNum *node); 
// ---------------------------------------------------- 

// Main program to test link list functions 
int main() 
{ 
    int choice; 

    t_nodeNum *pnode = NULL; 
    t_nodeNum *head = NULL; 
    t_nodeNum *temp = NULL; 

    int numAdd = 0; 
    int len = 0; 
    int first = 1; 

    do 
    { 
     choice = menu(); 

     switch (choice) 
     { 
      case 1: 
      { 
       printf("Please enter number : \n"); 
       scanf("%d", &numAdd); 
       if (first) 
       { 
        pnode = (t_nodeNum *)malloc(sizeof(t_nodeNum)); 
        if (pnode == NULL) 
        { 
         printf("\n Error in allocation\n"); 
         exit(0); 
        } 

        pnode->num = numAdd; 
        pnode->next = NULL; 
        first = 0; 
        head = pnode; 
       } 
       pnode = addition(pnode, numAdd); 
       break; 
      } 

      case 4: 
      { 
       printf("\n Print List: "); 
       print_list(head); 
       break; 
      } 
     } 
    } 
    while (choice != 5); 

    return 0; 
} 

// function menu display menu and return choice 
int menu() 
{ 
    int choice = 0; 

    do 
    { 
     printf("Please choose option to do: \n"); 
     printf("1. addition\n"); 
     printf("2. deletion\n"); 
     printf("3. search\n"); 
     printf("4. print\n"); 
     printf("5. exit\n"); 
     printf("\n option = "); 

     scanf("%d", &choice); 
    } 
    while (choice < 1 || choice > 5); 

    return choice; 
} 

// function addition to add item to linked list in recursion 
t_nodeNum* addition(t_nodeNum *p, int numAdd) 
{ 
    int len = 0; 

    if (p == NULL) 
    { 
     p = (t_nodeNum *)malloc(sizeof(t_nodeNum)); 
     if (p == NULL) 
     { 
      printf("\n Error in allocation\n"); 
      exit(0); 
     } 

     p->num = numAdd; 
     p->next = NULL; 
    } 
    else 
    { 
     p = addition(p->next, numAdd); 
    } 
    return (p); 

} 

// function print_list to print linked list in recursion 
void print_list(t_nodeNum *head) 
{  
     printf("%d ", head->num); 
     if (head->next == NULL) 
     { 
      printf("\n"); 
      return; 
     } 

     print_list(head->next); 

} 

Es gibt ein Problem mit der Zusatz-Funktion nicht richtig neues Element der verknüpften Liste hinzufügen funktioniert und ich weiß nicht, was falsch ist, bitte Nach dem Hinzufügen neue Produkte helfen und Druckliste Sie zeigen nur das erste ElementLinkliste hinzufügen Elemente in Rekursion Sprache C

+0

Rekursion ist der falsche Weg zu gehen. Was ist, wenn Sie 10.000 Knoten haben? Der verwendete Stapelspeicher wäre riesig. Iterieren Sie die Liste mit einem Zeiger. –

+0

@SteveWellens Alle Rekursion hier ist Tail-Rekursion, nicht wahr? –

+0

Es gibt drei Fälle, die behandelt werden müssen: 'p == NULL',' p-> next == NULL' und 'p-> next! = NULL'. – user3386109

Antwort

0

In Sie main Funktion -

pnode = addition(pnode, numAdd); 

statt pnode Sie pnode->next passieren müssen -

pnode = addition(pnode->next, numAdd); 

Problem ersten Aufruf ist, dass pnode ist nicht so NULL es kommt noch hinzu, neues Element an Kopfposition vorherigen Wert und kehrt zu ersetzen.

Daher wird kein neuer Knoten erstellt.

+0

Hallo Ich versuche deinen Vorschlag und ich änderte in main auf pnode = addition (pnode-> next, numAdd); aber es behebt das Problem nicht, da ist noch etwas Buggy !!! – user3879626