2015-10-14 14 views
0

我的循环链表有问题。我相信问题出在我的显示功能上。请让我知道发生了什么问题。我的问题是,第n-1个元素都显示出来,然后我得到一个分段错误(最后一个元素没有得到显示,我得到一个分段错误)。谢谢:-)在C中的循环链表中显示函数

   #include<stdio.h> 
      #include<stdlib.h> 
      struct Node 
      { 
       int data; 
       struct Node* link; 
      }; 
      struct Node* last = NULL; 
      void Insert_begin(int a) 
      { 
       struct Node* temp; 
       temp = malloc(sizeof(struct Node)); 
       temp->data = a;  
       if (last == NULL) 
       last = temp; 
       else 
       { 
       temp->link = last->link; 
       last->link = temp; 
       } 
      } 
      void Display() 
      { 
       struct Node* temp;  
       if (last == NULL) 
       { 
       printf("list is empty"); 
       }   
       temp = last->link; 
       while(temp!=last) 
       { 
       printf("%d\n",temp->data); 
       temp = temp->link;  
       } 
       printf("%d\n",temp->data); 
      } 
      int main() 
      { 
       Insert_begin(0);    
       Insert_begin(1); 
       Insert_begin(2); 
       Insert_begin(3); 
       Insert_begin(4); 
       Display(); 
       return 0; 
      } 
+0

'请让我知道了什么错误':你是不是做任何调试。 –

+0

是啊!我需要学习调试。开始做吧。谢谢 –

+0

如果你感兴趣的话,还有一个可能证明有用的[**一个奇异链接的循环链接列表**]的完整例子(http://pastebin.com/BirWtkvb)。 –

回答

1

当您插入第一个元素到列表中,你必须将其链接指向自己:

if (last == NULL) { 
    last = temp; 
    last->link = last; 
} else ... 

在你的代码,从最后一个元素的链接是未初始化。

+0

嗨噢!谢谢你的帮助。 –

+0

@ Sameer如果你有答案意味着接受它。 – Vinoth

0

下面是更正后的代码:
你已经做了一个错误是在插入你没有指向的链接第一个节点第一个值itself.In循环单向链表,如果有一个节点,然后链接(下一一般)字段应该指向该节点本身。

#include<stdio.h> 
    #include<stdlib.h> 
    struct Node 
    { 
     int data; 
     struct Node* link; 
    }; 

    struct Node* last = NULL; 

    void Insert_begin(int a) 
    { 
     struct Node* temp; 
     temp = malloc(sizeof(struct Node)); 
     temp->data = a; 

     if (last == NULL) 
     { 
      last = temp; 

      /*link field is pointing to that node 
      *it self(you have forgotten this)*/ 
      last->link = temp; 
     } 
     else 
     { 
      temp->link = last->link; 
      last->link = temp; 
     } 
    } 



    void Display() 
    { 

     struct Node* temp; 

     if (last == NULL) 
     { 
      printf("list is empty"); 
     } 

     temp = last->link; 
     while(temp!=last) 
     { 
      printf("%d\n",temp->data); 
      temp = temp->link; 

     } 
     printf("%d\n",temp->data); 

    } 

    int main() 
    { 
     Insert_begin(0); 

     Insert_begin(1); 
     Insert_begin(2); 
     Insert_begin(3); 
     Insert_begin(4); 
     Display(); 
     return 0; 
    } 

*

+0

嗨Jay!谢谢你的帮助。 –

+0

Sameer_184:你是否会把我的观点提高1点,并请你接受我的答案.. –

+0

也许如果你提供了更多的解释而不是“固定代码”,你会有更好的机会...... –

0
   if (last == NULL) 
       { 
       last = temp; 
       **// adding this line 
       last->link = last;** 
       } 

那解决问题

+0

嗨Sohil!谢谢你的帮助。 –

1
#include<stdio.h> 
#include<stdlib.h> 
struct Node 
{ 
     int data; 
     struct Node* link; 
}; 

struct Node* last = NULL; 

void Insert_begin(int a) 
{ 
     struct Node* temp; 
     temp = malloc(sizeof(struct Node)); 
     temp->data = a; 

     if (last == NULL) 
     { 
       last = temp; 
       temp->link=last;//you forget this 
     } 
     else 
     { 
       temp->link = last->link; 
       last->link = temp; 
       last=temp; 
     } 
} 



void Display() 
{ 

     struct Node* temp; 

     if (last == NULL) 
     { 
       printf("list is empty"); 
       return; 
     } 

     temp = last->link; 
     while(temp!=last) 
     { 
       printf("%d\n",temp->data); 
       temp = temp->link; 

     } 
     printf("%d\n",temp->data); 

} 

int main() 
{ 
     Insert_begin(0); 

     Insert_begin(1); 
     Insert_begin(2); 
     Insert_begin(3); 
     Insert_begin(4); 
     Display(); 
     return 0; 
} 
+0

嗨,lucifer!谢谢你的帮助。 –

+0

请接受此答案..... – lucifer