-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List 1.cpp
119 lines (108 loc) · 2.2 KB
/
Linked List 1.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
118
119
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node*next;
//initialization list if memory declared and assigned value and assignment works on already declared memory
Node(int d):data(d), next(NULL){}
~Node(){
if(next!=NULL){
delete next; //deletes all the LL
//recursive calling of destructor for next Node
}
cout<<"Deleting Node "<<data<<": ";
}
};
//&head for pass by reference wherver its value is to be changed again and again
void insertAtHead(Node*&head, int data){
if (head == NULL){
head = new Node(data); //if tail also then head=tail=new Node
return;
}
Node*n = new Node(data);
n->next = head;
head = n;
return;
}
int length(Node*head){
int count = 0;
while(head!=NULL){
count++;
head = head->next;
}
return count;
}
//& can and cannot be considered if p=0 then we need to change the head pointer so &head is helpfull
void insertInMiddle(Node*&head, int data, int pos){
if(pos == 0){
insertAtHead(head, data);
return;
}
if(pos>length(head)){
return;
}
Node*t = head;
for (int jump = 1; jump <= pos-1; jump++){
t = t->next;
}
Node*n = new Node(data);
n->next = t->next;
t->next = n;
return;
}
// void insertAfterHead(Node*&head, int data){
// if(head==NULL){
// head = new Node(data);
// return;
// }
// Node*n = new Node(data);
// Node*ptr = head;
// while(ptr->next!=NULL){
// ptr = ptr->next;
// }
// ptr->next=n;
// return;
// }
void deleteLL(Node*&head){
delete head;
head = NULL;
}
void deleteInMiddle(Node*&head, int pos){
if(pos == 0){
Node*t = head->next;
head->next = NULL;
delete head;
head = t;
return;
}
Node *prev = NULL, *temp = head;
for(int jump = 1; jump <= pos; jump++){
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
temp->next = NULL;
delete temp;
return;
}
void printLL(Node*head){
while(head!=NULL){
cout<<head->data<<"->";
head = head->next;
}
cout<<endl;
}
int main(int argc, char const *argv[]){
Node*head = NULL;
insertAtHead(head, 1);
insertAtHead(head, 2);
insertAtHead(head, 3);
printLL(head);
insertInMiddle(head, 4, 0);
printLL(head);
deleteInMiddle(head, 3);
printLL(head);
//deleteLL(head);
return 0;
}