0% found this document useful (0 votes)
6 views3 pages

DLL Operations in C Programming

The document contains a C program that implements a doubly linked list with functionalities such as creating a list, inserting nodes at the beginning, end, or a specific location, deleting a node, and traversing the list. It includes a menu-driven interface for user interaction to perform these operations. The program utilizes dynamic memory allocation for node creation and manipulation.

Uploaded by

Ravan Kumar
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)
6 views3 pages

DLL Operations in C Programming

The document contains a C program that implements a doubly linked list with functionalities such as creating a list, inserting nodes at the beginning, end, or a specific location, deleting a node, and traversing the list. It includes a menu-driven interface for user interaction to perform these operations. The program utilizes dynamic memory allocation for node creation and manipulation.

Uploaded by

Ravan Kumar
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

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* create(struct node *p,int element)
{
if(p==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=element;
p->next=NULL;
p->prev=NULL;
}
else
p->next= create(p->next,element);
return (p);
}
void insertatbeg( struct node **p, int element)
{
struct node *q;
q=(struct node*)malloc(sizeof(struct node));
q->data=element;
(*p)->prev=q;
q->next= *p;
q->prev=NULL;
*p =q;
}
void insertatend( struct node *p, int element)
{
struct node *q;
q=(struct node*)malloc(sizeof(struct node));
while(p->next !=NULL)
{
p=p->next;
}
q->prev=p;
p->next=q;
q->data=element;
q->next= NULL;
}
void insertatany( struct node *p,int key, int element)
{
struct node *q;
q=(struct node*)malloc(sizeof(struct node));
while((p->next !=NULL) && (p->data != key))
{
p=p->next;
}
q->next->prev=q;
q->next=p->next;
q->data=element;
q->prev=p;
p->next=q;

}
struct node* delnode(struct node *p, int n)
{
int i;
struct node *q,*r;
if(p==NULL)
printf("\n the list is empty");
else
{
q=NULL;
r=p;
i=1;
while(i<n)
{
q=r;
r=r->next;
i=i+1;
}
if(q==NULL)
{
p= p->next;
p->prev=NULL;
}
else
{
r->next->prev=q;
q->next= r->next;
free (r);
}
}
return (p);
}
void traverse(struct node *p)
{
printf("\n The list is \t");
while(p !=NULL)
{
printf("\t%d",p->data);
p=p->next;
}
}
void main()
{
int num=0,j,val,k,n;
struct node *head=NULL;
clrscr();
printf("\n 1. create ");
printf("\n 2. insert at beginning");
printf("\n 3. insert at end");
printf("\n 4. insert at any specific location");
printf("\n 5. deletion of any node ");
printf("\n 6. traverse");
printf("\n 7. exit");
do
{
printf("\n enter your choice");
scanf("%d",&j);

switch(j)
{
case 1 : printf("\n enter the data to put on node");
scanf("%d",&val);
head= create(head,val);
break;
case 2 : printf("\n enter new data to insert on the node");
scanf("%d",&val);
insertatbeg(&head,val);
break;
case 3 : printf("\n enter new data to insert on the node");
scanf("%d",&val);
insertatend(head,val);
break;
case 4 : printf("\n enter new data to insert on the node");
scanf("%d",&val);
printf("\n enter the key element after which you want to
insert anode");
scanf("%d",&k);
insertatany(head,k,val);
break;
case 5 : printf("\n enter the node no you want to delete");
scanf("%d",&n);
head=delnode(head,n);
break;
case 6 : traverse(head);
break;
case 7 : exit(0);
}
}
while(num==0);
getch();
}

You might also like