0% found this document useful (0 votes)
102 views5 pages

Doublylink

This document defines functions for a doubly linked list data structure including insertion, deletion, and display functions. It defines a node struct with data and next/prev pointers. Functions are provided to insert nodes at the head, end, or after a specified node. Deletion functions remove nodes from the head, end, or by data value. Display functions print the list forwards or backwards. The main function provides a menu to test the linked list functions.

Uploaded by

h hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views5 pages

Doublylink

This document defines functions for a doubly linked list data structure including insertion, deletion, and display functions. It defines a node struct with data and next/prev pointers. Functions are provided to insert nodes at the head, end, or after a specified node. Deletion functions remove nodes from the head, end, or by data value. Display functions print the list forwards or backwards. The main function provides a menu to test the linked list functions.

Uploaded by

h hehe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//doubly linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head=NULL;
struct node *end=NULL;

void insert()
{
int ch;
printf("Enter choice to insert at\n");
printf("1.Head\t2.End\t3.After Specific Node\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
insertHead();
break;
case 2:
insertEnd();
break;
case 3:
insertNode();
break;
default:
return;
}
}
void insertEnd()
{
struct node *newNode;
newNode=(struct node *)malloc(sizeof(struct node));
printf("Enter data ");
scanf("%d",&newNode->data);
newNode->next=NULL;
newNode->prev=end;
if(head==NULL)
{
head=newNode;
end=newNode;
}
else
{
end->next=newNode;
end=newNode;
}
}
void insertHead()
{
struct node *newNode;
newNode=(struct node *)malloc(sizeof(struct node));
printf("Enter data ");
scanf("%d",&newNode->data);
newNode->next=head;
newNode->prev=NULL;
if(head==NULL)
{
head=newNode;
end=newNode;
}
else
{
head->prev=newNode;
head=newNode;
}
}
void insertNode()
{
int data;
struct node *newNode, *tmp;
newNode=(struct node *)malloc(sizeof(struct node));
printf("Enter data ");
scanf("%d",&newNode->data);
printf("Enter data to insert after\t");
scanf("%d",&data);
tmp=head;
while(tmp!=NULL&&tmp->data!=data)
tmp=tmp->next;
newNode->next=tmp->next;
tmp->next->prev=newNode;
tmp->next=newNode;
newNode->prev=tmp;
}

void delete()
{
if(head==NULL)
{
printf("List Empty\n");
return;
}
int ch;
printf("Enter choice to delete\n");
printf("1.Head\t2.End\t3.Specific Node\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
deleteHead();
break;
case 2:
deleteEnd();
break;
case 3:
deleteNode();
break;
default:
return;
}
}
void deleteHead()
{
struct node *tmp;
tmp=head;
if(tmp->next==NULL)
{
head=NULL;
end=NULL;
}
else
{
head=head->next;
head->prev=NULL;
tmp->next=NULL;
}
free(tmp);
}
void deleteEnd()
{
struct node *tmp;
tmp=end;
if(tmp->prev==NULL)
{
head=NULL;
end=NULL;
}
else
{
end=end->prev;
end->next=NULL;
tmp->prev=NULL;
}
free(tmp);
}
void deleteNode()
{
int data;
printf("Enter data to delete");
scanf("%d",&data);
struct node *tmp;
tmp=head;
while(tmp!=NULL&&tmp->data!=data)
tmp=tmp->next;
if(tmp->next==NULL&&tmp->prev==NULL)
{
head=NULL;
end=NULL;
}
else if(tmp->next==NULL)
{
deleteEnd();
}
else if(tmp->prev==NULL)
{
deleteHead();
}
else
{
tmp->next->prev=tmp->prev;
tmp->prev->next=tmp->next;
}
free(tmp);
}

void displayListForw()
{
struct node *tmp;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
tmp=head;
while(tmp!=NULL)
{
printf("%d -> ",tmp->data);
tmp=tmp->next;
}
printf("\b\b\b");
printf(" \n");
}
}
void displayListBack()
{
struct node *tmp;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
tmp=end;
while(tmp!=NULL)
{
printf("%d -> ",tmp->data);
tmp=tmp->prev;
}
printf("\b\b\b");
printf(" \n");
}
}

void main()
{
int n;
while(1)
{
printf("1.Insert\t2.Delete\t3.Display\t4.Exit\n");
scanf("%d",&n);
switch(n)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
printf("Displaying nodes forwards\n");
displayListForw();
printf("Displaying nodes backwards\n");
displayListBack();
break;
case 4:
exit(0);
break;
}

}
}

You might also like