TEAM CODING-BCA

CIRCULAR LINK LIST

CIRCULAR  LINK LIST

#include<stdio.h>
#include<stdlib.h>
void add_begin();
void add_after();
void add_end();
void display();
void del_begin();
void del_end();
void del_value();

struct node
{
int data;
struct node *next;
}*last= NULL;

void add_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter data: ");
scanf("%d",&temp->data);
if (last==NULL)
{
last=temp;
last->next=temp;
}
else
{
temp->next=last->next;
last->next=temp;
}
}

void add_after()
{
  struct node *q,*temp;
  int pos,i;
  printf("\nEnter position of node after which u want to insert : ");
  scanf("%d",&pos);
  q=last->next;
  for (i=0;i<pos-1;i++)
  {
  q=q->next;
  if(q==NULL)
  {
  printf("\n less nodes present");
  return;
  }
  }
  temp=(struct node *)malloc(sizeof(struct node));
  printf("\nEnter data:");
  scanf("%d",&temp->data);
  if(last==q)
  {
  last=temp;
  }
  temp->next=q->next;
  q->next=temp;
}


void add_end()
{
if(last==NULL)
{
printf("\nNo Nodes Present: ");
return;
}
struct node *q,*temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data: ");
scanf("%d",&temp->data);
q=last->next;
last->next=temp;
temp->next=q;
last=temp;
}


void display()
{
if(last==NULL)
{
printf("\nNo Nodes Present!!");
return;
}
struct node *q;
q=last->next;
do
{
printf("%d  ",q->data);
q=q->next;
}
while(q!=last->next);
}


void del_begin()
{
struct node *q;
if(last==NULL)
{
printf("\nNo Nodes Present!!");
return;
}
q=last->next;
if(q==last)
{
last=NULL;
free(q);
}
else
{
last->next=q->next;
free(q);
}
}


void del_end()
{
struct node *q;
if(last==NULL)
{
printf("\nNo Nodes Present!!");
return;
}
q=last->next;
while(q->next!=last)
{
q=q->next;
}
q->next=last->next;
printf("\n%d deleted \n",last->data);
free(last);
last=q;
}


void del_value()
{
int val,flag=0;
struct node *q,*p;
if(last==NULL)
{
printf("\nNo Nodes Present!!");
return;
}
printf("\n enter data to be deleted :");
scanf("%d",&val);
q=last->next;
if(last->next->data==val)
{
flag=1;
last->next=q->next;
printf("\n deleted \n",q->data);
if(q==last)
last=NULL;
free(q);
}
else
{
p=q->next;
while(p!=last->next)
{
if(p->data==val)
{
flag=1;
q->next=p->next;
printf("\n deleted \n",p->data);
free(p);
if(p==last)
last=q;
}
else
{
q=q->next;
p=p->next;
}

}
}
if(flag==0)
printf("\n  %d not found\n",val);
}


void main()
{
int ch;
while(1)
{
printf("\nMENU: \n1.Add NODE to the Beginning. \n2.Add a node at the end. \n3.Add after.\n4.Display data present. \n5.delete begining. \n6.delete end. \n7.delete by value. \n8.Exit \n");
printf("\nEnter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add_begin();
break;
case 2:
add_end();
break;
case 3:
add_after();
break;
case 4:
display();
break;
case 5:
del_begin();
break;
case 6:
del_end();
break;
case 7:
del_value();
break;
case 8:
exit(0);

}

}
}

No comments:

Post a Comment