-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkList.cpp
More file actions
105 lines (96 loc) · 2.41 KB
/
linkList.cpp
File metadata and controls
105 lines (96 loc) · 2.41 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
// Linklist
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int data;
Node *next;
};
class LinkList {
private:
Node *head, *tail;
public:
LinkList() {
head = NULL;
tail = NULL;
}
void addNode(int n) {
Node *temp = new Node;
temp->data = n;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
temp = NULL;
} else {
tail->next = temp;
tail = temp;
}
}
void display() {
Node *temp = new Node;
temp = head;
while (temp != NULL) {
cout << temp->data << "\t";
temp = temp->next;
}
}
void insertStart(int value) {
Node *temp = new Node;
temp->data = value;
temp->next = head;
head = temp;
}
void insertPosition(int pos, int value) {
Node *pre = new Node;
Node *cur = new Node;
Node *temp = new Node;
cur = head;
for (int i = 1; i < pos; i++) {
pre = cur;
cur = cur->next;
}
temp->data = value;
pre->next = temp;
temp->next = cur;
}
void deleteFirst() {
Node *temp = new Node;
temp = head;
head = head->next;
delete temp;
}
void deleteLast() {
Node *current = new Node;
Node *previous = new Node;
current = head;
while (current->next != NULL) {
previous = current;
current = current->next;
}
tail = previous;
previous->next = NULL;
delete current;
}
void deletePosition(int pos) {
Node *current = new Node;
Node *previous = new Node;
current = head;
for (int i = 1; i < pos; i++) {
previous = current;
current = current->next;
}
previous->next = current->next;
}
};
int main () {
LinkList a;
a.addNode(1);
a.addNode(2);
a.addNode(3);
a.addNode(4);
a.addNode(5);
a.display();
return 0;
}