2014-09-26 160 views
0
#include<stdio.h> 
    #include<stdlib.h> 
    //Linked list implementation 

    typedef struct SLL{ 

     int info; 
     struct SLL *link; 

    }Node; 

    Node *head=NULL; 
// Node *rear=NULL; 

void insert_rear(int x) 
    { 

    Node *temp=malloc(sizeof(Node)); 
    Node *temp1=NULL; 

    if(temp==NULL) /* When malloc is unable to fetch Memory */ 
    { 
     printf("\n Insufficient memory"); 
    } 

    if(head==NULL) /* When there is no node created */ 
    { 
     temp->info=x; 
     temp->link=head; 
     head=temp; 
    } 

    else 
    temp1=head; 

    while(temp1->link!=NULL) 
    { 
     temp1=temp1->link; 
    } 
    printf("\n Temp1=%d",temp1); 
    temp->info=x; 
    temp->link=NULL; 
    temp1->link=temp; 

    } 


    void insert_front(int x) 
    { 

    Node *temp=malloc(sizeof(Node)); 
    if(temp==NULL) /* When malloc is unable to fetch Memory */ 
    { 
     printf("\n Insufficient memory"); 
    } 
    temp->info=x; 
    temp->link=head; 
    head=temp; 

    } 

    void display() 
    { 
    int i=0; 
    Node *temp=head; 
    printf("\n List Elements: \n "); 

    while(temp!=NULL) 
    { 
     printf(" %d) %d",++i,temp->info); 
     temp=temp->link; 
     printf("\t Link= %u \n",temp); 
    } printf("\n"); 
    } 


    void main() 
    { 
    int x,choice,i; 
    printf("\n To insert at front enter 1 \n To insert at rear enter 2 \n To exit enter 4 \n"); 
    while(choice!=4) 
    { 
     scanf("%d",&choice); 
     switch(choice) 
     { 

     case 1: printf("Enter an ELEMENT to be inserted at FRONT \n"); 
       scanf("%d",&x); 
       insert_front(x); 
       display(); 
       break; 

     case 2: printf("Enter an ELEMENT to be inserted at LAST \n"); 
       scanf("%d",&x); 
       insert_rear(x); 
       display(); 
       break; 


     }//End of switch 

    }//End of while 
    }//End of main 

我正在編寫這個鏈表程序,並且我在insert_rear()函數中提出了一個問題。 當我使用insert_front()添加幾個元素,然後使用insert_rear()與後面的現有節點一起添加元素時,程序工作得很好。 但是,當我嘗試使用insert_rear()添加一個沒有任何現有節點的節點時,我的程序因某種原因不起作用。鏈接列表 - 插入新節點

所以我花了一些時間與我的程序搞亂,並刪除下面的代碼部分,看看我是否能夠添加一個新的節點,而無需任何現有節點:

else 
    temp1=head; 

    while(temp1->link!=NULL) 
    { 
     temp1=temp1->link; 
    } 
    printf("\n Temp1=%d",temp1); 
    temp->info=x; 
    temp->link=NULL; 
    temp1->link=temp; 

    } 

和它的工作工作,即只與下面的代碼我能夠有任何現有節點

if(head==NULL) /* When there are no existing nodes created */ 
    { 
     temp->info=x; 
     temp->link=head; 
     head=temp; 
    } 

,但隨着其他條件我的代碼不能正常工作和程序崩潰之前,向其添加一個新的節點。 請幫我改正這個錯誤。我有一種感覺,我做了一些我無法找到的蠢事。

+2

使用'{}'爲else塊。 – BLUEPIXY 2014-09-26 09:50:31

+0

在發佈問題後,Yup意識到。 – 2014-09-26 09:57:29

+0

沒關係,我只是意識到我做了一些愚蠢的事情,我沒有添加其他的括號。 – 2014-09-26 09:56:43

回答

0

你應該小心使用全局變量,最好是完全避免它們。 在功能insert_front()更改列表的頭......

約翰內斯

1

當列表是空的,添加的第一個元素,你忘了退出,而是繼續向下在你的功能。嘗試這樣的代替

void insert_rear(int x) 
{ 
    Node *temp=malloc(sizeof(Node)); 
    Node *temp1=NULL; 

    temp->info=x; 
    temp->link=NULL; 

    if(temp==NULL) /* When malloc is unable to fetch Memory */ 
    { 
    printf("\n Insufficient memory"); 
    abort(); 
    } 

    if(head==NULL) /* When there is no node created */ 
    { 
    head=temp; 
    } 
    else 
    { 
    temp1=head; 

    while(temp1->link!=NULL) 
    { 
     temp1=temp1->link; 
    } 
    printf("\n Temp1=%d",temp1); 
    temp1->link=temp; 
    } 
}