c - 这段代码是循环链表吗

标签 c data-structures circular-list

<分区>

以下代码是我们学校提供的循环链表示例,并告诉每个学生构建一个自己的循环链表版本。

我的问题是,下面的代码真的是循环链表吗?

// Program of circular linked list
#include <stdio.h>
#include <malloc.h>

struct node
{
    int info;
    struct node *link;
}*last;

main()
{
    int choice,n,m,po,i;
    last=NULL;
    while(1)
    {
        printf("1.Create List\n");
        printf("2.Add at begining\n");
        printf("3.Add after \n");
        printf("4.Delete\n");
        printf("5.Display\n");
        printf("6.Quit\n");
        printf("Enter your choice : ");
        scanf("%d",&choice);

        switch(choice)
        {
         case 1:
            printf("How many nodes you want : ");
            scanf("%d",&n);
            for(i=0; i < n;i++)
            {
                printf("Enter the element : ");
                scanf("%d",&m);
                create_list(m);
            }
            break;
         case 2:
            printf("Enter the element : ");
            scanf("%d",&m);
            addatbeg(m);
            break;
         case 3:
            printf("Enter the element : ");
            scanf("%d",&m);
            printf("Enter the position after which this element is inserted : ");
            scanf("%d",&po);
            addafter(m,po);
            break;
         case 4:
            if(last == NULL)
            {
                printf("List underflow\n");
                continue;
            }
            printf("Enter the number for deletion : ");
            scanf("%d",&m);
            del(m);
            break;
         case 5:
            display();
            break;
         case 6:
            exit(0);
         default:
            printf("Wrong choice\n");
        }/*End of switch*/
    }/*End of while*/
}/*End of main()*/

create_list(int num)
{
    struct node *q,*tmp;
    tmp= malloc(sizeof(struct node));
    tmp->info = num;

    if(last == NULL)
    {
        last = tmp;
        tmp->link = last;
    }
    else
    {
        tmp->link = last->link; /*added at the end of list*/
        last->link = tmp;
        last = tmp;
    }
}/*End of create_list()*/

addatbeg(int num)
{
    struct node *tmp;
    tmp = malloc(sizeof(struct node));
    tmp->info = num;
    tmp->link = last->link;
    last->link = tmp;
}/*End of addatbeg()*/

addafter(int num,int pos)
{

    struct node *tmp,*q;
    int i;
    q = last->link;
    for(i=0; i < pos-1; i++)
    {
        q = q->link;
        if(q == last->link)
        {
            printf("There are less than %d elements\n",pos);
            return;
        }
    }/*End of for*/
    tmp = malloc(sizeof(struct node) );
    tmp->link = q->link;
    tmp->info = num;
    q->link = tmp;
    if(q==last)    /*Element inserted at the end*/
        last=tmp;
}/*End of addafter()*/

del(int num)
{
    struct node *tmp,*q;
    if( last->link == last && last->info == num)  /*Only one element*/
    {
        tmp = last;
        last = NULL;
        free(tmp);
        return;
    }
    q = last->link;
    if(q->info == num)
    {
        tmp = q;
        last->link = q->link;
        free(tmp);
        return;
    }
    while(q->link != last)
    {
        if(q->link->info == num)     /*Element deleted in between*/
        {
            tmp = q->link;
            q->link = tmp->link;
            free(tmp);
            printf("%d deleted\n",num);
            return;
        }
        q = q->link;
    }/*End of while*/
    if(q->link->info == num)    /*Last element deleted q->link=last*/
    {
        tmp = q->link;
        q->link = last->link;
        free(tmp);
        last = q;
        return;
    }
    printf("Element %d not found\n",num);
}/*End of del()*/

display()
{
    struct node *q;
    if(last == NULL)
    {
        printf("List is empty\n");
        return;
    }
    q = last->link;
    printf("List is :\n");
    while(q != last)
    {
        printf("%d ", q->info);
        q = q->link;
    }
    printf("%d\n",last->info);
}/*End of display()*/

我不同意的原因是因为NULL用于检查列表中的最后一个节点。

最佳答案

是的,代码实现了一个circular linked list

last变量一开始只是NULL,加入第一个元素后,lass链接会指向它自己。请参阅函数创建列表。

非循环列表总是将最后一个元素的下一个指针设置为 NULL 以指示列表的结尾。

编辑: 抱歉,我不能用我的 ipad 做 ascii 艺术。

Start:
last=NULL

call createlist( 12 )
Result: Last(12) -> Last

call addatbeg( 15 )

Result:
tmp( 15 ) -> Last( 12 -)
 ^                 |
 |                 |
 +----<-------<----+

为了理解这些指针是如何工作的,我建议画一个简单的图表 根据代码中的说明。希望这会有所帮助。

关于c - 这段代码是循环链表吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3473390/

相关文章:

php - 如何在php中制作一个循环链表?

c - 通过按位运算在 C 中解析 WEXITSTATUS int-summed 返回代码

c - 添加 HTML 表格标签

c - 将数据存储到多维数组中

c++ - 实现一个指向可变大小结构的指针

java - 当元素过期时具有信令功能的过期映射

java数据结构模拟数据树

c - 数组搜索 C 中的段错误

c - 为什么我的 C 循环双端队列程序无法退出打印功能?

双向循环链表删除节点的正确方法