-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingly_Link_List.cpp
More file actions
150 lines (149 loc) · 3.17 KB
/
Copy pathSingly_Link_List.cpp
File metadata and controls
150 lines (149 loc) · 3.17 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
#include <bits/stdc++.h>
#define null NULL
using namespace std;
class Node
{
public:
int val;
Node *next;
Node(int val)
{
this->val = val;
this->next = null;
}
};
void insert_val(Node *&head, int val)
{
Node *newnode = new Node(val);
if (head == null)
{
head = newnode;
cout << "Added to List" << endl
<< endl;
return;
}
Node *tmp = head;
while (tmp->next != null)
{
tmp = tmp->next;
}
tmp->next = newnode;
cout << "Added to List" << endl
<< endl;
}
void print_list(Node *head)
{
cout << "Your Link List: ";
Node *tmp = head;
while (tmp != null)
{
cout << tmp->val << " ";
tmp = tmp->next;
}
cout << endl;
}
void insert_at_pos(Node *&head, int pos, int val)
{
Node *newnode = new Node(val);
if (pos == 0)
{
newnode->next = head;
head = newnode;
cout << "Added at the " << pos << "index" << endl;
return;
}
Node *tmp = head;
for (int i = 1; i < pos; i++)
{
tmp = tmp->next;
if (tmp == null)
{
cout << "Invalid Position" << endl;
return;
}
}
newnode->next = tmp->next;
tmp->next = newnode;
cout << "Added at the " << pos << " index" << endl;
}
void delete_node(Node *&head, int pos)
{
if (pos == 0)
{
Node *tmp = head;
head = head->next;
delete tmp;
cout << "Deleted " << pos << " index" << endl;
return;
}
Node *tmp = head;
for (int i = 1; i < pos; i++)
{
tmp = tmp->next;
if (tmp == null)
{
cout << "Invalid Index" << endl;
return;
}
}
Node *deletenode = tmp->next;
tmp->next = tmp->next->next;
delete deletenode;
cout << "Deleted " << pos << " index" << endl;
}
int main()
{
Node *head = null;
while (true)
{
cout << "1. Insert Value" << endl;
cout << "2. Insert at position" << endl;
cout << "3. Delete Value" << endl;
cout << "4. Print Link List" << endl;
cout << "5. Terminate Program" << endl;
cout << "Enter your choice: ";
int val;
cin >> val;
if (val == 1)
{
cout << "Enter a value: ";
int val;
cin >> val;
insert_val(head, val);
}
else if (val == 2)
{
int pos, val;
cout << "Enter index: ";
cin >> pos;
cout << "Enter value: ";
cin >> val;
insert_at_pos(head, pos, val);
}
else if (val == 3)
{
int pos;
cout << "Enter index to delete: ";
cin >> pos;
delete_node(head, pos);
}
else if (val == 4)
{
print_list(head);
}
else if (val == 5)
{
cout << endl
<< "___Program___Terminated___" << endl
<< endl;
break;
}
else
{
cout << endl
<< "Invalid Choice" << endl
<< endl;
}
}
return 0;
}