C Program To Implement Circular Linked List

C Program to implement circular linked list.

c program to implement circular linked list c or c++ program source code for linked list implementation with example



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

struct node
    {
    int info;
    struct node *link;
    }*last;
void create_list(int num);
void addatbeg(int num);
void addafter(int num,int pos);
void del(int num);
void display();
void 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("nter 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");
           }
       }
   }
void create_list(int num)
   {
   struct node *temp;
   temp=(node*)malloc(sizeof(struct node));
   temp->info = num;
   if(last == NULL)
       {
       last = temp;
       temp->link = last;
       }
   else
       {
       temp->link = last->link;
       last->link = temp;
       last = temp;
       }
   }

void addatbeg(int num)
    {
    struct node *temp;
    temp =(node*)malloc(sizeof(struct node));
    temp->info = num;
    temp->link = last->link;
    last->link = temp;
    }
void addafter(int num,int pos)
    {
    struct node
    *temp,*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;
    }
       }
    temp =(node*)malloc(sizeof(struct node) );
    temp->link = q->link;
    temp->info = num;
    q->link = temp;
    if(q==last)  //Element inserted at the end
        last=temp;
    }//End of addafter()

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

void 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);
   }

No comments :

Post a Comment