TEAM CODING-BCA

BASIC FUNCTIONS OF CIRCULAR LINKED LIST.

BASIC FUNCTIONS OF CIRCULAR LINKED  LIST:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void add_beginning();
void add_after();
void add_end();
void display();
void del_begin();
void del_end();
void del_by_val();

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

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

void add_after()
{
struct node *q,*temp;
int pos,i;
printf("\n Enter the position after which you want to add a new node:");
scanf("%d",&pos);
q=last->next;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("\n Less number of node is present in the linked list");
return;
}
}
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&temp->data);
temp->next=q->next;
q->next=temp;
if(last==q)
{
last=temp;
}
}

void add_end()
{
struct node *q,*temp;
if(last==NULL)
{
printf("\n No node is present!!!");
return;
}
temp=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data:");
scanf("%d",&temp->data);
q=last->next;
last->next=temp;
temp->next=q;
last=temp;
}
void display()
{
struct node *q;
if(last==NULL)
{
printf("\n No node is present!!!");
return;
}

q=last->next;
if(last==NULL)
{
printf("\n No node is present in the linked list");
return;
}
if(last->next==last)
{
printf("\n the deleted item is:%d",q->data);
free(last);
last=NULL;
}
{
printf("\n%d",q->data);
q=q->next;
}while(q!=last->next);
}

void del_begin()
{
struct node *q;
if(last==NULL)
{
printf("\n No node is present in the linked list");
return;
}
if(last->next==last)
{
printf("\n the deleted item is:%d",q->data);
free(last);
last=NULL;
}
else
{
q=last->next;
last->next=q->next;
printf("\n the deleted item is:%d",q->data);
free(q);
}
}

void del_end()
{
struct node *q,*p;
p=last;
q=last->next;
if(last==NULL)
{
printf("\n No node is present in the linked list");
return;
}
while(q!=last)
{
p=p->next;
q=q->next;
}
q->next->next=p->next;
}
void main()
{
int z;
while(1)
{
printf("\n1.Add Beginning \n2.Add After\n3.Add End\n4.Dispaly\n5.delete at beginning\n6.Delete at end\n7.Delete by value\n8.Exit");
printf("\n Enter your choice:");
scanf("%d",&z);
switch(z)
{
case 1:add_beginning();
break;
case 2:add_after();
break;
case 3:add_end();
break;
case 4:display();
break;
case 5:del_begin();
break;
case 6:exit(0);
}
}
}

No comments:

Post a Comment