Code for Doubly Linked List C-programing
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
struct node *prev;
};
struct node *head;
void InsertAtBeginning(int x)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->next = NULL;
ptr->prev = NULL;
ptr->data = x;
if(ptr==NULL)
{
printf("\n MEMORY NOT AVAILABLE");
}
if(head==NULL)
{
head = ptr;
}
else
{
ptr->next = head;
head->prev=ptr;
head = ptr;
}
}
void InsertAtEnd(int y)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp;
ptr->data = y;
ptr->next = NULL;
ptr->prev = NULL;
if(ptr==NULL)
{
printf("\n MEMORY NOT AVAILABLE");
}
if(head==NULL)
{
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr->prev = temp;
ptr->next = NULL;
}
}
void InsertAtN(int z,int index)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
struct node *temp;
int i = 0;
if(ptr==NULL)
{
printf("\n MEMORY UNAVAILABLE");
}
else
{
printf("\n ENTER INDEX VALUE and INDEX:- ");
scanf("%d %d",&z,&index);
temp = head;
for(i=0 ; i<(index-2) ; i++)
{
temp = temp->next;
if(temp==NULL)
{
printf("\n CANNOT INSERT");
}
}
ptr->data = z;
ptr->next = temp->next;
ptr->prev = temp;
temp->next = ptr;
temp->next->prev = ptr;
printf("\n NODE INSERTED");
}
}
void DeleteFromBeginning()
{
struct node *ptr;
if(head==NULL)
{
printf("\n QUEUE UNDERFLOW");
}
else if(head->next==NULL)
{
head = NULL;
free(head);
printf("\n NODE DELETED");
}
else
{
ptr = head;
head = head->next;
head->prev = NULL;
free(ptr);
printf("\n NODE DELETED");
}
}
void DeleteFromEnd()
{
struct node *ptr,*temp;
if(head==NULL)
{
printf("\n QUEUE UNDERFLOW");
}
else if(head->next==NULL)
{
head = NULL;
free(head);
printf("\n NODE DELETED");
}
else
{
ptr = head;
while(ptr->next!=NULL)
{
ptr = ptr->next;
}
temp = ptr->prev;
temp->next = NULL;
ptr->prev = NULL;
free(ptr);
printf("\n NODE DELETED");
}
}
void DeleteFromN(int indexx)
{
struct node *temp1 = head,*temp2;
int i = 0;
if(head==NULL)
{
printf("\n LIST IS EMPTY");
}
else
{
printf("\n ENTER INDEX FOR DELETING A NODE :- ");
scanf("%d",&indexx);
if(indexx==1)
{
head = temp1->next;
free(temp1);
}
for(i=0 ; i<(indexx-2) ; i++)
{
temp1=temp1->next;
}
temp2=temp1->next;
temp1->next=temp2->next;
free(temp2);
printf("\n SUCCESSFULLY DELETED A NODE OF YOUR CHOICE");
}
}
void forward_printlist()
{
struct node *temp = head;
if(head==NULL)
{
printf("\n LINKED LIST IS EMPTY");
}
else
{
printf("\n LINKED LIST CONTAINS :- \n");
do{
printf(" | %u | %d | %u | -> ",temp->prev,temp->data,temp->next);
temp=temp->next;
}while(temp!=NULL);
}
printf("\n");
getch();
}
void reverse_printlist()
{
struct node *temp1 = head;
if(head==NULL)
{
printf("\n LINKED LIST IS EMPTY");
}
else
{
printf("\n LINKED LIST CONTAINS :- \n");
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
while(temp1!=NULL)
{
printf("| %u | %d | %u | ->",temp1->next,temp1->data,temp1->prev);
temp1=temp1->prev;
}
printf("\n");
}
getch();
}
void main()
{
int x,y,z,index,indexx,i,m;
clrscr();
printf("\n MENU");
printf("\n 1) INSERT AT BEGINNING");
printf("\n 2) INSERT AT END");
printf("\n 3) INSERT AT SPECIFIC INDEX");
printf("\n 4) DELETE FROM BEGINNING");
printf("\n 5) DELETE FROM END");
printf("\n 6) DELETE FROM SPECIFIC INDEX");
printf("\n 7) FORWARD PRINTING");
printf("\n 8) REVERSE PRINTING");
printf("\n 9) EXIT");
while(i!=0)
{
printf("\n\n ENTER YOUR CHOICE :- ");
scanf("%d",&m);
switch(m)
{
case 1:
printf("\n ENTER VALUE :- ");
scanf("%d",&x);
InsertAtBeginning(x);
forward_printlist();
break;
case 2:
printf("\n ENTER VALUE :- ");
scanf("%d",&y);
InsertAtEnd(y);
forward_printlist();
break;
case 3:
InsertAtN(m,indexx);
forward_printlist();
break;
case 4:
DeleteFromBeginning();
forward_printlist();
break;
case 5:
DeleteFromEnd();
forward_printlist();
break;
case 6:
DeleteFromN(indexx);
forward_printlist();
break;
case 7:
forward_printlist();
break;
case 8:
reverse_printlist();
break;
case 9:
exit(0);
}
getch();
}
}
Comments
Post a Comment