1
+ // Implementation of a Linked List in C++
2
+ #include < iostream>
3
+ using namespace std ;
4
+
5
+ // Implementation of a Node
6
+ class Node {
7
+ public:
8
+ Node *next; // Node pointer to point to next node
9
+ int data; // Integer stored in each node
10
+ public:
11
+ Node (int x){
12
+ data = x; // storing the data
13
+ next = NULL ; // pointer to next node is null by default
14
+ }
15
+ };
16
+ // Implementation of a Linked List Class
17
+ class SingleLinkedList {
18
+ private:
19
+ int listsize; // variable to store size of list
20
+ Node *head; // Node pointer to point to head node
21
+ public:
22
+ // constructor to initialise the list
23
+ SingleLinkedList (){
24
+ head = NULL ;
25
+ listsize = 0 ;
26
+ }
27
+ // Function to insert an element at the start
28
+ void insertionAtHead (int data){
29
+ Node *new_node = new Node (data); // create a new node
30
+ // if the list is empty new node is head node
31
+ if (head == NULL ){
32
+ head = new_node;
33
+ }
34
+ else {
35
+ // link the new node to head
36
+ new_node->next = head;
37
+ head = new_node;
38
+ }
39
+ listsize++;
40
+ }
41
+ // Function to insert an element at the end
42
+ void insertionAtTail (int data){
43
+ Node *new_node = new Node (data); // Create a new node
44
+ // if the list is empty new node is head node
45
+ if (head == NULL ){
46
+ head = new_node;
47
+ }
48
+ else {
49
+ // link the last node of the linked list to the new node
50
+ Node *curr = head; // current pointer to head node
51
+ while (curr->next != NULL ){
52
+ // traverse till last node is reached
53
+ curr = curr->next ;
54
+ }
55
+ curr->next = new_node;
56
+ listsize++;
57
+ }
58
+ }
59
+ // Function to insert at a user specified position
60
+ void insertionAtPos (int pos, int data){
61
+ if (pos < 0 && pos > listsize + 1 ){
62
+ // check is pos is a valid position in the list
63
+ cout << pos << " does not exist in the list" << endl;
64
+ }
65
+ else {
66
+ if (pos == 0 ){
67
+ // insert at head
68
+ insertionAtHead (data);
69
+ }
70
+ else if (pos == listsize + 1 ){
71
+ // insertion at tail
72
+ insertionAtTail (data);
73
+ }
74
+ else {
75
+ // insert anywhere else
76
+ Node *new_node = new Node (data); // new node
77
+ Node *curr = head; // pointer to the head node
78
+ Node *prev = head; // pointer to store previous node
79
+ int count = 0 ; // count positions till pos
80
+ while (curr != NULL ){
81
+ if (count == pos){break ;}
82
+ count++;
83
+ prev = curr;
84
+ curr = curr->next ;
85
+ }
86
+ // insertion between prev and curr if pos is found
87
+ if (curr != NULL )
88
+ {
89
+ prev->next = new_node;
90
+ new_node->next = curr;
91
+ }
92
+ }
93
+ listsize++;
94
+ }
95
+ }
96
+ // Function to delete head node of a linked list
97
+ void deletionAtHead (){
98
+ if (head == NULL ){
99
+ cout << " List is empty" << endl;
100
+ }
101
+ else {
102
+ cout << head->data << " has been deleted" << endl;
103
+ Node *after = head->next ; // pointer to point to next of head node
104
+ head->next = NULL ; // de-link head node from the rest of the list
105
+ head = after; // set next node as the head node, or NULL
106
+ listsize--;
107
+ }
108
+ }
109
+ // Function to delete the tail node of a linked list
110
+ void deletionAtTail (){
111
+ if (head == NULL ){
112
+ cout << " List is empty" << endl;
113
+ }
114
+ else {
115
+ // traverse to the last node
116
+ Node *curr = head; // pointer to the head node
117
+ Node *prev; // pointer to the second last node after traversal
118
+ while (curr->next != NULL ){
119
+ // traverses till last node
120
+ prev = curr;
121
+ curr = curr->next ;
122
+ }
123
+ // deletion of curr
124
+ cout << curr->data << " has been deleted" << endl;
125
+ prev->next = NULL ;
126
+ listsize--;
127
+ }
128
+ }
129
+ // Function to delete key specified by user
130
+ void deleteKey (int key){
131
+ if (head == NULL ){
132
+ cout << " List is empty" << endl;
133
+ }
134
+ else {
135
+ // if key is at head
136
+ if (head->data == key){deletionAtHead ();}
137
+ else {
138
+ // traverse to the last node
139
+ Node *curr = head; // pointer to the head node
140
+ Node *prev; // pointer to the second last node after traversal
141
+ while (curr != NULL ){
142
+ if (curr->data == key){break ;}
143
+ prev = curr;
144
+ curr = curr->next ;
145
+ }
146
+ if (curr != NULL ){
147
+ // deletion of curr
148
+ cout << curr->data << " has been deleted" << endl;
149
+ Node *temp = curr->next ;
150
+ curr->next = NULL ;
151
+ prev->next = temp;
152
+ }
153
+ else {
154
+ cout << key << " does not exist" << endl;
155
+ }
156
+ }
157
+ }
158
+ }
159
+ // Function to display the linked list
160
+ void display (){
161
+ Node *curr = head;
162
+ while (curr != NULL ){
163
+ cout << curr->data << " -->" ;
164
+ curr = curr->next ;
165
+ }
166
+ cout << " Null" << endl;
167
+ }
168
+ };
169
+
170
+ int main (){
171
+ SingleLinkedList L;
172
+ L.insertionAtHead (1 );
173
+ L.insertionAtTail (2 );
174
+ L.insertionAtPos (3 ,3 );
175
+ L.insertionAtTail (4 );
176
+ L.display ();
177
+ L.deletionAtHead ();
178
+ L.deletionAtTail ();
179
+ L.deleteKey (3 );
180
+ L.display ();
181
+ return 0 ;
182
+ }
0 commit comments