-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu_Driven_Linked_List.cpp
More file actions
166 lines (135 loc) · 4.4 KB
/
Menu_Driven_Linked_List.cpp
File metadata and controls
166 lines (135 loc) · 4.4 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
162
163
164
165
166
#include <iostream>
using namespace std;
class node // Declaring the node class
{
public:
int data; // The data member is an integer, and
node *next; // the next member is a pointer to another node (address of another node).
};
node *head = NULL; // Pointer to the starting node which named as "head" of node type which is currently holding adsress 'NULL'.
void insertatbeg(int val) // Defining Function
{
node *p = new node; // Creation of new node
p->data = val; // Putting data
p->next = NULL; // Putting adsress as NULL
if(head == NULL)
{
head = p;
}
else
{
p->next = head; // Linking between the nodes
head = p; // head points to the 'p' because it is a pointer holding some address of 'p' node
}
}
void insertatend(int val)
{
node *p = new node; // Creation of new node
p->data = val; // Putting data
p->next = NULL; // Putting adsress as NULL
node*temp = head; // here temp holding the address of head
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next = p;
}
void insertatposition(int val, int pos)
{
node *p = new node; // Creation of new node
p->data = val; // Putting data
p->next = NULL; // Putting adsress as NULL
node *temp = head; // here temp holding the address of head
int i = 1;
while (i < pos-1)
{
temp = temp->next;
i++;
}
p->next = temp->next;
temp->next = p;
}
void traverse() // For traversing the nodes
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
void deleteatbeginning() // Delete the element at beginning
{
node * temp = head;
head = head->next;
delete(temp);
}
void deleteatend() // Delete the element at end
{
node *temp = head;
node *temp2 = NULL;
while(temp->next!=NULL)
{
temp2 = temp;
temp = temp->next;
}
delete(temp);
temp2->next=NULL;
}
void deleteatposition(int val,int pos) // Delete the element at random position
{
node *temp = head;
node *temp2 = NULL;
int i=1;
while(i<pos)
{
temp2 = temp;
temp = temp->next;
i++;
}
temp2->next=temp->next;
delete(temp);
}
int main()
{
int choice , val, pos;
cout<<"\n\t\t* * * * * MENU DRIVEN PROGRAM FOR INSERTION AND DELETION OF THE LINKED LISTS * * * * * \n";
cout<<"\n1.Insertion at Beginning \n2.Insertion at End \n3.Insertion at Random Position \n4.Traverse \n"
"5.Deletion at Beginning \n6.Deletion at End \n7.Deletion at Random Position \n8.Exit\n";
while(1)
{
cout<<"\nEnter the Choice ";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter the value which you want to insert ";
cin>>val;
insertatbeg(val);
break;
case 2: cout<<"Enter the value which you want to insert ";
cin>>val;
insertatend(val);
break;
case 3: cout<<"Enter the value which you want to insert ";
cin>>val;
cout<<"Enter the position where you want to insert the value ";
cin>>pos;
insertatposition(val,pos);
break;
case 4: traverse();
case 5: deleteatbeginning();
break;
case 6: deleteatend();
break;
case 7: cout<<"Enter the position where you want to delete the value ";
cin>>pos;
cout<<"Enter the value which you want to delete ";
cin>>val;
deleteatposition(val,pos);
break;
case 8: exit(0);
default: cout<<"\n INVALID CHOICE....";
}
}
return 0;
}