Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Doubly

The document contains a C program that implements a doubly linked list with various functionalities such as creating a list, inserting nodes at the beginning or after a specific position, deleting nodes, displaying the list, and counting elements. The program uses a menu-driven approach to allow users to interact with the linked list. It includes functions for each operation and handles memory allocation for new nodes.

Uploaded by

isha chatterjee
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 views9 pages

Doubly

The document contains a C program that implements a doubly linked list with various functionalities such as creating a list, inserting nodes at the beginning or after a specific position, deleting nodes, displaying the list, and counting elements. The program uses a menu-driven approach to allow users to interact with the linked list. It includes functions for each operation and handles memory allocation for new nodes.

Uploaded by

isha chatterjee
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/ 9

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
int data;
struct node *next;
} *start;
void create_ll(int);
void insertatbeg(int);
void insertafter(int,int);
void del(int);
void display();

void main()
{
clrscr();
int choice,n,m,pos,i;
start=NULL;
while(1)
{
printf("1. create list\n");
printf("2. Add at
beginning\n");
printf("3. Add after\n");
printf("4. Delete\n");
printf("5. Display\n");
printf("6. count\n");
printf("7. Reverse\n");
printf("8. Search\n");
printf("9. Quit\n");
printf("Enter your
choice:\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("how many nodes
you want: \n ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the
data=");
scanf("%d",&m);
create_ll(m);
}
printf("single linked
list created \n\n");
break;
case 2:
printf("Enter the
data=");
scanf("%d",&m);
insertatbeg(m);
printf("Insert at
beginning done \n\n");
break;

case 3:
printf("Enter the data
\n ");
scanf("%d", &m);
printf("Enter the
position after which
data will be inserted:\n");
scanf("%d",&pos);
insertafter(m,pos);

printf("Insertion at
position = %d done \n\n",pos);
break;
case 4:
if(start == NULL)
{
printf("List is
empty\n");
continue;
}
printf("Enter the
elements for deletion=");
scanf("%d",&m);
del(m);
printf("Deletion
Complete\n\n");
break;

case 5:
display();
break;

case 9:
exit(9);

default:
printf("Wrong
Choice\n");
}
getch();

}
}

void create_ll(int e)
{
struct node *q,*tmp;
tmp = (struct node*)malloc
(sizeof(struct node));
tmp-> data = e;
tmp-> next = NULL;
if(start == NULL)
{
tmp-> prev = NULL;
start = tmp;
}
else
{
q=start;
while(q->next!= NULL)
q = q->next;
q-> next =tmp;
tmp-> prev= q;
}
}

void insertatbeg(int e)
{
struct node *tmp;
tmp=(struct node *)malloc
(sizeof(struct node));
tmp-> data = e;
tmp-> prev = NULL;
tmp -> next = start;
start-> prev = tmp;
start = tmp;
}

void insertafter(int e, int pos)


{
struct node *tmp, *q;
int i;
q=start;
for(i=0;i<pos-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("there are less than
%d elements", pos);
return;
}
}
tmp=(struct node*)malloc
(sizeof(struct node));
tmp->data=e;
q->next->prev = tmp;
tmp->next = q->next;
tmp->prev = q;
q->next=tmp;
}

void del(int e)
{
struct node *tmp,*q;
if(start->data==e)
{
tmp=start;
start=start->next;
start->prev = NULL;
free(tmp);
return;
}
q=start;
while(q->next->next !=NULL)
{
if(q->next->data==e)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
free(tmp);
return;
}
q=q->next;
}
if(q->next->data==e)
{
tmp=q->next;
free(tmp);
q->next = NULL;
return;
}
printf("elements %d not found
\n",e);
}
void display()
{
struct node *q;
if(start == NULL)
{
printf("list is empty \n");
return;
}
q=start;
printf("list is:\n");
while(q!=NULL)
{
printf("link_of_prev_node = %d
and Data = %d and
link_of_next_node =
%u\n",q->prev,q->data,q-
>next);
q=q->next;
}
printf("\n");
}

You might also like