-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublylinkedlist.cpp
162 lines (145 loc) · 3.72 KB
/
doublylinkedlist.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<iostream>
#include<conio.h>
#include<malloc.h>
using namespace std;
struct node
{
int data;
struct node *prev;
struct node *next;
}*head;
void create(int n)
{
int val;
struct node *newnode,*temp;
head= (struct node *) malloc(sizeof(struct node));
cout<<"\n Enter the data of first node\n";
cin>>val;
head->data=val;
head->prev=NULL;
head->next=NULL;
temp=head;
for(int i=2;i<=n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
cout<<"\nUnable to allocate memory";
break;
}
cout<<"\nEnter the value of node no "<<i<<"\n";
cin>>val;
newnode->data=val;
newnode->prev=temp;
newnode->next=NULL;
temp->next =newnode;
temp=temp->next;
}
}
void addFirst(int val)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->prev=NULL;
newnode->next=head;
head->prev=newnode;
head=newnode;
}
void addlast(int val)
{
struct node *last;
last=head;
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
while(last->next!=NULL)
{
last = last->next;
}
newnode->data=val;
newnode->next=NULL;
newnode->prev=last;
last->next=newnode;
cout<<"\n---Element inserted successfully---";
}
int search(int key)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
return 1;
}
temp=temp->next;
}
return -1;
}
void display()
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
cout<<" "<<temp->data;
temp=temp->next;
}
cout<<" "<<temp->data;
}
int main()
{
int ch;
do{
cout<<"\n ----Doubly lnked list menu-----";
cout<<"\n1.Create Doubly Linked";
cout<<"\n2.Display";
cout<<"\n3.Add element in the first position";
cout<<"\n4.Addd element in the last position";
cout<<"\n5.Search a element in the Douunly linked list";
cout<<"\n6.Exit";
cout<<"\nEnter your choice";
cin>>ch;
switch(ch)
{
case 1:
int n;
cout<<"\nEnter the no of nodes you want innyour linked list";
cin>>n;
create(n);
cout<<"\n---Doubly linked list created successfully";
break;
case 2:
cout<<"\n------DISPLAYING DOUBLY LINKEED LIST-------";
display();
break;
case 3:
int val;
cout<<"\n Enter the value you want add at the first position";
cin>>val;
addFirst(val);
cout<<"\n----Element inserted successfully in the doubly linkd list";
break;
case 4:
int v;
cout<<"\n Enter the value you want to add at last position";
cin>>v;
addlast(v);
break;
case 5:
int key;
cout<<"\n Enter the key you want to searcch in the linkedlist";
cin>>key;
int result=search(key);
if(result==1)
{
cout<<"\n---------Element is present in the list-----------";
}
else{
cout<<"\n---------Element is not present in the key-----------";
}
break;
}
}while(ch!=6);
return 0;
}