In this tutorial, we will write a C++ program to implement singly linked list.
In this program, we will create a separate function for each operation that is to be performed in a linked list. This operation includes:
- create()
- insert(): this includes insetion at the begining, at the end or at a given position.
- delet() : delete is also the same that is at the begining, at the end or at a given position.
- display()
- search()
The program takes user input at every step necessary from the user and performed accordingly. The following are the various C++ programming topics used in the program.
Singly linked list in C++
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #include <iostream> #include <conio.h> #include <stdlib.h> using namespace std; class Node { public_colon int info; Node * next; }; class List: public Node { Node *first, *last; public_colon List() { first = NULL; last = NULL; } void create(); void insert(); void delet(); void display(); void search(); }; //create function void List::create() { Node * temp; temp = new Node; int n; cout << "\nEnter an Element: "; cin >> n; temp->info = n; temp->next = NULL; if (first == NULL) { first = temp; last = first; } else { last->next = temp; last = temp; } } //insert function //at chosen position void List::insert() { Node *prev, *cur; prev = NULL; cur = first; int count = 1, pos, ch, n; Node *temp = new Node; cout << "\nEnter an Element: "; cin >> n; temp->info = n; temp->next = NULL; cout << "\nChoose the position:"; cout << "\n1 for the beginning."; cout << "\n2 for the end."; cout << "\n3 for in between."; cout << "\nEnter Your Choice: "; cin >> ch; switch (ch) { case 1: temp->next = first; first = temp; break; case 2: last->next = temp; last = temp; break; case 3: cout << "\nEnter the Position to Insert: "; cin >> pos; while (count != pos) { prev = cur; cur = cur->next; count++; } if (count == pos) { prev->next = temp; temp->next = cur; } else cout << "\nNot Able to Insert"; break; } } //function to delete //at chosen position void List::delet() { Node *prev = NULL, *cur = first; int count = 1, pos, ch; cout << "Select the position for deletion: \n"; cout << "\n1 for the beginning."; cout << "\n2 for the end."; cout << "\n3 for in between."; cout << "\nEnter Your Choice:"; cin >> ch; switch (ch) { case 1: if (first != NULL) { cout << "\nDeleted Element is " << first->info; first = first->next; } else cout << "\nOperation Failed"; break; case 2: while (cur != last) { prev = cur; cur = cur->next; } if (cur == last) { cout << "\nDeleted Element is: " << cur->info; prev->next = NULL; last = prev; } else cout << "\nOperation Failed"; break; case 3: cout << "\nEnter the Position of Deletion: "; cin >> pos; while (count != pos) { prev = cur; cur = cur->next; count++; } if (count == pos) { cout << "\nDeleted Element is: " << cur->info; prev->next = cur->next; } else { cout << "\nOperation Failed"; } break; } } //display function //check for empty list first void List::display() { Node *temp = first; if (temp == NULL) { cout << "\nList is Empty"; } while (temp != NULL) { cout << temp->info; cout << "-->"; temp = temp->next; } cout << "NULL"; } //search function //check for empty list first void List::search() { int value, pos = 0; bool flag = false; if (first == NULL) { cout << "List is Empty"; return; } cout << "Enter the Value to be Searched: "; cin >> value; Node * temp; temp = first; while (temp != NULL) { pos++; if (temp->info == value) { flag = true; cout << "Element" << value << "is Found at " << pos << " Position"; return; } temp = temp->next; } if (!flag) { cout << value << " is not present in the List"; } } //drive function int main() { List ls; int ch; while (1) { cout << "\nChoose an Operation: "; cout << "\n1 to CREATE."; cout << "\n2 to INSERT."; cout << "\n3 to DELETE."; cout << "\n4 to SEARCH."; cout << "\n5 to DISPLAY."; cout << "\n6 to EXIT."; cout << "\nEnter Your Choice: "; cin >> ch; switch (ch) { case 1: ls.create(); break; case 2: ls.insert(); break; case 3: ls.delet(); break; case 4: ls.search(); break; case 5: ls.display(); break; case 6: return 0; } } return 0; } |
Output:
Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 1
Enter an Element: 2
Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 2
Enter an Element: 4
Choose the position:
1 for the beginning.
2 for the end.
3 for in between.
Enter Your Choice: 2
Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 5
2-->4-->NULL
Choose an Operation:
1 to CREATE.
2 to INSERT.
3 to DELETE.
4 to SEARCH.
5 to DISPLAY.
6 to EXIT.
Enter Your Choice: 6
Similarly, you can follow the option, and then you can search and delete an element from the list.