-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlinkedlist.cpp
169 lines (128 loc) · 3.27 KB
/
linkedlist.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
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
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this.data = data;
this->next = NULL
}
public:
int size(Node* head) {
if(head == NULL) {
cout << "Linkedlist is empty" <<endl;
return -1
}
int size = 0;
while(head->next != NULL) {
size +=1;
head = head->next;
}
return size;
}
void display(Node* head) {
if(head->next == NULL) {
cout << "Linkedlist is empty" <<endl;
}
while(head->next != NULL) {
cout << head.data;
head = head->next;
}
}
// insert at the head of a linkedlist
Node* insertAtHead(int data, Node* head) {
Node* newNode = new Node(data);
newNode->next = head;
head = newNode;
return head;
}
//insert at the end of a linkedlist
Node* insertAtTail(int data, Node*head) {
Node* newNode = new Node(data);
while(true) {
if(head->next == NULL) {
head->next = newNode;
break;
}
head = head->next;
}
}
//insert at any position in a linkedlist
void insertAtPosition(Node** headRef, int position, int data) {
Node* newNode = new Node(data);
if (*headRef == NULL || position == 0) {
newNode->next = *headRef;
*headRef = newNode;
return;
}
Node* current = *headRef;
for (int i = 0; current != NULL && i < position - 1; i++) {
current = current->next;
}
if (current == NULL) {
return;
}
// link the new node to the next node
newNode->next = current->next;
// link the previous node to the new node
current->next = newNode;
}
//delete node at head
Node* deleteAthead(Node* head) {
if (head == nullptr) {
return nullptr;
}
Node* newHead = head->next;
delete head;
return newHead; // return new head to calling function
}
//delete at tail
Node* deleteAtTail(Node* head) {
if (head == nullptr) {
cout << "Linked list is empty." << endl;
return nullptr;
}
if (head->next == nullptr) {
// list has only one node, delete it and return nullptr
delete head;
return nullptr;
}
Node* cur = head;
while (cur->next->next != nullptr) {
cur = cur->next;
}
delete cur->next;
cur->next = nullptr;
return head;
}
//delete at any position of a linkedlist
Node* deleteAtPosition(int position, Node* head) {
Node* current = head;
Node* prev = nullptr;
int i = 0;
// Traverse the list to the node before the one to be deleted
while (current != nullptr && i < position-1) {
prev = current;
current = current->next;
i++;
}
// Check if position is valid
if (current == nullptr || current->next == nullptr) {
cout << "Invalid position" << endl;
return head;
}
// Delete the node at the specified position
Node* temp = current->next;
current->next = temp->next;
delete temp;
// Update the head pointer if necessary
if (position == 1) {
head = current->next;
}
return head;
}
};
int main() {
return 0;
}