-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHKDLL.CPP
More file actions
161 lines (151 loc) · 2.29 KB
/
CHKDLL.CPP
File metadata and controls
161 lines (151 loc) · 2.29 KB
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
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class dlist
{
dlist *right,*left;
int info;
public: dlist()
{
right=left=NULL;
}
dlist* getnode()
{
return (new dlist);
}
dlist* insertp(dlist*);
dlist* deletep(dlist*);
void display(dlist*);
};
typedef dlist* node;
node dlist::insertp(node first)
{
node temp,prev,cur;
int i,pos;
temp=getnode();
cout<<"\n Enter item to be inserted \n";
cin>>i;
cout<<"\n Enter position \n";
cin>>pos;
temp->info=i;
temp->right=temp->left=NULL;
if(first==NULL&&pos==1)
{
return temp;
}
if(first==NULL)
{
cout<<"\n Invalid position to insert value \n";
return first;
}
if(pos==1)
{
temp->right=first;
first->left=temp;
return temp;
}
int count=1;
prev=NULL;
cur=first;
while(cur!=NULL&&count!=pos)
{
prev=cur;
cur=cur->right;
cur->left=prev;
count++;
}
if(count==pos)
{
prev->right=temp;
temp->left=prev;
temp->right=cur;
cur->left=temp;
return first;
}
return first;
}
node dlist::deletep(node first)
{
node cur,prev;
int count,pos;
cout<<"\n Enter position of node to be deleted \n";
cin>>pos;
if(first==NULL&&pos<=1)
{
cout<<"\n Invalid position to delete \n";
return NULL;
}
if(pos==1)
{
cur=first;
first=first->right;
first->left=NULL;
free(cur);
return first;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL)
{
if(count==pos)
{
break;
}
prev=cur;
cur=cur->right;
cur->left=prev;
count++;
}
if(count!=pos)
{
cout<<"\n Invalid position\n";
return first;
}
prev->right=cur->right;
// prev->left=NULL;
free(cur);
return first;
}
void dlist::display(node first)
{
node temp;
temp=first;
if(temp==NULL)
{
cout<<"\n List empty\n";
return;
}
cout<<"\n Contents are \n";
while(temp!=NULL)
{
cout<<"\t"<<temp->info;
temp=temp->right;
}
}
void main()
{
node first=NULL;
int ch;
clrscr();
for(;;)
{
cout<<"\n\t MENU \n";
cout<<"1:INSERT NODE AT SPECIFIED POSITION \n";
cout<<"2:DELETE A SPECIFIED NODE \n";
cout<<"3:DISPLAY\n";
cout<<"4:EXIT\n";
cout<<"\n enter your choice \n";
cin>>ch;
switch(ch)
{
case 1:first=first->insertp(first);
break;
case 2:first=first->deletep(first);
break;
case 3:first->display(first);
break;
default:exit(0);
}
}
}