【问题标题】:Display function in Circular Linked List in CC语言循环链表中的显示函数
【发布时间】:2015-10-14 06:40:39
【问题描述】:

我的循环链接列表有问题。我相信问题出在我的显示功能上。请让我知道出了什么问题。我遇到的问题是显示前 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;
             }

【问题讨论】:

  • '请让我知道出了什么问题':您没有进行任何调试。
  • 是的!我需要学习调试。就要开始做了。谢谢
  • 如果您有兴趣,还有一个完整的A Singularly Linked Circular Linked List 示例可能会很有用。
  • 是的,当然!谢谢大卫。

标签: c linked-list


【解决方案1】:

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

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

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

【讨论】:

  • 嗨哦!感谢您的帮助。
  • @Sameer 如果你得到答案意味着接受它。
【解决方案2】:
#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】:
             if (last == NULL)
               {
                last = temp;
                **// adding this line 
                last->link = last;**
               }

解决问题

【讨论】:

  • 嗨,索希尔!感谢您的帮助。
【解决方案4】:

以下是更正的代码:
您犯的一个错误是在插入第一个值时您没有将链接指向第一个节点本身。在循环单链表中,如果有一个节点,则链接(通常为下一个)字段应指向该节点本身。

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

*

【讨论】:

  • 嗨,杰!感谢您的帮助。
  • Sameer_184:您能否为您的帮助将我的分数加 1?请接受我的回答..
  • 也许如果您提供更多解释而不是“这是固定代码”,您将有更好的机会...
  • 嗨,杰!我确实投了赞成票,但因为我是新来的。它没有出现。我需要至少 15 分之类的。
【解决方案5】:

问题在于 Insert_begin(int a) 函数, 当您插入第一个节点时,您并没有将他的 next 链接到自身,因此下次插入第二个/第三个/.. 节点时,您尝试将第一个节点作为 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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-07
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多