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

时间:2022-09-05 19:56:05

I am having a problem with my Circular Linked list. I believe the problem is with my display function. Please let me know what is going wrong. The problem I have is that the first n-1 elements are displayed and then I get a segmentation fault(The last element doesn't get displayed and I get a segmentation fault).Thank you :-)

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

#1


1  

When you insert the first element into the list, you must its link point to itself:

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

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

In your code, the link from the last element was uninitialised.

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

#2


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

#3


0  

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

That solve the problem

那解决了问题

#4


0  

Below is corrected code:
one mistake you have done that is at inserting first value you are not pointing the link to first node itself.In circular singly linked list if there is one node then link(next in general) field should be pointed to that node itself.

下面是更正的代码:你做的一个错误是插入第一个值你没有指向第一个节点本身的链接。在圆形单链表中如果有一个节点那么链接(下一般)字段应该指向那个节点本身。

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

*

*

#5


0  

Problem is with Insert_begin(int a) function, When you are inserting first node, we are not linking his next to itself, so next time while inserting second/third/.. node, ou trying to access first node as last->link but it gives you garbage value and that is reason

问题在于使用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;
               }
 }**

Also In display function, I would suggest to put everything below the if condition into else block

另外在显示功能中,我建议将if条件下的所有内容放入else块中

**

**

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

#1


1  

When you insert the first element into the list, you must its link point to itself:

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

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

In your code, the link from the last element was uninitialised.

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

#2


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

#3


0  

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

That solve the problem

那解决了问题

#4


0  

Below is corrected code:
one mistake you have done that is at inserting first value you are not pointing the link to first node itself.In circular singly linked list if there is one node then link(next in general) field should be pointed to that node itself.

下面是更正的代码:你做的一个错误是插入第一个值你没有指向第一个节点本身的链接。在圆形单链表中如果有一个节点那么链接(下一般)字段应该指向那个节点本身。

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

*

*

#5


0  

Problem is with Insert_begin(int a) function, When you are inserting first node, we are not linking his next to itself, so next time while inserting second/third/.. node, ou trying to access first node as last->link but it gives you garbage value and that is reason

问题在于使用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;
               }
 }**

Also In display function, I would suggest to put everything below the if condition into else block

另外在显示功能中,我建议将if条件下的所有内容放入else块中

**

**

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