Skip to content

Commit df65f85

Browse files
committed
Linked list intro program
1 parent 158fe86 commit df65f85

File tree

2 files changed

+110
-6
lines changed

2 files changed

+110
-6
lines changed

Linked-list/Linked-list-intro.cpp

+110-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Node // class to create a node
1414
}
1515
};
1616

17-
class List
17+
class List // class to add, delete, print and search nodes
1818
{
1919
Node *head;
2020
Node *tail;
@@ -25,7 +25,7 @@ class List
2525
head = tail = NULL;
2626
}
2727

28-
void push_front(int val)
28+
void push_front(int val) // add node on front of linked list
2929
{
3030
Node *newNode = new Node(val);
3131

@@ -41,28 +41,132 @@ class List
4141
}
4242
}
4343

44-
void printLL()
44+
void push_back(int val) // add node on back or end of linked list
45+
{
46+
Node *newNode = new Node(val);
47+
48+
if (head == NULL) // empty linked list
49+
{
50+
head = tail = newNode;
51+
return;
52+
}
53+
else // not empty
54+
{
55+
tail->next = newNode;
56+
tail = newNode;
57+
}
58+
}
59+
60+
void pop_front() // delete node from front of linked list
61+
{
62+
if (head == NULL) // empty linked list
63+
{
64+
cout << "LL is empty\n";
65+
return;
66+
}
67+
68+
Node *temp = head;
69+
head = head->next;
70+
temp->next = NULL;
71+
72+
delete temp;
73+
}
74+
75+
void pop_back() // delete node from back or end of linked list
76+
{
77+
if (head == NULL) // empty linked list
78+
{
79+
cout << "LL is empty\n";
80+
return;
81+
}
82+
83+
Node *temp = head;
84+
while (temp->next != tail)
85+
{
86+
temp = temp->next;
87+
}
88+
89+
temp->next = NULL;
90+
delete tail;
91+
tail = temp;
92+
}
93+
94+
void insert(int val, int pos) // to insert or add a node at specific position
95+
{
96+
if (pos < 0)
97+
{
98+
cout << "Invalid position\n";
99+
return;
100+
}
101+
102+
if (pos == 0)
103+
{
104+
push_front(val);
105+
return;
106+
}
107+
108+
Node *temp = head;
109+
for (int i = 0; i < pos - 1; i++)
110+
{
111+
if (temp == NULL)
112+
{
113+
cout << "Invalid position\n";
114+
return;
115+
}
116+
117+
temp = temp->next;
118+
}
119+
120+
Node *newNode = new Node(val);
121+
newNode->next = temp->next;
122+
temp->next = newNode;
123+
}
124+
125+
void printLL() // to print linked list
45126
{
46127
Node *temp = head;
47128

48129
while (temp != NULL)
49130
{
50-
cout << temp->data << "->";
131+
cout << temp->data << " -> ";
51132
temp = temp->next;
52133
}
53134
cout << "NULL" << endl;
54135
}
136+
137+
int search(int key) // to search a node
138+
{
139+
Node *temp = head;
140+
int idx = 0;
141+
142+
while (temp != NULL)
143+
{
144+
if (temp->data == key)
145+
{
146+
return idx;
147+
}
148+
149+
temp = temp->next;
150+
idx++;
151+
}
152+
153+
return -1;
154+
}
55155
};
56156

57157
int main()
58158
{
59159
List ll;
60160

61-
ll.push_front(1);
62-
ll.push_front(2);
63161
ll.push_front(3);
162+
ll.push_front(2);
163+
ll.push_front(1);
164+
165+
ll.insert(4, 1);
64166

65167
ll.printLL();
66168

169+
cout << ll.search(2) << endl;
170+
67171
return 0;
68172
}

Linked-list/Linked-list-intro.exe

996 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)