-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathll.cpp
More file actions
176 lines (137 loc) · 2.86 KB
/
ll.cpp
File metadata and controls
176 lines (137 loc) · 2.86 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
167
168
169
170
171
172
173
174
175
176
//Linked List Implementation
//fully_correct
//Wow, This feels really good.
//Here, push is equivalent to insertAfter(&head,1,new_data)
//i.e if n=1, push operation is performed
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
//write 3 functions
/* one for inserting a node in the beginning
one for inserting a node at the nth position
one for inserting a node at the end
*/
void push(Node** head, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = *head;
*head = new_node;
}
//void insertAfter(Node* prev_node, int new_data)
//{
// if(prev_node == NULL)
// {
// cout<<"previous node can not be null"<<endl;
// return;
// }
// Node* new_node = new Node();
// new_node->data = new_data;
// new_node->next=prev_node->next;
// prev_node->next = new_node;
//}
void insertAfter(Node** head, int n, int new_data)
{
Node * new_node = new Node();
new_node->data = new_data;
new_node->next = NULL;
if(n==1)
{
new_node->next = *head;
*head = new_node;
return;
}
Node * temp = *head;
for(int i=0;i<n-2;i++)
{
temp= temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
void append(Node** head, int new_data)
{
Node* new_node = new Node();
Node* last = *head;
new_node->data = new_data;
new_node->next = NULL;
if(*head == NULL)
{
*head = new_node;
return;
}
//linked list traversal
while(last->next!=NULL)
{
last= last->next;
}
last->next = new_node;
return;
}
//Function for deleting a node
void deleteNode (Node** head,int n)
{
Node *temp = *head;
if (n==1)
{
*head= temp->next;
delete temp;
return;
}
for (int i=0;i<n-2;i++)
{
temp = temp->next;
Node* temp1 = temp->next;
temp->next = temp1->next;
delete temp1;
}
}
//function to reverse the linked list
void reverse(Node** head)
{
Node* current;
Node* next1;
Node* prev;
current = *head;
prev = NULL;
while(current !=NULL)
{
next1 = current->next;
current->next = prev;
prev = current;
current=next1;
}
*head = prev;
}
//printList Function
void printList(Node* n)
{
while(n!=NULL)
{
cout<<n->data<<"->";
n=n->next;
}
}
//driver code
int main()
{
Node* head = NULL;
append(&head,6); //6->NULL
push(&head,7); //7->6->NULL
push(&head,1); //1->7->6->NULL
append(&head,4); //1->7->6->4->NULL
insertAfter(&head,1,8); //1->7->8->6->4->NULL //explanation 1 is head, head->next is 7. so insert 8 after 7.
//this implementation of inserting a node @ nth position is not the best.
append(&head,111);
push(&head,1000);
printList(head);
deleteNode(&head,3);
reverse(&head);
cout<<"\n"<<"Reversed List is -->\n"<<endl;
printList(head);
return 0;
}