-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11. DLL.cpp
112 lines (111 loc) · 2.37 KB
/
11. DLL.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
#include<iostream>
using namespace std;
class DLIST{
int data,n,i,pos,count;
DLIST *prev,*next;
public:
void create();
void insert();
void del();
void dis();
} *head=NULL,*temp=NULL,*newnode,*tail=NULL;
void DLIST::create(){
cout<<"Enter the Number of Nodes to be created:";
cin>>n;
for(i=0;i<n;i++){
newnode = new DLIST;
cout<<"Enter the data field:";
cin>>newnode->data;
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL)
head=temp=newnode;
else{
temp->next=newnode;
newnode->prev=temp;
temp=temp->next;
}
}
}
void DLIST::insert(){
newnode = new DLIST;
cout<<"Enter the data field:";
cin>>newnode->data;
newnode->next=NULL;
newnode->prev=NULL;
cout<<"Enter the position:";
cin>>pos;
temp=head;
count=1;
if(count<pos){
while(count<pos){
temp=temp->next;
count++;
}
newnode->prev=temp->prev;
temp->prev->next=newnode;
temp->prev=newnode;
newnode->next=temp;}
else if(count==pos){
newnode->next=head;
head->prev=newnode;
newnode->prev=NULL;
head=newnode;
}
}
void DLIST::del(){
cout<<"Enter the position:";
cin>>pos;
temp=head;
count=1;
if(head==NULL)
cout<<"Underflow!!\n";
else if(count<pos){
while(count<pos){
temp=temp->next;
count++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
else if(count==pos){
temp=head;
temp->next->prev=NULL;
head=temp->next;
delete temp;
}
}
void DLIST::dis(){
temp=head;
if(head==NULL)
cout<<"List is empty\n";
else{
while(temp->next!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}cout<<temp->data<<" "<<endl;;
}
}
int main(){
int ch;
DLIST d;
while(1){
cout<<"Double Linked List:-"<<endl;
cout<<"1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit\nEnter your choice:"<<endl;
cin>>ch;
switch(ch){
case 1:d.create();
break;
case 2:d.insert();
break;
case 3:d.del();
break;
case 4:d.dis();
break;
case 5:exit(0);
default:cout<<"Invalid Choice!!\n";
}
}
return 0;
}