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

问题描述 投票:0回答:5

我的循环链接列表有问题。我相信问题在于我的显示功能。请告诉我出了什么问题。我的问题是显示第一个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;
             }
c linked-list
5个回答
1
投票

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

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

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


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
投票
             if (last == NULL)
               {
                last = temp;
                **// adding this line 
                last->link = last;**
               }

那解决了这个问题


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

*


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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.