Friday, 23 September 2016

Circular Singly Linked List: Insert at Front

Circular Singly Linked List: Insert at Front


#include<stdio.h>
#include<stdlib.h>

struct node
{
                int data;
                struct node *link;
};
typedef struct node* NODE;
NODE getnode();
NODE insert_at_front(NODE last, int item);
void display(NODE last);

NODE getnode()
{
                NODE x;
                x =(NODE) malloc(sizeof(struct node));
                return x;
}


NODE insert_at_front(NODE last, int item)
{
                NODE temp;
                temp = getnode();
                temp->data = item;

                if(last == NULL)                                                              /*Case 1: List is empty*/
                {
                                last = temp;
                }
                else                                                                                       /*Case 2: If not empty*/
                {
                                temp->link = last->link;
                }
                last->link = temp;
                return last;
}

void display(NODE last)
{
                NODE cur;
                printf("\nContents are:\n");
                if(last == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                cur = last->link;
                                printf("==> ");
                                while(cur != last)
                                {
                                                printf("| %d | %d | => ", cur->data, cur->link);
                                                cur = cur->link;
                                }
                                printf("| %d | %d | => ", cur->data,  cur->link);
                 }
}

void main()
{
                int ch, item;
                NODE last = NULL;
                while(1)
                {
                                printf("\n\n~~MENU~~");
                                printf("\n1.Insert at front");
                                printf("\n2.Display");
                                printf("\n3.exit");
                                printf("\nEnter your choice: ");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                            case 1:                      printf("\nEnter the value: ");
                                                                                scanf("%d", &item);
                                                                                last = insert_at_front(last, item);
                                                                                break;
                                                case 2:                  display(last);
                                                                                break;
                                                case 3:                  exit(0);
                                }
                }
}

Output:

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 1
Enter the value: 11

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 2
Contents are:
==> | 11 | 7804784 | =>

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 2
Contents are:
==> | 12 | 7804784 | => | 11 | 7804680 | =>

~~MENU~~
1.Insert at front
2.Display
3.exit
Enter your choice: 3

Also Check,