INDEX
INDEX
i
4. Program to implement singly Linked list
#include <iostream>
struct Node {
int data;
Node* next;
};
class SinglyLinkedList {
public:
Node* head;
SinglyLinkedList() : head(nullptr) {}
newNode->data = value;
newNode->next = head;
head = newNode;
void deleteFirst() {
if (head) {
head = head->next;
delete temp;
ii
void display() {
while (temp) {
temp = temp->next;
};
int main() {
SinglyLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
list.deleteFirst();
cout << "Singly Linked List after deleting the first element: ";
list.display();
return 0;
Output:
iii
iv
5. Program to implement doubly linked list
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev;
};
class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
newNode->data = value;
newNode->next = head;
newNode->prev = nullptr;
head = newNode;
void deleteFirst() {
if (head) {
head = head->next;
v
if (head) head->prev = nullptr;
delete temp;
void display() {
while (temp) {
temp = temp->next;
};
int main() {
DoublyLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
list.deleteFirst();
cout << "Doubly Linked List after deleting the first element: ";
vi
list.display();
return 0;
Output:
vii
6. Program to implement circular Linked list
#include <iostream>
struct Node {
int data;
Node* next;
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() : head(nullptr) {}
newNode->data = value;
if (!head) {
head = newNode;
newNode->next = head;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = head;
viii
}
void display() {
if (!head) return;
do {
temp = temp->next;
};
int main() {
CircularLinkedList list;
// Inserting elements
list.insert(10);
list.insert(20);
list.insert(30);
list.display();
return 0;
Output:
ix
7. Implement Stack using singly Linked list
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
public:
Node* top;
Stack() : top(nullptr) {}
newNode->data = value;
newNode->next = top;
top = newNode;
void pop() {
if (top) {
top = top->next;
delete temp;
x
void display() {
while (temp) {
temp = temp->next;
};
int main() {
Stack stack;
stack.push(10);
stack.push(20);
stack.push(30);
stack.display();
stack.pop();
stack.display();
return 0;
Output:
xi
xii
8. Implement Queue using linked list
#include <iostream>
struct Node {
int data;
Node* next;
};
class Queue {
public:
newNode->data = value;
newNode->next = nullptr;
if (!rear) {
} else {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (front) {
xiii
Node* temp = front;
front = front->next;
delete temp;
void display() {
while (temp) {
temp = temp->next;
};
int main() {
Queue queue;
// Enqueueing elements
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.display();
// Dequeueing an element
xiv
queue.dequeue();
queue.display();
return 0;
Output:
xv