C中循环链表中的显示功能

时间:2015-10-14 06:40:39

标签: c linked-list

我的循环链接列表出了问题。我相信问题在于我的显示功能。请告诉我出了什么问题。我遇到的问题是显示第一个n-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;
               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;
             }

5 个答案:

答案 0 :(得分:1)

将第一个元素插入列表时,必须将其链接指向自身:

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

在您的代码中,最后一个元素的链接未初始化。

答案 1 :(得分: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;
}

答案 2 :(得分:0)

以下是更正后的代码:
你做的一个错误就是在插入第一个值时你没有将链接指向第一个节点本身。在圆形单链表中如果有一个节点则链接(通常是下一个)字段应指向该节点本身。

#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;
    }

*

答案 3 :(得分:0)

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

解决问题

答案 4 :(得分:0)

问题出在Insert_begin(int a)函数上, 当您插入第一个节点时,您不会将其下一个链接到自身,因此,下次在插入第二个/第三个/ ..节点时,您尝试将第一个节点作为last-> link访问,但这给了您垃圾值,这就是原因

void Insert_begin(int a)
 { 
               struct Node* temp;
               temp = malloc(sizeof(struct Node));
               temp->data = a;    
               if (last == NULL)
               {
                 last = temp;
                 last->link = last;
               }
               else
               {
                temp->link = last->link;
                last->link = temp;
                last = temp;
               }
 }

void Display()
  {   
               struct Node* temp;    
               if (last == NULL)
               {
                 printf("list is empty");
               }    
              else
               {    
                    temp = last->link;
                    while(temp!=last);
                    {
                           printf("%d\n",temp->data);
                           temp = temp->link;     
                    } 
                    printf("%d\n",temp->data); 
               }  
  }
  void main()
  {
      Insert_begin(10);
      Insert_begin(20);
      Insert_begin(30);
      Display();
    }