-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertInLL.cpp
85 lines (76 loc) · 1.77 KB
/
insertInLL.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
//Insert operation for linked list at beginning, middle and end of the list
// TC = O(1) to insert at beginnning and end, O(n) to insert at the end
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data; //node has data
Node* next; //next pointer of node
//constructor
Node(int data){
this -> data = data;
this -> next = NULL;
}
};
//insert at Head
void insertAtHead(Node* &head, int data){
//create a new node
Node* temp = new Node(data);
temp -> next = head;
head = temp;
}
void insertAtTail(Node* &tail, int data) {
//create a new node
Node* temp = new Node(data);
tail ->next = temp;
tail = temp;
}
void insertAtPosition(Node* &head, Node* &tail, int position, int d) {
//insert at start
if (position == 1) {
insertAtHead(head, d);
return;
}
Node* temp = head;
int cnt = 1;
//reach that position
while(cnt < position-1) {
temp = temp ->next;
cnt++;
}
//insert at last postion and update tail
if (temp->next == NULL){
insertAtTail(tail, d);
return;
}
//insert at middle
//create a node for d
Node* nodeToInsert = new Node(d);
nodeToInsert -> next = temp -> next;
temp -> next = nodeToInsert;
}
//traverse through the list
void printList(Node* &head) { //reference is passed to not create a copy
//create a temp pointer
Node* temp = head;
while(temp!= NULL) {
cout << temp-> data<<" ";
temp = temp -> next;
}
cout<<endl;
}
int main() {
//create a new node(object of Node class)
Node* node1 = new Node(10);
//point head to the node1;
Node* head = node1;
Node* tail = node1;
//print the list
printList(head);
insertAtTail(tail, 15);
printList(head);
insertAtTail(tail, 20);
printList(head);
insertAtPosition(head, tail, 4, 1);
printList(head);
}