Mids Lab
Mids Lab
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
A.K
U>M
2
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
A.K
U>M
3
head = newNode;
return;
}
newNode->next = head;
head = newNode;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}
};
A.K
U>M
4
int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtStart(5);
list.insertAtStart(6);
list.display();
return 0;
} Output: 6 5 2
A.K
U>M
5
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
A.K
U>M
6
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
A.K
U>M
7
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(3);
list.insertAtEnd(4);
list.display();
return 0;
} Output: 1234
A.K
U>M
8
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
A.K
U>M
9
Node* head;
public:
LinkedList() {
head = NULL;
}
void insertAtStart(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
return;
}
newNode->next = head;
head = newNode;
}
void insertAtEnd(int val) {
A.K
U>M
10
A.K
U>M
11
if (temp == NULL) {
cout << "Node with previous data not
found." << endl;
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
void display() {
Node* temp = head;
while (temp != NULL) {
A.K
U>M
12
A.K
U>M
13
A.K
U>M
14
class Node {
public:
int data;
Node* next;
Node(int val){
data = val;
next = NULL;
}
};
class LinkedList {
public:
Node* head;
LinkedList(){
A.K
U>M
15
head = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
bool searchElement(int item) {
Node* temp = head;
A.K
U>M
16
void lengthList() {
int c = 0;
Node* temp = head;
while (temp != NULL) {
c++;
temp = temp->next;
}
A.K
U>M
17
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtEnd(1);
A.K
U>M
18
list.insertAtEnd(2);
list.insertAtEnd(3);
list.insertAtEnd(4);
cout << "The total number of elements in
the LinkedList:" << endl;
list.display();
bool x = list.searchElement(4);
if (x == true) {
cout << "Found" << endl;
} else {
cout << "Not found" << endl;
}
list.lengthList();
return 0;
}Output:
The total number of elements in the
LinkedList:
A.K
U>M
19
1234
Found
Length of the list is: 4
// This program creates a linked list, inserts
elements at the end, displays the total
number of elements, reverses the list, and
displays the reversed list.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val){
data = val;
A.K
U>M
20
next = NULL;
}
};
class LinkedList {
public:
Node* head;
LinkedList(){
head = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
A.K
U>M
21
A.K
U>M
22
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(3);
A.K
U>M
23
list.insertAtEnd(4);
cout << "The total number of elements in
the LinkedList:" << endl;
list.display();
list.reverseList();
cout << "the elements in the linked list
become reversed:" << endl;
list.display();
return 0;
}Output:
The total number of elements in the
LinkedList:
1234
the elements in the linked list become
reversed:
4321
//Deletion at Front in Linked List
A.K
U>M
24
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
A.K
U>M
25
Node* head;
public:
LinkedList() {
head = NULL;
}
A.K
U>M
26
}
temp->next = newNode;
}
void deleteFront() {
if (head == NULL)
return;
Node* temp = head;
head = head->next;
delete temp;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
A.K
U>M
27
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(3);
list.insertAtEnd(4);
cout << "The total number of element in
the LinkedList:" << endl;
list.display();
list.deleteFront();
A.K
U>M
28
class Node {
public:
int data;
A.K
U>M
29
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
A.K
U>M
30
void deleteFront() {
if (head == NULL) {
A.K
U>M
31
return;
}
Node* temp = head;
head = head->next;
delete temp;
}
void deleteLast() {
if (head == NULL) {
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
A.K
U>M
32
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
A.K
U>M
33
A.K
U>M
34
A.K
U>M
35
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
A.K
U>M
36
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
A.K
U>M
37
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int calculateSum() {
int sum = 0;
Node* temp = head;
while (temp != NULL) {
sum += temp->data;
A.K
U>M
38
temp = temp->next;
}
return sum;
}
};
int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtStart(5);
list.insertAtStart(6);
list.display();
A.K
U>M
39
return 0;
}Output: 6 5 2
Total sum: 13
Node(int val) {
data = val;
A.K
U>M
40
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
A.K
U>M
41
head = newNode;
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int findMax() {
int maxVal = INT_MIN;
Node* temp = head;
while (temp != NULL) {
A.K
U>M
42
int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtStart(5);
list.insertAtStart(6);
list.display();
int maxNumber = list.findMax();
if (maxNumber != INT_MIN) {
cout << "The greatest number in the
list: " << maxNumber << endl;
A.K
U>M
43
} else {
cout << "List is empty." << endl;
}
return 0;
}Output:
652
The greatest number in the list: 6
A.K
U>M
44
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
void insertAtStart(int val) {
Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
}
int findSecondMax() {
int maxVal = INT_MIN;
int secondMax = INT_MIN;
Node* temp = head;
while (temp != NULL) {
A.K
U>M
45
int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtStart(5);
list.insertAtStart(6);
if (secondMax != INT_MIN) {
A.K
U>M
46
return 0;
}
Output:The second highest value in the linked
list: 5
//DoubleLinked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node* prev;
A.K
U>M
47
Node(int val) {
data = val;
next = NULL;
prev = NULL;
}
};
class DoubleLinkedList {
public:
Node* head;
Node* tail;
DoubleLinkedList() {
head = NULL;
tail = NULL;
}
void insertAtBeginning(int val) {
Node* newNode = new Node(val);
A.K
U>M
48
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
A.K
U>M
49
int main() {
DoubleLinkedList list;
list.insertAtBeginning (10);
list.insertAtBeginning (20);
list.insertAtBeginning (30);
list.insertAtBeginning (40);
list.display();
return 0;
}Output: 10, 20, 30, 40
A.K
U>M
50
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = NULL;
prev = NULL;
}
};
class DoubleLinkedList {
public:
Node* head;
A.K
U>M
51
Node* tail;
DoubleLinkedList() {
head = NULL;
tail = NULL;
}
void insertAtBeginning(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
tail = newNode;
}
else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
A.K
U>M
52
}
void display() {
Node* temp = tail;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->prev;
}
cout << endl;
}
};
int main() {
DoubleLinkedList list;
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
A.K
U>M
53
list.insertAtBeginning(40);
list.display();
return 0;
}Output: 10 20 30 40
A.K
U>M
54
prev = NULL;
}
};
class DoubleLinkedList {
public:
Node* head;
Node* tail;
DoubleLinkedList() {
head = NULL;
tail = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (tail == NULL) {
head = newNode;
tail = newNode;
A.K
U>M
55
}
else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
A.K
U>M
56
int main() {
DoubleLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.display();
return 0;
}Output: 10 20 30 40
A.K
U>M
57
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
A.K
U>M
58
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
A.K
U>M
59
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd (20);
list.insertAtEnd (30);
list.insertAtEnd (40);
list.display();
A.K
U>M
60
return 0;
}Output:
10 20 30 40
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
A.K
U>M
61
}
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = NULL;
}
A.K
U>M
62
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
int max_value = head->data;
A.K
U>M
63
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
A.K
U>M
64
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
cout<<"Total number of elements in the
linked list:"<<endl;
list.display();
return 0;
}Output:
Total number of elements in the linked list:
10 20 30 40
Greatest value in the circular linked list: 40
A.K
U>M
65
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class CircularLinkedList {
public:
Node* head;
A.K
U>M
66
CircularLinkedList() {
head = NULL;
}
A.K
U>M
67
}
}
int countNodes() {
Node* temp = head;
int result = 0;
if (head != NULL) {
do {
temp = temp->next;
result++;
} while (temp != head);
}
return result;
}
void display() {
A.K
U>M
68
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
A.K
U>M
69
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.display();
cout << "Number of nodes: " <<
list.countNodes() << endl;
return 0;
}Output:
10 20 30 40
Number of nodes: 4
A.K
U>M
70
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class CircularLinkedList {
public:
Node* head;
A.K
U>M
71
CircularLinkedList() {
head = NULL;
}
A.K
U>M
72
newNode->next = head;
}
}
void deleteFirst() {
if (head == NULL) {
cout << "List is empty. Nothing to
delete." << endl;
return;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
if (temp == head) {
head = NULL;
}
A.K
U>M
73
else {
temp->next = head->next;
delete head;
head = temp->next;
}
}
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
A.K
U>M
74
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.deleteFirst();
A.K
U>M
75
return 0;
}Output:
Original List: 10 20 30 40
List after deleting the first node: 20 30 40
// The program implements a Double Circular
Linked List data structure in C++
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
A.K
U>M
76
Node* prev;
Node(int val) {
data = val;
next = NULL;
prev = NULL;
}
};
class DoubleCircularLinkedList {
public:
Node* head;
DoubleCircularLinkedList() {
head = NULL;
}
void insertAtEnd(int val) {
Node* newNode = new Node(val);
if (head == NULL) {
A.K
U>M
77
head = newNode;
head->next = head;
head->prev = head;
}
else {
Node* lastNode = head->prev;
lastNode->next = newNode;
newNode->prev = lastNode;
newNode->next = head;
head->prev = newNode;
}
}
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
return;
A.K
U>M
78
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
DoubleCircularLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd (20);
list.insertAtEnd (30);
list.insertAtEnd (40);
A.K
U>M
79
list.display();
return 0;
}Output:
10 20 30 40
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
A.K
U>M
80
next = NULL;
}
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = NULL;
}
A.K
U>M
81
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
void deleteFirst() {
if (head == NULL) {
cout << "List is empty. Nothing to
delete." << endl;
return;
A.K
U>M
82
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
if (temp == head) {
head = NULL;
}
else {
temp->next = head->next;
delete head;
head = temp->next;
}
}
void display() {
A.K
U>M
83
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
A.K
U>M
84
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.deleteFirst();
return 0;
}Output:
Original List: 10 20 30 40
List after deleting the first node: 20 30 40
A.K
U>M
85
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
A.K
U>M
86
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = NULL;
}
A.K
U>M
87
void deleteLast() {
if (head == NULL) {
cout << "List is empty. Nothing to
delete." << endl;
return;
}
if (head->next == head) {
delete head;
A.K
U>M
88
head = NULL;
return;
}
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
A.K
U>M
89
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
A.K
U>M
90
list.insertAtEnd(40);
list.deleteLast();
return 0;
}
Output:
Original List: 10 20 30 40
List after deleting the last node: 10 20 30
A.K
U>M
91
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
A.K
U>M
92
};
class CircularLinkedList {
public:
Node* head;
CircularLinkedList() {
head = NULL;
}
A.K
U>M
93
else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
A.K
U>M
94
if (head->data == val) {
if (head->next == head) {
delete head;
head = NULL;
return;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = head->next;
Node* toDelete = head;
head = head->next;
delete toDelete;
return;
}
A.K
U>M
95
A.K
U>M
96
void display() {
if (head == NULL) {
cout << "List is empty." << endl;
return;
}
Node* temp = head;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
cout << endl;
}
};
int main() {
CircularLinkedList list;
A.K
U>M
97
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.deleteAtIndex(10);
list.deleteAtIndex(30);
return 0;
}
A.K
U>M
98
Output:
Original List: 10 20 30 40
List after deleting the node with value 10 and
30:
20 40
//This program implements a stack using an
array, allowing pushing, popping, and peeking
elements. It demonstrates these operations
by filling the stack and then emptying it while
displaying each element.
#include<iostream>
using namespace std;
class Stack {
int top;
int size;
public:
int a[5];
A.K
U>M
99
Stack(int n) {
size = n;
top = -1;
}
void push(int x) {
if(top >= size-1) {
cout<<"Stack Overflow\n";
return;
}
a[++top] = x;
}
void pop() {
if(top < 0) {
cout<<"Stack Underflow\n";
return;
}
A.K
U>M
100
top--;
}
int peek() {
return a[top];
}
int isEmpty() {
if(top < 0){
return true;
}
return false;
}
};
int main() {
Stack s(5);
s.push(1);
A.K
U>M
101
s.push(2);
s.push(3);
s.push(4);
s.push(5);
while(!s.isEmpty()) {
int val = s.peek();
cout<<val<<" ";
s.pop();
}
return 0;
}Output:
54321
A.K
U>M
102
class Stack {
int top;
int size;
public:
int a[5];
Stack(int n) {
size = n;
top = -1;
}
void push(int x) {
if(top >= size-1) {
cout<<"Stack Overflow\n";
return;
}
a[++top] = x;
A.K
U>M
103
}
void pop() {
if(top < 0) {
cout<<"Stack Underflow\n";
return;
}
top--;
}
int peek() {
return a[top];
}
int isEmpty() {
if(top < 0){
return true;
}
return false;
A.K
U>M
104
}
};
int main() {
Stack s(5);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
while(!s.isEmpty()) {
int val = s.peek();
cout<<val<<" ";
s.pop();
A.K
U>M
105
s.pop();
return 0;
}Output:
Stack Overflow
5 4 3 2 1 Stack Underflow
class Node {
public:
int data;
Node* next;
Node(int val) {
A.K
U>M
106
data = val;
next = NULL;
}
};
class Stack {
Node* top;
int size;
int count;
public:
Stack(int n) {
size = n;
count = 0;
top = NULL;
}
A.K
U>M
107
void push(int x) {
if (count >= size) {
cout << "Stack Overflow" << endl;
return;
}
void pop() {
if (top == NULL) {
cout << "Stack Underflow" << endl;
A.K
U>M
108
return;
}
int peek() {
if (top == NULL) {
cout << "Stack is empty" << endl;
return -1;
}
return top->data;
}
A.K
U>M
109
bool isEmpty() {
return top == NULL;
}
};
int main() {
Stack s(5);
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
while (!s.isEmpty()) {
A.K
U>M
110
s.pop();
return 0;
}
Output:
Stack Overflow
5 4 3 2 1 Stack Underflow
A.K
U>M
111
#include <iostream>
using namespace std;
class Queue {
private:
int qu[5];
int size;
int front;
int rear;
int capacity;
public:
Queue() {
size = 0;
front = 0;
rear = 0;
A.K
U>M
112
capacity = 5;
}
bool isFull() {
if (size == capacity) return true;
else return false;
}
bool isEmpty() {
if (size == 0) return true;
else return false;
}
void enqueue(int data) {
if (isFull()) {
cout << "Queue is Full" << endl;
return;
}
A.K
U>M
113
qu[rear] = data;
rear = (rear + 1) % capacity;
size++;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
int item = qu[front];
front = (front + 1) % capacity;
size--;
cout << "Dequeued element: " << item
<< endl;
}
void display() {
if (size == 0) {
A.K
U>M
114
int main() {
Queue q1;
q1.enqueue(1);
q1.enqueue(2);
A.K
U>M
115
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.display();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.display();
return 0;
}
Output:
12345
Dequeued element: 1
A.K
U>M
116
Dequeued element: 2
Dequeued element: 3
Dequeued element: 4
Dequeued element: 5
Queue is Empty
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
A.K
U>M
117
Node(int value) {
data = value;
next = NULL;
}
};
class Queue {
private:
Node* front;
Node* rear;
int size;
public:
Queue() {
front = NULL;
A.K
U>M
118
rear = NULL;
size = 0;
}
bool isEmpty() {
return (front == NULL);
}
A.K
U>M
119
size++;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
int item = front->data;
Node* temp = front;
front = front->next;
if (front == NULL) {
rear = NULL;
}
delete temp;
size--;
A.K
U>M
120
void display() {
if (isEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
Node* temp = front;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
A.K
U>M
121
int main() {
Queue q1;
q1.enqueue(1);
q1.enqueue(2);
q1.enqueue(3);
q1.enqueue(4);
q1.enqueue(5);
q1.display();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.dequeue();
q1.display();
A.K
U>M
122
return 0;
}Output:
12345
Dequeued element: 1
Dequeued element: 2
Dequeued element: 3
Dequeued element: 4
Dequeued element: 5
Queue is Empty
//Write a program that takes two linked list
and display the common elements in the third
linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
A.K
U>M
123
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() {
head = NULL;
}
void insertAtEnd(int val) {
A.K
U>M
124
A.K
U>M
125
A.K
U>M
126
A.K
U>M
127
A.K
U>M
128
};
int main() {
LinkedList list1;
list1.insertAtEnd(1);
list1.insertAtEnd(3);
list1.insertAtEnd(5);
list1.insertAtEnd(7);
list1.insertAtEnd(9);
LinkedList list2;
list2.insertAtEnd(2);
list2.insertAtEnd(3);
list2.insertAtEnd(6);
A.K
U>M
129
list2.insertAtEnd(7);
list2.insertAtEnd(10);
LinkedList list3;
LinkedList::findCommonElements(list1,
list2, list3);
return 0;
}
Output:
Common elements: 3 7
A.K
U>M
130
#include <iostream>
using namespace std;
A.K
U>M
131
class Node {
public:
int data;
Node* next;
Node(int val) {
data = val;
next = NULL;
}
};
class Stack {
Node* top;
int size;
int count;
A.K
U>M
132
public:
Stack(int n) {
size = n;
count = 0;
top = NULL;
}
void push(int x) {
if (count >= size) {
cout << "Stack Overflow" << endl;
return;
}
A.K
U>M
133
count++;
}
void pop() {
if (top == NULL) {
cout << "Stack Underflow" << endl;
return;
}
int peek() {
A.K
U>M
134
if (top == NULL) {
cout << "Stack is empty" << endl;
return -1;
}
return top->data;
}
bool isEmpty() {
return top == NULL;
}
};
int main() {
cout << "Creating a linked list with the
following values: 1 -> 2 -> 3 -> 4 -> 5" <<
endl;
Node* head = new Node(1);
A.K
U>M
135
A.K
U>M
136
return 0;
}
Output:
Creating a linked list with the following
values: 1 -> 2 -> 3 -> 4 -> 5
Creating a stack with a maximum size of 5
Pushing the values from the linked list into
the stack:
A.K
U>M
137
Pushed value: 1
Pushed value: 2
Pushed value: 3
Pushed value: 4
Pushed value: 5
Displaying the values in the stack:
54321
A.K
U>M