Skip to content

Commit 1e128b6

Browse files
authored
singly linked list insertions and other operations
1 parent 2a36125 commit 1e128b6

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// A complete working C++ program to demonstrate
2+
// all insertion methods on Linked List
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
// A linked list node
7+
class Node
8+
{
9+
public:
10+
int data;
11+
Node *next;
12+
};
13+
14+
/* Given a reference (pointer to pointer)
15+
to the head of a list and an int, inserts
16+
a new node on the front of the list. */
17+
void push(Node** head_ref, int new_data)
18+
{
19+
/* 1. allocate node */
20+
Node* new_node = new Node();
21+
22+
/* 2. put in the data */
23+
new_node->data = new_data;
24+
25+
/* 3. Make next of new node as head */
26+
new_node->next = (*head_ref);
27+
28+
/* 4. move the head to point to the new node */
29+
(*head_ref) = new_node;
30+
}
31+
32+
/* Given a node prev_node, insert a new node after the given
33+
prev_node */
34+
void insertAfter(Node* prev_node, int new_data)
35+
{
36+
/*1. check if the given prev_node is NULL */
37+
if (prev_node == NULL)
38+
{
39+
cout<<"the given previous node cannot be NULL";
40+
return;
41+
}
42+
43+
/* 2. allocate new node */
44+
Node* new_node = new Node();
45+
46+
/* 3. put in the data */
47+
new_node->data = new_data;
48+
49+
/* 4. Make next of new node as next of prev_node */
50+
new_node->next = prev_node->next;
51+
52+
/* 5. move the next of prev_node as new_node */
53+
prev_node->next = new_node;
54+
}
55+
56+
/* Given a reference (pointer to pointer) to the head
57+
of a list and an int, appends a new node at the end */
58+
void append(Node** head_ref, int new_data)
59+
{
60+
/* 1. allocate node */
61+
Node* new_node = new Node();
62+
63+
Node *last = *head_ref; /* used in step 5*/
64+
65+
/* 2. put in the data */
66+
new_node->data = new_data;
67+
68+
/* 3. This new node is going to be
69+
the last node, so make next of
70+
it as NULL*/
71+
new_node->next = NULL;
72+
73+
/* 4. If the Linked List is empty,
74+
then make the new node as head */
75+
if (*head_ref == NULL)
76+
{
77+
*head_ref = new_node;
78+
return;
79+
}
80+
81+
/* 5. Else traverse till the last node */
82+
while (last->next != NULL)
83+
last = last->next;
84+
85+
/* 6. Change the next of last node */
86+
last->next = new_node;
87+
return;
88+
}
89+
90+
// This function prints contents of
91+
// linked list starting from head
92+
void printList(Node *node)
93+
{
94+
while (node != NULL)
95+
{
96+
cout<<" "<<node->data;
97+
node = node->next;
98+
}
99+
}
100+
101+
/* Driver code*/
102+
int main()
103+
{
104+
/* Start with the empty list */
105+
Node* head = NULL;
106+
107+
// Insert 6. So linked list becomes 6->NULL
108+
append(&head, 6);
109+
110+
// Insert 7 at the beginning.
111+
// So linked list becomes 7->6->NULL
112+
push(&head, 7);
113+
114+
// Insert 1 at the beginning.
115+
// So linked list becomes 1->7->6->NULL
116+
push(&head, 1);
117+
118+
// Insert 4 at the end. So
119+
// linked list becomes 1->7->6->4->NULL
120+
append(&head, 4);
121+
122+
// Insert 8, after 7. So linked
123+
// list becomes 1->7->8->6->4->NULL
124+
insertAfter(head->next, 8);
125+
126+
cout<<"Created Linked list is: ";
127+
printList(head);
128+
129+
return 0;
130+
}
131+
132+
133+
// This code is contributed by rathbhupendra

0 commit comments

Comments
 (0)