-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_at_head.cpp
117 lines (108 loc) · 2.25 KB
/
insert_at_head.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
113
114
115
116
117
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int val;
Node *next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};
void print_linked_list(Node *head)
{
cout << "Your Linked List: ";
Node *tmp = head;
while (tmp != NULL)
{
cout << tmp->val << " ";
tmp = tmp->next;
}
cout << endl;
}
void insert_at_tail(Node *&head, int v)
{
Node *newNode = new Node(v);
if (head == NULL)
{
head = newNode;
return;
}
Node *tmp = head;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = newNode;
}
void insert_at_position(Node *head, int pos, int v)
{
Node *newNode = new Node(v);
Node *tmp = head;
for (int i = 1; i <= pos - 1; i++)
{
tmp = tmp->next;
}
newNode->next = tmp->next;
tmp->next = newNode;
cout << "Inserted at Position:- " << pos << endl;
}
void insert_at_head(Node *&head, int v)
{
Node *newNode = new Node(v);
newNode->next = head;
head = newNode;
cout << endl
<< "Inserted at Head" << endl;
}
int main()
{
Node *head = NULL;
while (true)
{
cout << "Option 1: Insert at Tail" << endl;
cout << "Option 2: Print Linked List" << endl;
cout << "Option 3: Insert at Position" << endl;
cout << "Option 4: Insert at Head" << endl;
cout << "Option 5: Terminate" << endl;
int op;
cin >> op;
if (op == 1)
{
cout << "Insert Value: ";
int v;
cin >> v;
insert_at_tail(head, v);
}
else if (op == 2)
{
print_linked_list(head);
}
else if (op == 3)
{
int pos, v;
cout << "Enter Position: ";
cin >> pos;
cout << "Enter Value: ";
cin >> v;
if (pos == 0)
insert_at_head(head, v);
else
insert_at_position(head, pos, v);
}
else if (op == 4)
{
cout << "Enter Value: ";
int v;
cin >> v;
insert_at_head(head, v);
}
else if (op == 5)
{
break;
}
}
return 0;
}