views:

108

answers:

2

The following code was been given from our school as a sample for circular linkedlist and told each student to build a own version of circular linkedlist. Now my query is that is the following code is really a circular linkedlist ?

// 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()*/

The reason why i am not agreeing is why NULL is used to check the last node in the list ?

A: 

I didn't scrutinize in great detail, but it looks like it is indeed a linked list. As for a circular linked list, I haven't come across that before.

The check for NULL is testing whether the node has a next node. If it doesn't (NULL), then it is the last node.

wallyk
A *circular linked list* is a normal linked list where there's no *last* node, or "last node points back to first node", so to speak. You just hold a pointer to *any* node in the list and iterate until you are back where you started.
Nikolai N Fetissov
@Nikolai N Fetissov: Yes exactly !But here in this list the last node should always point to head instead it is checked that weather the last node is NULL or not.I hope you can understand what i mean.in circular list there is no point of using a NULL...I think the above code is a plain singly linkedlist or rather modifies singly linked list
Tuhin
@Tuhin, when the list is empty, your pointer is `NULL`, so you have to check for it.
Nikolai N Fetissov
+3  A: 

Yes, the code implements a circular linked list

The variable last is only NULL at the beginning, after adding the first element lasts link will point to itself. See funtion createlist.

Non circular lists always have the last elements next pointer set to NULL ti indicate the end of the list.

Edit: Sorry I can't do ascii art with my ipad.

Start:
last=NULL

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

call addatbeg( 15 )

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

To understand how these pointers work I would recommend to draw a simple diagramm based on the instructions in the code. Hope this helps.

stacker
Yep, definitely circular. The point where its most obvious is in `addafter` where it checks for `if(q == last->link)` instead of `if(q == NULL)` to detect if it has run out of list items.
torak
Tuhin
@Tuhin I will edit the post later it's not possible right now
stacker
@stacker: Sure! Take your time
Tuhin