Skip to content

Commit a77d1b0

Browse files
authored
Merge pull request DeepNinja07x#65 from Spyy004/master
Linked List Template All Operations(Add, Delete, Search)
2 parents 0e93f8a + 910ea97 commit a77d1b0

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

Data Strucrures/LinkedListTemplate

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// A simple CPP program to introduce
2+
// a linked list
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
class Node {
7+
public:
8+
int data;
9+
Node* next;
10+
};
11+
12+
// Program to create a simple linked
13+
// list with 3 nodes
14+
int main()
15+
{
16+
Node* head = NULL;
17+
Node* second = NULL;
18+
Node* third = NULL;
19+
20+
// allocate 3 nodes in the heap
21+
head = new Node();
22+
second = new Node();
23+
third = new Node();
24+
25+
/* Three blocks have been allocated dynamically.
26+
We have pointers to these three blocks as head,
27+
second and third
28+
head second third
29+
| | |
30+
| | |
31+
+---+-----+ +----+----+ +----+----+
32+
| # | # | | # | # | | # | # |
33+
+---+-----+ +----+----+ +----+----+
34+
35+
# represents any random value.
36+
Data is random because we haven’t assigned
37+
anything yet */
38+
39+
head->data = 1; // assign data in first node
40+
head->next = second; // Link first node with
41+
// the second node
42+
43+
/* data has been assigned to the data part of first
44+
block (block pointed by the head). And next
45+
pointer of the first block points to second.
46+
So they both are linked.
47+
48+
head second third
49+
| | |
50+
| | |
51+
+---+---+ +----+----+ +-----+----+
52+
| 1 | o----->| # | # | | # | # |
53+
+---+---+ +----+----+ +-----+----+
54+
*/
55+
56+
// assign data to second node
57+
second->data = 2;
58+
59+
// Link second node with the third node
60+
second->next = third;
61+
62+
/* data has been assigned to the data part of the second
63+
block (block pointed by second). And next
64+
pointer of the second block points to the third
65+
block. So all three blocks are linked.
66+
67+
head second third
68+
| | |
69+
| | |
70+
+---+---+ +---+---+ +----+----+
71+
| 1 | o----->| 2 | o-----> | # | # |
72+
+---+---+ +---+---+ +----+----+ */
73+
74+
third->data = 3; // assign data to third node
75+
third->next = NULL;
76+
77+
/* data has been assigned to the data part of the third
78+
block (block pointed by third). And next pointer
79+
of the third block is made NULL to indicate
80+
that the linked list is terminated here.
81+
82+
We have the linked list ready.
83+
84+
head
85+
|
86+
|
87+
+---+---+ +---+---+ +----+------+
88+
| 1 | o----->| 2 | o-----> | 3 | NULL |
89+
+---+---+ +---+---+ +----+------+
90+
91+
92+
Note that only the head is sufficient to represent
93+
the whole list. We can traverse the complete
94+
list by following the next pointers. */
95+
96+
return 0;
97+
}
98+
99+
// This code is contributed by rathbhupendra

0 commit comments

Comments
 (0)