Assignment 7 PPS LAB Yash Tiwari

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 61

Name : Yash

Tiwari
Sec : A
Roll No. : 91
Subject : PPS-II
Topic : ASSIGNMENT
7

1|Page
ASSIGNMENT-7
1.Write a C program to create and display a singly linked
list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void create(int n);
void display();
int main()
{
int n;
printf("\n Linked list: To create and display singly linked list: \n");
printf("\n Input the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Data entered in the list: \n");
display();
return 0;
}

2|Page
void create(int n)
{
struct node *fnnode,*tmp;
int num,i;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL)
{
printf("\n Memory cannot be allocated.");
}
else
{
printf("\n Input data for node 1: ");
scanf("%d",&num);
stnode->num=num;
stnode->nextptr=NULL;
tmp=stnode;
for(i=2;i<=n;i++)
{
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL)
printf("\n Memory cannot be allocated.");
else
{
printf("\n Input data for node %d: ",i);
scanf("%d",&num);
fnnode->num=num;
fnnode->nextptr=NULL;
tmp->nextptr=fnnode;
tmp=tmp->nextptr;

3|Page
}
}
}
}
void display()
{
struct node *tmp;
if(stnode==NULL)
printf("\n List is empty.");
else
{
tmp=stnode;
while(tmp!=NULL)
{
printf("\n Data= %d",tmp->num);
tmp=tmp->nextptr;
}
}
}

Output:

4|Page
2.Write a program in C to insert a node at beginning of a
Singly linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *nextptr;
}*stnode;
void create(int n);
void newnodebeg(int data);
void display();
int main()
{
int n,data;
printf("\n Linked list: To create and display and insert a node at the beginning of
singly linked list: \n");
printf("\n Input the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Enter data to insert at the beginning of the list: ");
scanf("%d",&data);
newnodebeg(data);
printf("\n Data entered in the list: \n");
display();

5|Page
return 0;
}
void create(int n)
{
struct node *fnnode,*tmp;
int num,i;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL)
{
printf("\n Memory cannot be allocated.");
}
else
{
printf("\n Input data for node 1: ");
scanf("%d",&num);
stnode->num=num;
stnode->nextptr=NULL;
tmp=stnode;
for(i=2;i<=n;i++)
{
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL)
printf("\n Memory cannot be allocated.");
else
{
printf("\n Input data for node %d: ",i);
scanf("%d",&num);
fnnode->num=num;
fnnode->nextptr=NULL;

6|Page
tmp->nextptr=fnnode;
tmp=tmp->nextptr;
}
}
}
}
void newnodebeg(int data)
{
struct node *newNode;
newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
printf("\n Unable to allocate memory.");
else
{
newNode->num=data;
newNode->nextptr=stnode;
stnode=newNode;
printf("\n Data inserted successfully.");
}
}
void display()
{
struct node *tmp;
if(stnode==NULL)
printf("\n List is empty.");
else
{
tmp=stnode;
while(tmp!=NULL)

7|Page
{
printf("\n Data= %d",tmp->num);
tmp=tmp->nextptr;
}
}
}

Output:

8|Page
3. Write a program in C to insert a node at the end of a
Singly Linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void insertend(int data);
void display();
int main()
{
int n,data;
printf("\n Linked list: To create and display and insert a node at the end of singly
linked list: \n");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Enter data to insert at the end: ");
scanf("%d",&data);
insertend(data);
printf("\n Data in the list: \n");
display();

9|Page
return 0;
}
void create(int n)
{
struct node *newnode,*tmp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\n Unable to allocate memory.");
}
else
{
printf("\n Enter the data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
tmp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter the data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;

10 | P a g e
tmp->next=newnode;
tmp=tmp->next;
}
}
}
}
void insertend(int data)
{
struct node *newnode,*tmp;
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memeory.");
else
{
newnode->data=data;
newnode->next=NULL;
tmp=head;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=newnode;
printf("\n Data inserted successfully.");
}
}
void display()
{
struct node *tmp;
if(head==NULL)
printf("\n List is empty.");
else

11 | P a g e
{
tmp=head;
while(tmp!=NULL)
{
printf("\n Data= %d\n",tmp->data);
tmp=tmp->next;
}
}
}

Output:

12 | P a g e
4. Write a program in C to insert a node at the middle of a
Singly Linked List.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void insertmid(int data,int pos);
void display();
int main()
{
printf("\n Linked List:To create and display and insert a node at the middle of a
Linked list.");
int n,data,pos;
printf("\n\n Enter the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Enter the data to insert at the middle of the list: ");
scanf("%d",&data);
printf("\n Enter the position to insert a new node: ");
scanf("%d",&pos);
insertmid(data,pos);

13 | P a g e
printf("\n Data in the list: \n");
display();
}
void create(int n)
{
struct node *newnode,*temp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
printf("\n Unable to allocate memeory.");
else
{
printf("\n Enter the data of node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memeory.");
else
{
printf("\n Enter the data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;

14 | P a g e
temp=temp->next;
}
}
printf("\n Linked list created successfully.");
}
}
void insertmid(int data,int pos)
{
int i;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memory.");
else
{
newnode->data=data;
newnode->next=NULL;
temp=head;
for(i=2;i<=pos-1;i++)
{
temp=temp->next;
if(temp==NULL)
break;
}
if(temp!=NULL)
{
newnode->next=temp->next;
temp->next=newnode;
printf("\n Data inserted successfully.");

15 | P a g e
}
else
printf("\n Unable to allocate memory.");
}
}
void display()
{
struct node *temp;
if(head==NULL)
printf("\n List is empty.");
else
{
temp=head;
while(temp!=NULL)
{
printf("\n Data= %d\n",temp->data);
temp=temp->next;
}
}
}

Output:

16 | P a g e
5. Write a program in C to delete a node from the beginning
of a singly linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void deletenode();
void display();
int main()
{
int n,ch;
printf("\n Enter the number of nodes: ");
scanf("%d",&n);

17 | P a g e
create(n);
printf("\n\n Data in the list: \n");
display();
printf("\n Press 1 to delete the first node: ");
scanf("%d",&ch);
if(ch==1)
deletenode();
printf("\n\n Final data in the list: \n");
display();
return 0;
}
void create(int n)
{
struct node *newnode,*temp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter the data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)

18 | P a g e
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
printf("\n Singly Linked list created successfully.");
}
}
void deletenode()
{
struct node *del;
if(head==NULL)
printf("\n List is already empty.");
else
{
del=head;
head=head->next;
printf("\n Data deleted= %d\n",del->data);
free(del);
printf("\n Successfully deleted first node from the list.");
}
}
void display()

19 | P a g e
{
struct node *temp;
if(head==NULL)
printf("\n List is empty.");
else
{
temp=head;
while(temp!=NULL)
{
printf("\n Data= %d\n",temp->data);
temp=temp->next;
}
}
}

Output:

20 | P a g e
6. Write a program in C to delete a node from the end of a
singly linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void deletenode();
void display();
int main()
{
int n,ch;
printf("\n Enter the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n\n Data in the list: \n");
display();
printf("\n Press 1 to delete the last node: ");
scanf("%d",&ch);
if(ch==1)
deletenode();
printf("\n\n Final data in the list: \n");
display();
return 0;
}

21 | P a g e
void create(int n)
{
struct node *newnode,*temp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter the data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}

22 | P a g e
printf("\n Singly Linked list created successfully.");
}
}
void deletenode()
{
struct node *del,*seclast;
if(head==NULL)
printf("\n List is already empty.");
else
{
del=head;
seclast=head;
while(del->next!=NULL)
{
seclast=del;
del=del->next;
}
if(del==head)
head=NULL;
else
seclast->next=NULL;
free(del);
printf("\n Successfully Deleted last node of list.\n");
}
}
void display()
{
struct node *temp;
if(head==NULL)

23 | P a g e
printf("\n List is empty.");
else
{
temp=head;
while(temp!=NULL)
{
printf("\n Data= %d\n",temp->data);
temp=temp->next;
}
}
}

Output:

7. Write a program in C to delete a node from the middle of


a singly linked list.
Solution:

24 | P a g e
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void delmid(int pos);
void display();
int main()
{
int n,pos;
printf("\n Enter the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Data in the list: \n");
display();
printf("\n Enter the node position you want to delete: ");
scanf("%d",&pos);
delmid(pos);
printf("\n Final data in the list: \n");
display();
return 0;
}
void create(int n)
{
struct node *newnode,*tmp;
int data,i;

25 | P a g e
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\n Unable to allocate memory.");
}
else
{
printf("\n Enter data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
tmp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("\n Unable to allocate memory.");
break;
}
else
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
tmp->next=newnode;
tmp=tmp->next;
}

26 | P a g e
}
printf("\n Linked List created successfully.");
}
}
void delmid(int pos)
{
int i;
struct node *del,*prevnode;
if(head==NULL)
{
printf("\n List is already empty.");
}
else
{
del=head;
prevnode=head;
for(i=2;i<=pos;i++)
{
prevnode=del;
del=del->next;
if(del==NULL)
break;
}
if(del!=NULL)
{
if(del==head)
head=head->next;
prevnode->next=del->next;
del->next=NULL;

27 | P a g e
free(del);
printf("\n Successfully deleted node from middle of the list.");
}
else
{
printf("\n Invalid position unable to delete.");
}
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("\n List is empty.");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("\n Data= %d\n",temp->data);
temp=temp->next;
}
}
}

Output:

28 | P a g e
8. Write a program in C to create and display a doubly
linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
}*head,*last;
void create(int n);
void displayfirst();
void displaylast();
int main()

29 | P a g e
{
int n,ch;
head=NULL;
last=NULL;
printf("\n Creation of Double Linked List: ");
printf("\n Enter the number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Press 1 to display list from first.");
printf("\n Press 2 to display list from last.");
printf("\n Enter your choice: ");
scanf("%d",&ch);
if(ch==1)
displayfirst();
else
displaylast();
return 0;
}
void create(int n)
{
int i,data;
struct node *newnode;
if(n>=1)
{
head=(struct node*)malloc(sizeof(struct node));
if(head!=NULL)
{
printf("\n Enter data for node 1: ");
scanf("%d",&data);

30 | P a g e
head->data=data;
head->prev=NULL;
head->next=NULL;
last=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode!=NULL)
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->prev=last;
newnode->next=NULL;
last->next=newnode;
last=newnode;
}
else
{
printf("\n Unable to allocate memory.");
break;
}
}
printf("\n Double linked list created successfully.");
}
else
printf("\n Unable to alloacte memory.");
}
}

31 | P a g e
void displayfirst()
{
struct node *temp;
int n=1;
if(head==NULL)
printf("\n List is empty.");
else
{
temp=head;
printf("\n Data in the list: ");
while(temp!=NULL)
{
printf("\n Data of node %d: %d\n",n,temp->data);
n++;
temp=temp->next;
}
}
}
void displaylast()
{
struct node *tmp;
int n=0;
if(last==NULL)
printf("\n List is empty.");
else
{
tmp=last;
printf("\n Data in the list: ");
while(tmp!=NULL)

32 | P a g e
{
printf("\n Data of last node %d: %d",n,tmp->data);
n++;
tmp=tmp->prev;
}
}
}

Output:

9. Write a program in C to insert a node in the middle of a


Doubly linked-list.

33 | P a g e
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
}*head,*last;
void create(int n);
void display();
void insertmid(int data,int pos,int n);
int main()
{
int n,data,pos;
printf("\n Creating and insertion at the middle of a doubly linked list..");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Enter the data to be inserted: ");
scanf("%d",&data);
printf("\n Enter the position to be inserted: ");
scanf("%d",&pos);
insertmid(data,pos,n);
printf("\n Final data in the doubly linked list: \n");
display();
}
void create(int n)
{

34 | P a g e
int i,data;
struct node *newnode;
if(n>=1)
{
head=(struct node*)malloc(sizeof(struct node));
if(head!=NULL)
{
printf("\n Enter data for node 1: ");
scanf("%d",&data);
head->data=data;
head->prev=NULL;
head->next=NULL;
last=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode!=NULL)
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->prev=last;
newnode->next=NULL;
last->next=newnode;
last=newnode;
}
else
{
printf("\n Unable to allocate memory.");

35 | P a g e
break;
}
}
printf("\n Doubly linked list created successfully.");
}
else
{
printf("\n Unable to allocate memory.");
}
}
}
void display()
{
struct node *temp;
int n=1;
if(head==NULL)
{
printf("\n List is empty.");
}
else
{
temp=head;
printf("\n Data in the list: \n");
while(temp!=NULL)
{
printf("\n Data of node %d: %d",n,temp->data);
n++;
temp=temp->next;
}

36 | P a g e
}
}
void insertmid(int data,int pos,int n)
{
int i;
struct node *newnode,*tmp;
if(head==NULL)
printf("\n List is alraedy empty.");
else
{
if(pos>1&&pos<n)
{
tmp=head;
i=1;
while(i<pos-1 && tmp!=NULL)
{
tmp=tmp->next;
i++;
}
if(tmp!=NULL)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=tmp->next;
newnode->prev=tmp;
if(tmp->next!=NULL)
{
tmp->next->prev=newnode;
}

37 | P a g e
tmp->next=newnode;
}
else
{
printf("\n Invalid position");
}
}
else
printf("\n Invalid position.");
}
}

Output:

10. Write a program in C to delete a node from the tail of a


Doubly linked-list.
38 | P a g e
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
}*head,*last;
void create(int n);
void display();
void delend();
int main()
{
int n,data;
head=NULL;
last=NULL;
printf("\n DOUBLE LINKED LIST: ");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Data in the list: \n");
display();
delend();
printf("\n Final data in the list: \n");
display();
return 0;
}
void create(int n)

39 | P a g e
{
int i,data;
struct node *newnode;
if(n>=1)
{
head=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data for node 1: ");
scanf("%d",&data);
head->data=data;
head->prev=NULL;
head->next=NULL;
last=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->prev=last;
newnode->next=NULL;
last->next=newnode;
last=newnode;
}
}
}
void display()
{
struct node *tmp;
int n=1;

40 | P a g e
if(head==NULL)
{
printf("\n List is empty.");
}
else
{
tmp=head;
while(tmp!=NULL)
{
printf("\n Data of node %d: %d",n,tmp->data);
n++;
tmp=tmp->next;
}
}
}
void delend()
{
struct node *del;
if(last==NULL)
{
printf("\n Unable to delete.");
}
else
{
del=last;
last=last->prev;
if(last!=NULL)
last->next=NULL;
free(del);

41 | P a g e
printf("\n\n Successfully deleted the last node.\n");
}
}

Output:

11.Write a program in c to reverse a singly linked list.


Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void display();
void reverse();
int main()
{
int n;
printf("\n SINGLY LINKED LIST: ");

42 | P a g e
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Data in the list: \n");
display();
printf("\n Reversed linked list: \n");
reverse();
display();
return 0;
}
void create(int n)
{
struct node *newnode,*tmp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\n Unable to allocate memory.");
}
else
{
printf("\n Enter data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
tmp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));

43 | P a g e
if(newnode==NULL)
{
printf("\n Unable to allocate memory.");
break;
}
else
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
tmp->next=newnode;
tmp=tmp->next;
}
}
printf("\n Singly Linked List created successfully.");
}
}
void reverse()
{
struct node *prev,*cur;
if(head!=NULL)
{
prev=head;
cur=head->next;
head=head->next;
prev->next=NULL;
while(head!=NULL)
{

44 | P a g e
head=head->next;
cur->next=prev;
prev=cur;
cur=head;
}
head=prev;
}
}
void display()
{
struct node *tmp;
if(head==NULL)
{
printf("\n List is empty.");
}
else
{
tmp=head;
while(tmp!=NULL)
{
printf("\n Data : %d",tmp->data);
tmp=tmp->next;
}
}
}

Output:

45 | P a g e
12.Write a program in C to search an element in a singly
linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void create(int n);
void display();
int search(int m);

46 | P a g e
int main()
{
int n,m,index;
printf("\n Searching an element in a singly linked list.");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(n);
printf("\n Data in the list: \n");
display();
printf("\n Enter an element to search: ");
scanf("%d",&m);
index=search(m);
if(index>=0)
printf("%d found in the list at position %d \n",m,index+1);
else
printf("\n The data is not found in the list.");
return 0;
}
void create(int n)
{
struct node *newnode,*tmp;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\n Unable to allocate memory.");
}
else
{

47 | P a g e
printf("\n Enter data for node 1: ");
scanf("%d",&data);
head->data=data;
head->next=NULL;
tmp=head;
for(i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
printf("\n Unable to allocate memory.");
else
{
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
tmp->next=newnode;
tmp=tmp->next;
}
}
}
}
void display()
{
struct node *tmp;
if(head==NULL)
{
printf("\n List is empty.");
}

48 | P a g e
else
{
tmp=head;
while(tmp!=NULL)
{
printf("\n Data: %d",tmp->data);
tmp=tmp->next;
}
}
}
int search(int m)
{
int index;
struct node *cur;
index=0;
cur=head;
while(cur!=NULL && cur->data!=m)
{
index++;
cur=cur->next;
}
return (cur!=NULL)?index:-1;
}

Output:

49 | P a g e
13.Write a program in C to delete an element from a
circular linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void create(struct node **head,int n);
void del(struct node **head,int k);
void display(struct node *head);
int main()
{
int n,k,data;
struct node *head=NULL;

50 | P a g e
printf("\n CIRCULAR LINKED LIST: ");
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(&head,n);
printf("\n Data in the list: ");
display(head);
printf("\n Enter the element to delete: ");
scanf("%d",&k);
del(&head,k);
printf("\n Final data in the list: ");
display(head);
return 0;
}
void create(struct node **head,int n)
{
int i,data;
struct node *prev,*newnode;
prev=NULL;
for(i=1;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
if(prev!=NULL)
prev->next=newnode;
if(*head==NULL)
*head=newnode;

51 | P a g e
prev=newnode;
}
prev->next=*head;
printf("\n Circular linked list created successfully.");
}
void del(struct node **head,int k)
{
int i,count;
struct node *prev,*cur;
if(*head==NULL)
{
printf("\n List is empty.");
return;
}
count =0;
cur=*head;
prev=cur;
while(prev->next!=*head)
{
prev=prev->next;
count++;
}
i=0;
while(i<=count)
{
if(cur->data==k)
{
if(cur->next!=cur)
prev->next=cur->next;

52 | P a g e
else
prev->next=NULL;
if(cur==*head)
*head=prev->next;
free(cur);
if(prev!=NULL)
cur=prev->next;
else
cur=NULL;
}
else
{
prev=cur;
cur=cur->next;
}
i++;
}
}
void display(struct node *head)
{
struct node *cur;
int n=1;
if(head==NULL)
{
printf("\n List is empty.");
return;
}
cur=head;
do

53 | P a g e
{
printf("\n Data of node %d: %d",n++,cur->data);
cur=cur->next;
}while(cur!=head);
}

Output:

14.Write a C program to reverse a circular linked list.


Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;

54 | P a g e
};
void create(struct node **head,int n);
void display(struct node *head);
void reverse(struct node **head);
int main()
{
int n;
struct node *head=NULL;
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(&head,n);
printf("\n Original list: \n");
display(head);
printf("\n Reversed list: \n");
reverse(&head);
display(head);
return 0;
}
void reverse(struct node **head)
{
struct node *prev,*cur,*next,*last;
if(*head==NULL)
{
printf("\n Cannot reverse empty list.");
return;
}
last=*head;
prev=*head;
cur=(*head)->next;

55 | P a g e
*head=(*head)->next;
while(*head!=last)
{
*head=(*head)->next;
cur->next=prev;
prev=cur;
cur=*head;
}
cur->next=prev;
*head=prev;
}
void create(struct node **head,int n)
{
int i,data;
struct node *prev,*newnode;
prev=NULL;
newnode=NULL;
for(i=1;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
if(prev!=NULL)
prev->next=newnode;
prev=newnode;
if(*head==NULL)
*head=newnode;

56 | P a g e
}
prev->next=*head;
printf("\n Circular linked list created successfully.");
}
void display(struct node *head)
{
struct node *cur;
int n=1;
if(head==NULL)
{
printf("\n List is empty.");
return;
}
cur=head;
do
{
printf("\n Data of node %d: %d",n++,cur->data);
cur=cur->next;
}while(cur!=head);
}

Output:

57 | P a g e
15. Write a program in C to search an element in a circular
linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void create(struct node **head,int n);
void display(struct node *head);
int search(struct node *head,int k);
int main()
{
int n,index,k;
struct node *head=NULL;

58 | P a g e
printf("\n Enter the total number of nodes: ");
scanf("%d",&n);
create(&head,n);
printf("\n Data in the list: \n");
display(head);
printf("\n Enter an element to search: ");
scanf("%d",&k);
index=search(head,k);
if(index==-1)
printf("\n Data is not found in the list.");
else
printf("\n %d found at %d position.",k,index+1);
return 0;
}
int search(struct node *head,int k)
{
int index=0;
struct node *cur=head;
do
{
if(cur==NULL)
printf("\n List is empty.");
if(cur->data==k)
return index;
cur=cur->next;
index++;
}while(cur!=head);
return -1;
}

59 | P a g e
void create(struct node **head,int n)
{
int i,data;
struct node *prev,*newnode;
prev=NULL;
newnode=NULL;
for(i=1;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data for node %d: ",i);
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
if(prev!=NULL)
prev->next=newnode;
prev=newnode;
if(*head==NULL)
*head=newnode;
}
prev->next=*head;
printf("\n Circular linked list created successfully.");
}
void display(struct node *head)
{
struct node *cur;
int n=1;
if(head==NULL)
{
printf("\n List is empty.");

60 | P a g e
return;
}
cur=head;
do
{
printf("\n Data of node %d: %d",n++,cur->data);
cur=cur->next;
}while(cur!=head);
}

Output:

61 | P a g e

You might also like