-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.cpp
95 lines (90 loc) · 2.69 KB
/
linked_list.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
#include <iostream>
using namespace std;
struct node
{
int no=0;
struct node* ptr=NULL;
};
struct node* h=NULL;
int main()
{
struct node* prev=NULL;
while (0<1)
{
struct node* n=new node;
cout<<h<<endl;
cout<<prev<<endl;
cout<<"Insert at the front:1, Insert at the back:2, Delete from the front:3, Delete from the back:4, Display:5, Exit:6"<<endl;
int in;
cin >> in;
switch(in)
{
case 1:
if(h==NULL)
{
h=n;
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
prev=n;
}
else
{
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
n->ptr=h;
h=n;
}
break;
case 2:
if(h==NULL)
{
h=n;
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
prev=n;
}
else
{
cout<<"Enter the element to insert: \n";int a;cin >> a;
n->no=a;
prev->ptr=n;
prev=n;
}
break;
case 3:
if(h==NULL) cout<<"List is empty!";
else
{
struct node* t=h;
h=t->ptr;
delete t;
}
break;
case 4:
if(h==NULL) cout<<"List is empty!";
else
{
struct node* t=h;
while(t->ptr!=prev) t=t->ptr;
prev=t;
delete t->ptr;
t->ptr=NULL;
}
break;
case 5:
if(h==NULL) cout<<"List is empty!";
else
{
cout<<endl;
struct node* t=h;
while(t!=NULL)
{
cout<<t->no<<endl;
t=t->ptr;
}
}
break;
case 6: exit(0);
}
}
}