0% found this document useful (0 votes)
5 views6 pages

Double Linked List program

This C program implements a doubly linked list (DLL) with various operations such as creating the list, adding and deleting nodes at the beginning, end, and middle, displaying the list, and traversing it in reverse. The program provides a menu-driven interface for users to perform these operations. It includes functions for memory management and node manipulation to ensure proper handling of the linked list structure.

Uploaded by

nknitishiosehh
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)
5 views6 pages

Double Linked List program

This C program implements a doubly linked list (DLL) with various operations such as creating the list, adding and deleting nodes at the beginning, end, and middle, displaying the list, and traversing it in reverse. The program provides a menu-driven interface for users to perform these operations. It includes functions for memory management and node manipulation to ensure proper handling of the linked list structure.

Uploaded by

nknitishiosehh
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/ 6

#include<stdio.

h>
#include<conio.h>

struct node{
int data;
struct node *prev;
struct node *next;
};
struct node
*start=NULL,*temp,*new1,*curr_node,*next_node,*prev_node,*current,*las
t;
void create();
void display();
void addFirst();
void addLast();
void addMiddle();
void deleteFirst();
void deleteLast();
void deleteMiddle();
void reverseTraverse();
//void reverse();
void main()
{
int ch;
clrscr();
do{
printf("\n\t1]. create DLL");
printf("\n\t2]. addFirst ");
printf("\n\t3]. addLast ");
printf("\n\t4]. addMiddle");
printf("\n\t5]. deleteFirst");
printf("\n\t6]. deleteLast");
printf("\n\t7]. deleteMiddle");
printf("\n\t8]. reverseTraverse");
printf("\n\t9]. display");
printf("\n\t10]. reverse");
printf("\n\t11]. exit\n");
scanf("%d",&ch);
switch(ch){
case 1: {
create();
break;
}
case 2:{
addFirst(); break;
}
case 3: addLast(); break;
case 4: addMiddle(); break;
case 5: deleteFirst(); break;
case 6: deleteLast(); break;
case 7: deleteMiddle(); break;
case 8: reverseTraverse(); break;
case 9: display(); break;
//case 10: reverse(); break;
case 11:{
exit(0);
break;
}
}
}while(ch!=11);
getch();
}
void create()
{
int n,num;
new1=(struct node *)malloc(sizeof(struct node));
printf("\nenter data part ");
scanf("%d",&n);
new1->data=n;
new1->prev=NULL;
new1->next=NULL;
if(start==NULL)
{start=new1; }
else{
temp=start;
while(temp->next!=NULL)
temp=temp->next;
new1->prev=temp;
temp->next=new1;
}
printf("\n\tdata inserted succesfully");
}
void display()
{
temp=start;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL");
}

void addFirst()
{
int num;
printf("\nEnter data part");
scanf("%d",&num);
new1=(struct node *)malloc(sizeof(struct node));
new1->data=num;
new1->next=NULL;
new1->prev=NULL;
if(start==NULL)
start=new1;
else{
new1->next=start;
start->prev=new1;
start=new1;
}}
void addLast()
{
int num;
printf("Add data part");
scanf("%d",&num);
new1=(struct node *)malloc(sizeof(struct node));
new1->data=num;
new1->next=new1->prev=NULL;
temp=start;
if(start==NULL)
start=new1;
else{
while(temp->next!=NULL)
temp=temp->next;
temp->next=new1;
new1->prev=temp;
temp=new1;
} }

void addMiddle()
{
int pos,i=1,n;
printf("Enter position you want insert ");
scanf("%d",&pos);
new1=(struct node * )malloc(sizeof(struct node));
printf("\nenter data part\t");
scanf("%d",&n);
new1->data=n;
new1->prev=new1->next=NULL;
prev_node=next_node=start;
while(i<pos)
{
next_node=next_node->next;
prev_node=next_node->prev;
i++;
}
prev_node->next=new1;
next_node->prev=new1;
printf("data inserted");

void deleteFirst()
{
if(start==NULL)
printf("List not found");
else{
temp=start;
start=start->next;
printf("Deleted Node %d",temp->data);
free(temp);
}
}
void deleteLast()
{
if(start==NULL)
printf("List not found");
else{
temp=start;
while(temp->next!=NULL){
temp=temp->next;
curr_node=temp->prev;}
printf("Deleted node %d",temp->data);
curr_node->next=NULL;
free(temp);
}
}
void deleteMiddle()
{
int pos,i;
printf("Which node you wants to delete");
scanf("%d",&pos);
temp=prev_node=start;
next_node=temp->next;
for(i=1;i<pos;i++)
{
temp=temp->next;
next_node=next_node->next;
prev_node=temp->prev;
}
printf("deleted node %d",temp->data);
prev_node->next=next_node;
next_node->prev=prev_node;
free(temp);
}
void reverseTraverse()
{
temp=start;
if(start==NULL)
printf("List not found");
else{
while(temp->next!=NULL)
temp=temp->next;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->prev;
}
printf("NULL");
}
}
/*void reverse()
{
current=start;
while(current!=NULL)
{
last=current->prev;
current->prev=current->next;
current->next=last;
current=current->prev;
}
} */

You might also like