-
Notifications
You must be signed in to change notification settings - Fork 6
/
DLinkedList.c
executable file
·105 lines (102 loc) · 1.55 KB
/
DLinkedList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next, *prev;
}*head = NULL;
void create ()
{
struct node *p, *q;
p=(struct node*)malloc(sizeof(struct node));
printf("Enter the value you want to insert : ");
scanf("%d",&p->data);
p->next=NULL;
p->prev=NULL;
if(head==NULL)
{
head=p;
return ;
}
q=head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
p->prev=q;
}
void display ()
{
if(head==NULL)
{
printf("The list is empty\n");
return;
}
struct node *p;
p=head;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
void any (int item)
{
int pos;
printf("Enter the position : ");
scanf("%d",&pos);
struct node *p,*q;
p=(struct node*)malloc(sizeof(struct node));
p->data=item;
p->next = NULL;
int counter = 0;
q=head;
while(q!=NULL)
{
++counter;
if(counter==pos-1)
{
p->next=q->next;
q->next->prev=p;
q->next=p;
p->prev=q;
break;
}
q=q->next;
}
printf("The number of elements in the DLL is : %d\n",counter);
}
void search (int item)
{
struct node *q;
q=head;
int counter;
while(q!=NULL)
{
++counter;
if(q->data==item)
{
printf("The required element was found\n");
}
q=q->next;
}
}
int main ()
{
int num = 0;
while(num!=4)
{
printf("Enter the choice : ");
scanf("%d",&num);
if(num==1){create();}
if(num==2){display ();}
if(num==3)
{
int item;
printf("Enter the value you want to push : \n");
scanf("%d",&item);
any(item);
}
}
}