【问题标题】:Is this code a Circular linkedlist [closed]这段代码是循环链表吗[关闭]
【发布时间】:2010-08-13 02:02:20
【问题描述】:

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

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

// 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是用来检查列表中最后一个节点的。

【问题讨论】:

    标签: c data-structures circular-list


    【解决方案1】:

    是的,代码实现了circular linked list

    变量last开头只有NULL,加上第一个元素lasts链接后会指向自己。请参阅函数 createlist。

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

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

    Start:
    last=NULL
    
    call createlist( 12 )
    Result: Last(12) -> Last
    
    call addatbeg( 15 )
    
    Result:
    tmp( 15 ) -> Last( 12 -)
     ^                 |
     |                 |
     +----<-------<----+
    

    要了解这些指针的工作原理,我建议您绘制一个简单的图表 根据代码中的说明。希望这会有所帮助。

    【讨论】:

    • 是的,绝对是循环的。最明显的地方是addafter,它检查if(q == last-&gt;link) 而不是if(q == NULL),以检测它是否已用完列表项。
    • @torak & @stacker: addatbeg 函数中 (tmp->link = last->link) 的值是多少,它是否指向列表中的第一个节点?还有 q 在 (q = last->link) 中会指向什么,需要有点困惑的解释......
    • @Tuhin 我稍后会编辑帖子,现在不可能
    【解决方案2】:

    我没有仔细看,但是看起来确实是一个链表。至于循环链表,我还没有遇到过。

    NULL 的检查是测试节点是否有 next 节点。如果不是(NULL),那么它是最后一个节点。

    【讨论】:

    • 一个循环链表是一个没有last节点的普通链表,或者说“最后一个节点指向第一个节点”,所以说话。您只需持有指向列表中 any 节点的指针并迭代,直到回到开始的位置。
    • @Nikolai N Fetissov:没错!但在此列表中,最后一个节点应始终指向 head,而不是检查最后一个节点是否为 NULL。我希望你能明白我的意思。在循环列表中,使用 NULL 没有意义......我认为上面的代码是一个普通的单链表,或者更确切地说是修改单链表
    • @Tuhin,当列表为空时,你的指针是NULL,所以你必须检查它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    相关资源
    最近更新 更多