0% found this document useful (0 votes)
2 views

doubly linked list

The document contains a C program that implements a doubly linked list with functionalities for insertion, deletion, traversal, and searching of elements. It provides a menu-driven interface for users to perform operations at the beginning, a specific position, or the end of the list. The program includes functions for both forward and reverse traversal of the list, as well as error handling for empty lists and out-of-bounds positions.

Uploaded by

merlinjr.csb2226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

doubly linked list

The document contains a C program that implements a doubly linked list with functionalities for insertion, deletion, traversal, and searching of elements. It provides a menu-driven interface for users to perform operations at the beginning, a specific position, or the end of the list. The program includes functions for both forward and reverse traversal of the list, as well as error handling for empty lists and out-of-bounds positions.

Uploaded by

merlinjr.csb2226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*

Coded By:
Fahim Shafeek
CSE-A,60

DOUBLY LINKED LIST


*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node* prev;
struct node* next;
};
struct node *head=NULL,*temp,*loc,*p;
void insertbeg()
{
p=(struct node*)malloc(sizeof(struct node));
printf("Enter the element: ");
scanf("%d",&p->data);
if (head!=NULL)
{
p->next=head;
p->prev=NULL;
head=p;
}
else
{
head=p;
p->next=NULL;
p->prev=NULL;
}
}
void insertpos()
{
p=(struct node*)malloc(sizeof(struct node));
int pos;
if(head==NULL)
{
insertbeg();
}
else
{
printf("Enter the element: ");
scanf("%d",&p->data);
printf("Enter the position: ");
scanf("%d",&pos);
temp=head;
for(int i=0;(i<pos-1)&&(temp!=NULL);i++,temp=temp->next);
p->next=temp->next;
p->prev=temp;
if(temp->next!=NULL)
{
temp->next->prev=p;
}
temp->next=p;
}
}
void insertend()
{
p=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
insertbeg();
}
else
{
printf("Enter the element: ");
scanf("%d",&p->data);
for(temp=head;temp->next!=NULL;temp=temp->next);
temp->next=p;
p->prev=temp;
p->next=NULL;
}
}
void deletebeg()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
temp=head;
head=head->next;
if(head!=NULL)
{
head->prev=NULL;
}
free(temp);
}
}
void deletepos()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
int pos;
printf("Enter the position: ");
scanf("%d",&pos);
temp=head;
for(int i=1;i<pos-1;i++,temp=temp->next)
{
if(temp==NULL)
{
printf("Position out of bounds!");
break;
}
}
if(temp->prev!=NULL)
{
temp->prev->next=temp->next;
}
if(temp->next!=NULL)
{
temp->next->prev=temp->prev;
}
free(temp);
}

}
void deleteend()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
for(temp=head;temp->next!=NULL;temp=temp->next);
if(temp->prev==NULL)
{
head=NULL;
}
else
{
temp->prev->next=NULL;
}
free(temp);
}
}
void forwardtraverse()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("%d",temp->data);
if(temp->next!=NULL)
{
printf("<->");
}
}
printf("\n");
}
}
void reversetraverse()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
for(temp=head;temp->next!=NULL;temp=temp->next);
for(temp=temp;temp!=NULL;temp=temp->prev)
{
printf("%d",temp->data);
if(temp->prev!=NULL)
{
printf("<->");
}
}
printf("\n");
}
}
void search()
{
if(head==NULL)
{
printf("List is Empty!");
}
else
{
int ele;
printf("Enter the element to be searched: ");
scanf("%d",&ele);
for(temp=head;temp!=NULL;temp=temp->next)
{
if(temp->data==ele)
{
printf("Element found\n");
}
break;
}
if(temp==NULL)
{
printf("Element not found!\n");
}
}
}

void main()
{
int ch1,ch2;
while(1)
{
printf("1) Insertion\n2) Deletion\n3) Traversal\n4) Search\n5) Exit\nEnter Choice: ");
scanf("%d",&ch1);
switch(ch1)
{
case 1: printf("1) Beginning\n2) At Postion\n3) End\n4) Go Back\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1: insertbeg();
break;
case 2: insertpos();
break;
case 3: insertend();
break;
case 4: break;
}
break;
case 2: printf("1) Beginning\n2) At Postion\n3) End\n4) Go Back\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1: deletebeg();
break;
case 2: deletepos();
break;
case 3: deleteend();
break;
case 4: break;
}
break;
case 3: printf("1) Forward\n2) Reverse\n3) Go Back\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1: forwardtraverse();
break;
case 2: reversetraverse();
break;
case 3: break;
}
break;
case 4: search();
break;
case 5: exit(0);
default: printf("Invalid Choice! Try Again!");
break;
}
}
}

You might also like