Circular Link List

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct lnklist
{
	int data;
   struct lnklist *next;                 
};
typedef struct lnklist NODE ;
NODE *start=NULL;

void insertAtBeg();
void insertAtAnyPos();
void insert();                                      
void display();
void delet();
void deleteAtEnd();
void deleteAtBeg();

int main (void)
{
	int ch=0;
   while(ch!=8)
   {
   	printf("\nEnter your choise:\n1. Insert At Beg\n2. Insert At Any Position\n3. Insert At End\n4. Display\n5. Delete At Begining\n6. Delete At Any Position \n7. delete At End\n8. Exit\n");
   	scanf("%d",&ch);
      switch(ch)
      {
      	case 1: insertAtBeg();
         	break;
         case 2: insertAtAnyPos();
         	break;
         case 3: insert();
         	break;
         case 4: display();
         	break;
         case 5: deleteAtBeg();
         	break;
         case 6: delet();
         	break;
         case 7: deleteAtEnd();
         	break;
      }
   }
   return 0;
}


void insert()
{
	NODE *p,*node;
   p=(NODE*)malloc(sizeof(NODE));
   if(p==NULL)
   	printf("NO Space!");
   else
   {
   	printf("Enter the element in node: ");
      scanf("%d",&p->data);
      p->next=NULL;
      if(start==NULL)
      	start=p;
      else
      {
         node=start;
         while(node->next!=start)
         {
      		node=node->next;
         }
         node->next=p;
      }
      p->next=start;
   }
   clrscr();
}



void display()
{
	NODE *node;
   if(start==NULL)
   	printf("List is Empty\n\n");
   else
   {
      printf("The elements are: ");
   	node=start;
      while(node->next!=start)
      {
      	printf("%d ",node->data);
         node=node->next;
      }
      printf("%d ",node->data);
   }
   getch();
   clrscr();
}

void delet()
{
	NODE *node,*p;
   int pos,i;
   if(start==NULL)
   	printf("List is Empty! Deletion can't possible!\n\n");
   else
   {
      node=start;
   	printf("Enter the position: ");
   	scanf("%d",&pos);
      if(pos==1)
      {
         if(node->next==start)
         	start=NULL;
         else
         {
            node=start;
         	while(node->next!=start)
         	{
      			node=node->next;
         	}
      	  	node->next=start->next;
            start=node->next;//
         }
      }
      else
      {
      	for(i=1;i<pos;i++)
      	{
      		p=node;
      		node=node->next;
     		}
      	p->next=node->next;
      }
   }
}


void deleteAtEnd()
{
	NODE *node;
	node=start;
   while(node->next->next!=start)
   {
   	node=node->next;
   }
   node->next=start;
	clrscr();
}


void deleteAtBeg()
{
	NODE *node,*p;
   if(start==NULL)
   	printf("List is Empty! Deletion can't possible!\n\n");
   else
   {
      node=start;
      if(node->next==start)
      	start=NULL;
      else
      {
      	node=start;
         while(node->next!=start)
      	{
      		node=node->next;
         }
      	node->next=start->next;
      	start=node->next;
      }
   }
   clrscr();
}


void insertAtBeg()
{
	NODE *p,*node;
   p=(NODE*)malloc(sizeof(NODE));
   if(p==NULL)
   	printf("NO Space!");
   else
   {
   	printf("Enter the element in node: ");
      scanf("%d",&p->data);
      p->next=NULL;
      if(start==NULL)
      {
      	start=p;
         p->next=start;
      }
      else
      {

         node=start;
         while(node->next!=start)
         {
      		node=node->next;
         }
         node->next=p;
         p->next=start;
         start=p;
      }
      start=p;
   }
   clrscr();
}


void insertAtAnyPos()
{
	NODE *p,*node;
   int pos,i;
   p=(NODE*)malloc(sizeof(NODE));
   if(p==NULL)
   	printf("NO Space!");
   else
   {
      printf("Enter the position: ");
      scanf("%d",&pos);
   	printf("Enter the element in node: ");
      scanf("%d",&p->data);
      p->next=NULL;
      node=start;
      for(i=1;i<pos-1;i++)
      	node=node->next;
      p->next=node->next;
      node->next=p;
   }
   clrscr();
} 

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *