KEMBAR78
Week-5 Dsa Program | PDF | Software Engineering | Computer Programming
0% found this document useful (0 votes)
19 views5 pages

Week-5 Dsa Program

The document contains a C program that implements a doubly linked list with various functionalities including insertion and deletion at the beginning, end, and any specified position, as well as displaying the list. It defines a structure for the nodes and provides functions for creating nodes, counting nodes, and managing the list. The main function allows user interaction to perform operations on the doubly linked list through a menu-driven interface.

Uploaded by

Aravind
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)
19 views5 pages

Week-5 Dsa Program

The document contains a C program that implements a doubly linked list with various functionalities including insertion and deletion at the beginning, end, and any specified position, as well as displaying the list. It defines a structure for the nodes and provides functions for creating nodes, counting nodes, and managing the list. The main function allows user interaction to perform operations on the doubly linked list through a menu-driven interface.

Uploaded by

Aravind
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/ 5

Week-5 C Program to implement Doubly Linked List

#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *llink;
int data;
struct node *rlink;
}*head=NULL,*tail=NULL;
int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->rlink!=NULL)
{
temp=temp->rlink;
i++;
}
return(i);
}
//Creation of the doubly linked list
struct node *create(int value)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->rlink=NULL;
temp->llink=NULL;
return temp;
}
//insertion at beginning of the doubly linked list
void insert_begin(int value)
{
struct node *newnode;
newnode=create(value);
if(head==NULL)
{
head=tail=newnode;
}
else
{
newnode->rlink=head;
head->llink=newnode;
head=newnode;
}
}
//insertion at end of the doubly linked list
void insert_end(int value)
{
struct node *newnode, *temp;
newnode=create(value);
if(head==NULL)
{
head=tail=newnode;
}
else
{
newnode->llink=tail;
tail->rlink=newnode;
tail=newnode;
}
}
//insertion at any position of the doubly linked list
void insert_pos(int value,int pos)
{
struct node *newnode, *temp1,*temp2,*temp;
int i,c=1;
newnode=create(value);
i=count();
if(pos==1)
insert_begin(value);
else if(pos>i+1)
{
printf("insertion is not posible");
return;
}
else if(pos==i+1)
{
insert_end(value);
}
else
{
temp=head;
while(c<=pos-1 && temp!=NULL)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp1->rlink=newnode;
temp->llink=newnode;
newnode->llink=temp1;
newnode->rlink=temp;
}
}
//Deletion at beginning of the doubly linked list
void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->rlink;
if(head==NULL)
tail=NULL;
else
head->llink=NULL;
free(temp);
}
}
//Deletion at end of the doubly linked list
void delete_end()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=tail;
tail=tail->llink;
if(tail==NULL)
head=NULL;
else
tail->rlink=NULL;
free(temp);
}
}
//Deletion at any position of the doubly linked list
void delete_pos(int pos)
{
struct node *temp1,*temp2,*temp;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else if(pos==i)
{
delete_end();
}
else
{
temp=head;
while(c<pos && temp->rlink!=NULL)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp2=temp->rlink;
temp1->rlink=temp2;
temp2->llink=temp1;
free(temp);
}
}
//Display operation for doubly linked list
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->rlink!=NULL)
{
printf("%d <-> ",temp->data);
temp=temp->rlink;
}
printf("%d",temp->data);
}
}
//main program for Doubly linked list
void main()
{
int ch,pos,value;
do
{
printf("\n1.Insert Begining \n2.Insert End\n3.Insert any
Position\n4.Delete Begining\n5.Delete End\n6.Delete
Position\n7.Display\n8.Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the value: ");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("Enter value: ");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("Enter value: ");
scanf("%d",&value);
printf("Enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("Enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}

You might also like