0% found this document useful (0 votes)
18 views40 pages

Week 2 Dslab

The document outlines C programs for implementing single and double linked lists, detailing operations such as insertion, deletion, display, and reversal. It provides source code for these operations, including a menu-driven interface for user interaction. The document is affiliated with CMR College of Engineering & Technology and includes multiple sections for different linked list functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views40 pages

Week 2 Dslab

The document outlines C programs for implementing single and double linked lists, detailing operations such as insertion, deletion, display, and reversal. It provides source code for these operations, including a menu-driven interface for user interaction. The document is affiliated with CMR College of Engineering & Technology and includes multiple sections for different linked list functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

CMR COLLEGE OF ENGINEERING & TECHNOLOGY

(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

4.AIM:C program to implement single linked list and following operations.


i. Insertion
ii. Deletion
iii. Display

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void delete_begin();
void delete_end();
void delete_random();
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
int main()
{
int opt;
while(1)
{
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\n 7.Delete at end");


printf("\n 8.Delete at random");
printf("\n 9.exit");
printf("\n choose any option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:display();break;
case 3:insert_begin();break;
case 4:insert_end();break;
case 5:insert_random();break;
case 6:delete_begin();break;
case 7:delete_end();break;
case 8:delete_random();break;
case 9:printf("terminated by you");exit(0);

}// end of switch


}// end of while
return 0;
}// end of main
void create()
{
int val;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
{
newnode->next=head;
head=newnode;
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void display()
{
struct node *temp;
temp=head;
if(head==NULL)
printf("\n List is empty");
else
{
printf("\n list is ");
while(temp!=NULL)
{
printf("-->%d",temp->data);
temp=temp->next;
}
}
}
void insert_begin()
{
int val;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
{
newnode->next=head;
head=newnode;
}
}
void insert_end()
{
int val;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("enter the value to insert");
scanf("%d",&val);

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

newnode->data=val;
newnode->next=NULL;
if(head==NULL)
head=newnode;
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
}
void insert_random()
{
int val,pos,i;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
printf("enter position to insert");
scanf("%d",&pos);
if(pos==0)
insert_begin();
else
{
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
for(i=0;i<pos-1;i++)
{
temp=temp->next;
}
if(temp==NULL)
{

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\n position is out of scope");


}
else
{
newnode->next=temp->next;
temp->next=newnode;
}
}
}
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("\n List is empty");
}
else
{
temp=head;
head=head->next;
printf("deleting the node data is %d",temp->data);
free(temp);
}
}
void delete_end()
{
struct node *temp1,*temp2;
if(head==NULL)
{
printf("\n List is empty");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

temp2->next=NULL;
printf("\n deleting node data is %d",temp1->data);
free(temp1);
}
}
void delete_random()
{
struct node *temp,*position;
int i=0,pos;
if(head==NULL)
printf("\n List is empty");
else
printf("enter the position where you want to delete");
scanf("%d",&pos);
position=malloc(sizeof(struct node));
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
position=temp->next;
temp->next=position->next;
free(position);
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website:www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE PageNo.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

5.AIM:C program to implement to reverse the nodes of a single linked list

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void reverse();
struct node
{
int data;
struct node *link;
};
struct node *head=NULL;
int main()
{
int opt;
while(1)
{
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.reverse");
printf("\n 4.exit");
printf("\n choose any option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:display();break;
case 3:reverse();break;
case 4:printf("terminated by you");exit(0);
}// end of switch
}// end of while
return 0;
}// end of main

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void create()
{
int val;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->link=NULL;
if(head==NULL)
head=newnode;
else
{
newnode->link=head;
head=newnode;
}
}
void display()
{
struct node *temp;
temp=head;
if(head==NULL)
printf("\n List is empty");
else
{
printf("\n list is ");
while(temp!=NULL)
{
printf("-->%d",temp->data);
temp=temp->link;
}
}
}
void reverse()
{
struct node *prev,*current,*next;
prev=NULL;
current=next=head;
if(head==NULL)
printf("list is empty");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

else
{
while(next!=NULL)
{
next=next->link;
current->link=prev;
prev=current;
current=next;
}
head=prev;
}
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

6.AIM:C program to implement various operations of double linked list and following operations
i. Creation
ii. Insertion
iii. Deletion
iv. Traversal
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void delete_begin();
void delete_end();
void delete_random();
struct node
{
int data;
struct node *next,*prev;
};
struct node *head=NULL;
int main()
{
int opt;
while(1)
{
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");
printf("\n 7.Delete at end");
printf("\n 8.Delete at random");
printf("\n 9.exit");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\n choose any option");


scanf("%d",&opt);
switch(opt)
{
case 1:create(); break;
case 2:display(); break;
case 3:insert_begin(); break;
case 4:insert_end(); break;
case 5:insert_random(); break;
case 6:delete_begin(); break;
case 7:delete_end(); break;
case 8:delete_random(); break;
case 9:printf("terminated by you"); exit(0);
break;

}// end of switch


}// end of while
return 0;
}// end of main
void create()
{
int n,i,val;
struct node *newnode;
printf("Enter number of nodes:");
scanf("%d",&n);
printf("Enter NODE DATA:");
for(i=0;i<n;i++)
{
scanf("%d",&val);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
{
head=newnode;
}
else
{

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

newnode->next=head;
head->prev=newnode;
head=newnode;
}
}
}
void display()
{
struct node *temp;
temp=head;
if(head==NULL)
printf("\n List is empty");
else
{
printf("\n list is ");
while(temp!=NULL)
{
printf("-->%d",temp->data);
temp=temp->next;
}
}
}
void insert_begin()
{
int val;
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=newnode;
else
{
newnode->next=head;
newnode->prev=NULL;
head->prev=newnode;
head=newnode;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

}
}
void insert_end()
{
int val;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter value to insert:");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
}
}
void insert_random()
{
int val,i,pos;
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node)) ;
printf("enter the value to insert");
scanf("%d",&val);
newnode->data=val;
newnode->next=NULL;
newnode->prev=NULL;
printf("enter position to insert");
scanf("%d",&pos)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

if(pos==NULL)
{
head=newnode;
}
else
{
temp=head;
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
if(temp==NULL)
{
printf("\n position is out of scope");
}
else
{
newnode->next=temp->next;
newnode->prev=temp;
temp->next=newnode;
}
}
}
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("LIST IS EMPTY");
}
else
{
temp=head;
head=head->next;
head->prev=NULL;
free(temp);
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void delete_end()
{
struct node *temp1,*temp2;
if(head==NULL)
{
printf("LIST IS EMPTY");
}
else
{
temp1=head;
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
temp1->prev=NULL;
temp2->next=NULL;
free(temp1);
}
}
void delete_random()
{
struct node *temp;
int pos,i=1;
printf("Enter position of node to delete:");
scanf("%d",&pos);
if(temp==head)
{
head=head->next;
head->prev=NULL;
free(temp);
}
else
{
temp=head;
while(i<pos)
{
temp=temp->next;
i++;
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

7.AIM:C Program to implement circular linked list and its operations


i. Creation
ii. Insertion
iii. Deletion
iv. Traversal
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_random();
void delete_begin();
void delete_end();
void delete_random();
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
int main()
{
int opt;
while(1)
{
printf("\n \t MENU");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.Insert at begin");
printf("\n 4.Insert at end");
printf("\n 5.Insert at random");
printf("\n 6.Delete at begin");
printf("\n 7.Delete at end");
printf("\n 8.Delete at random");
printf("\n 9.exit");

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("\n choose any option");


scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:display();break;
case 3:insert_begin();break;
case 4:insert_end();break;
case 5:insert_random();break;
case 6:delete_begin();break;
case 7:delete_end();break;
case 8:delete_random();break;
case 9:printf("terminated by you");exit(0);

}// end of switch


}// end of while
return 0;
}// end of main
void create()
{
struct node *ptr,*temp;
int i,n,val;
printf("enter no of elements\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("enter data of node %d",i);
scanf("%d",&val);
ptr->data=val;
if(head==NULL)
{
head=ptr;
ptr->next=head;
temp=head;
}
else
{
temp->next=ptr;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

temp=ptr;
temp->next=head;
}
}
}
void insert_begin()
{
struct node *temp,*ptr;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&num);
ptr->data=num;
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
temp=head;
if(temp->next==head)
{
temp->next=ptr;
ptr->next=temp;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
ptr->next=head;
head=ptr;
temp->next=head;
}
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void insert_end()
{
struct node *ptr,*temp;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&num);
ptr->data=num;
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
temp=head;
if(temp->next==head)
{
temp->next=ptr;
ptr->next=temp;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=head;
}
}
}
void insert_random()
{
struct node *ptr;
int pos,i=1,num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&num);
ptr->data=num;

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("enter position to insert:");


scanf("%d",&pos);
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
struct node *temp;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
ptr->next=temp->next;
temp->next=ptr;
}
}
void delete_begin()
{
if(head==NULL)
{
printf("list is empty");
}
else
{
struct node *temp,*temp1;
temp=head;
temp1=head;
if(temp->next==head)
{
head==NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(temp->next!=head)

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

{
temp=temp->next;
}
head=temp1->next;
temp->next=head;
printf("Deleted element is %d",temp1->data);
free(temp1);
}
}
}
void delete_end()
{
if(head==NULL)
{
printf("list is empty");
}
else
{
struct node *temp1,*temp;
temp=head;
if(temp->next==head)
{
head==NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(temp->next!=head)
{
temp1=temp;
temp=temp->next;
}
temp1->next=head;
printf("deleted element is %d",temp->data);
free(temp);
}
}
}

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

void delete_random()
{
int pos,i=1;
if(head==NULL)
{
printf("list is empty");
}
else
{
struct node *temp,*t;
temp=head;
printf("enter position to delete");
scanf("%d",&pos);
if(temp->next==NULL)
{
head=NULL;
printf("deleted element is %d",temp->data);
free(temp);
}
else
{
while(i<pos-1)
{
temp=temp->next;
i++;
}
t=temp->next;
temp->next=t->next;
t->next=NULL;
printf("deleted element is %d",t->data);
free(t);
}
}
}
void display()
{
if(head==NULL)
{

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

printf("list is empty");
}
else
{
struct node *temp;
temp=head;
printf("The linked list is:\n");
while(temp->next!=head)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
OUTPUT:

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.


CMR COLLEGE OF ENGINEERING & TECHNOLOGY
(UGC AUTONOMOUS)
*Approved by AICTE *Affiliated to JNTUH * NAAC Accredited with A+ GRADE
Kandlakoya(V), Medchal Road, Hyderabad, Telangana-501401.
Website: www.cmrcet.ac.in

Dept of CSE Page No.

You might also like