0% found this document useful (0 votes)
35 views137 pages

Mids Lab

Uploaded by

Ayesha Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views137 pages

Mids Lab

Uploaded by

Ayesha Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 137

1

Mid term Programs

// This program defines a linked list data


structure and demonstrates inserting
elements at the beginning of the list and
displaying the list.
#include <iostream>

using namespace std;

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;
}

void insertAtStart(int val) {


Node* newNode = new Node(val);
if (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

// This Program implement a single linked list


and insert elements at the end
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;

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

//This program creates a linked list, inserts


elements at the start and end, displays the

A.K
U>M
8

list, and inserts an element at a specified


index.
#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
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

Node* newNode = new Node(val);


if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

void insertAtIndex(int prevData, int


newData) {
Node* newNode = new Node(newData);
Node* temp = head;

A.K
U>M
11

while (temp != NULL && temp->data !=


prevData) {
temp = temp->next;
}

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

cout << temp->data << " ";


temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtEnd(3);
list.insertAtStart(1);
list.insertAtEnd(4);
cout<<"Total no of elements in the
linkedlist:"<<endl;
list.display();
list.insertAtIndex(3, 7);

A.K
U>M
13

cout<<"Insertion at given index in the


linkedlist:"<<endl;
list.display();
return 0;
}
Output:
Total no of elements in the linkedlist:
1234
Insertion at given index in the linkedlist:
12374
// The program creates a singly linked list,
inserts elements at the end, displays the list
length, searches for an element, and prints
whether it is found or not.
#include <iostream>
using namespace std;

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

while (temp != NULL) {


if (temp->data == item) {
return true;
}
temp = temp->next;
}
return false;
}

void lengthList() {
int c = 0;
Node* temp = head;
while (temp != NULL) {
c++;
temp = temp->next;
}

A.K
U>M
17

cout << "Length of the list is: " << c <<


endl;
}

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

while (temp->next != NULL) {


temp = temp->next;
}
temp->next = newNode;
}
void reverseList() {
Node* temp = head;
Node* prev = NULL;
while (temp != NULL) {
Node* next = temp->next;
temp->next = prev;
prev = temp;
temp = next;
}
head = prev;
}

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;
}

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;

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

cout << "Delete the element from the start


of the LinkedList:" << endl;
list.display();
return 0;
}Output:
The total number of element in the LinkedList:
1234
Delete the element from the start of the
LinkedList:
234
// Deletion at Front and last in Linked List
#include <iostream>
using namespace std;

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 insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}

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

Node* prev = NULL;


Node* temp = head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
delete temp;
}

void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}

A.K
U>M
33

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();
cout << "Delete the element from the start
of the LinkedList:" << endl;
list.display();
list.deleteLast();

A.K
U>M
34

cout << "Delete the element from the end


of the LinkedList:" << endl;
list.display();
return 0;
}Output:
The total number of element in the LinkedList:
1234
Delete the element from the start of the
LinkedList:
234
Delete the element from the end of the
LinkedList:
23
//This program defines a LinkedList class to
manage a linked list, with methods for
insertion at the start, display, and sum
calculation, demonstrated by inserting

A.K
U>M
35

elements, displaying them, and calculating


their sum
#include <iostream>
using namespace std;

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;
}

void insertAtStart(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
} else {
newNode->next = head;
head = newNode;
}

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();

int totalSum = list.calculateSum();


cout << "Total sum: " << totalSum <<
endl;

A.K
U>M
39

return 0;
}Output: 6 5 2
Total sum: 13

//This program that finds the greatest number


in the linked list:
#include <iostream>
#include <climits>
using namespace std;
class Node {
public:
int data;
Node* next;

Node(int val) {
data = val;

A.K
U>M
40

next = NULL;
}
};

class LinkedList {
private:
Node* head;

public:
LinkedList() {
head = NULL;
}

void insertAtStart(int val) {


Node* newNode = new Node(val);
newNode->next = head;

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

maxVal = max(maxVal, temp->data);


temp = temp->next;
}
return maxVal;
}
};

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

// calculate the second highest value in the


single linked list:
#include <iostream>
#include <climits>
using namespace std;
class Node {
public:
int data;
Node* next;

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

if (temp->data > maxVal) {


secondMax = maxVal;
maxVal = temp->data;
} else if (temp->data > secondMax
&& temp->data != maxVal) {
secondMax = temp->data;
}
temp = temp->next;
}
return secondMax;
}
};

int main() {
LinkedList list;
list.insertAtStart(2);
list.insertAtStart(5);
list.insertAtStart(6);

int secondMax = list.findSecondMax();

if (secondMax != INT_MIN) {

A.K
U>M
46

cout << "The second highest value in


the linked list: " << secondMax << endl;
} else {
cout << "There is no second highest
value in the linked list." << endl;
}

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

cout << endl;


}
};

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

// Double Linked list insert at beginning

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

// Double linked list insert at end


#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = NULL;

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

// Singular circular linked list


#include <iostream>
using namespace std;
class Node {
public:
int data;

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

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);
list.insertAtEnd (40);
list.display();

A.K
U>M
60

return 0;
}Output:
10 20 30 40

//Write a C++ program that find the greatest


value in circular single linked list
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
head->next = head;

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

Node* temp = head;


do {
cout << temp->data << " ";
if (temp->data > max_value) {
max_value = temp->data;
}
temp = temp->next;
} while (temp != head);
cout << endl;
cout << "Greatest value in the circular
linked list: " << max_value << endl;
}
};

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

//This program creates a circular linked list,


inserts elements at the end, displays the list,
and counts the number of nodes.
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
head->next = head;
} else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;

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

//This program demonstrates the


implementation of a singly circular linked list,
including insertions at the end, deletion of the
first node, and displaying the list.
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;

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

} while (temp != head);


cout << endl;
}
};

int main() {
CircularLinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);

cout << "Original List: ";


list.display();

list.deleteFirst();

A.K
U>M
75

cout << "List after deleting the first node:


";
list.display();

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

//Delete the first node in the singly cicular


linked list
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;

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);

cout << "Original List: ";


list.display();

list.deleteFirst();

cout << "List after deleting the first node:


";
list.display();

return 0;
}Output:
Original List: 10 20 30 40
List after deleting the first node: 20 30 40

A.K
U>M
85

//Delete the last node in the singly cicular


linked list
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
head->next = head;
}
else {
Node* temp = head;

A.K
U>M
87

while (temp->next != head) {


temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}

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;
}

Node* temp = head;


while (temp->next->next != head) {
temp = temp->next;
}

Node* lastNode = temp->next;


temp->next = head;
delete lastNode;
}

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);

cout << "Original List: ";


list.display();

list.deleteLast();

cout << "List after deleting the last node:


";
list.display();

return 0;
}
Output:
Original List: 10 20 30 40
List after deleting the last node: 10 20 30

A.K
U>M
91

//The program implements a singly Circular


Linked List data structure in C++ with
operations to insert nodes at the end, delete
nodes by value, and display the contents of
the list.
#include <iostream>
using namespace std;

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;
}

void insertAtEnd(int val) {


Node* newNode = new Node(val);
if (head == NULL) {
head = newNode;
head->next = head;
}

A.K
U>M
93

else {
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}

void deleteAtIndex(int val) {


if (head == NULL) {
cout << "List is empty. Nothing to
delete." << endl;
return;
}

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

Node* prev = NULL;


Node* temp = head;
while (temp->next != head && temp-
>data != val) {
prev = temp;
temp = temp->next;
}
if (temp->data != val) {
cout << "Value not found in the list."
<< endl;
return;
}
prev->next = temp->next;
delete temp;
}

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);

cout << "Original List: ";


list.display();

list.deleteAtIndex(10);
list.deleteAtIndex(30);

cout << "List after deleting the node with


value 10 and 30: "<<endl;
list.display();

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

// In this program using stack overflow and


underflow calling in the main part
#include<iostream>
using namespace std;

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

//Write a C++ program that implements the


dynamic stack.
#include <iostream>
using namespace std;

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;
}

Node* newNode = new Node(x);


newNode->next = top;
top = newNode;
count++;
}

void pop() {
if (top == NULL) {
cout << "Stack Underflow" << endl;

A.K
U>M
108

return;
}

Node* temp = top;


top = top->next;
delete temp;
count--;
}

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

int val = s.peek();


cout << val << " ";
s.pop();
}

s.pop();

return 0;
}
Output:
Stack Overflow
5 4 3 2 1 Stack Underflow

//This C++ program implements a circular


queue with enqueue, dequeue, and display
operations, showcasing FIFO data
management using a fixed-size array.

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

cout << "Queue is Empty" << endl;


return;
}
int i = front;
for (int j = 0; j < size; j++) {
cout << qu[i] << " ";
i = (i + 1) % capacity;
}
cout << endl;
}
};

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

//The program implements a queue data


structure using a linked list, allowing for
dynamic resizing and efficient enqueue and
dequeue operations.

#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);
}

void enqueue(int data) {


Node* newNode = new Node(data);
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}

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

cout << "Dequeued element: " << item


<< endl;
}

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

Node* newNode = new Node(val);


if (head == NULL) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

/* Explain the algorithm of find common


element function.
The key points to understand are:

A.K
U>M
125

 The function compares the values


at the head pointers of the two
input lists.
 If the values are equal, it adds the
common value to the third list.
 If the values are not equal, it
moves the pointer of the list with
the smaller value to the next node.
 The loop continues until one of the
lists reaches the end.
Why we use static keyword in find
common element function?
--> In this case, the static keyword
is used to make the findCommonElements
function a class-level function rather than an
instance-level function. This is done for the
following reasons: Accessibility: A
static function can be called without creating
an instance of the class.

A.K
U>M
126

Memory Efficiency: Static


functions don't require a reference to the
class instance, saving memory.
Encapsulation: Static functions
can access the private members of the class,
maintaining encapsulation.*/

static void findCommonElements(LinkedList&


list1, LinkedList& list2, LinkedList& list3) {

Node* head1 = list1.head;


Node* head2 = list2.head;

while (head1 != NULL && head2 != NULL) {

if (head1->data < head2->data) {


head1 = head1->next;
}

A.K
U>M
127

else if (head2->data < head1->data) {


head2 = head2->next;
}
else {
list3.insertAtEnd(head1->data);
head1 = head1->next;
head2 = head2->next;
}
}
}
void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}

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);

cout << "Common elements: ";


list3.display();

return 0;
}
Output:
Common elements: 3 7

A.K
U>M
130

// write a C++ program that counts are


number of nodes in a circular linked list if
number of nodes in linked list are even
display the linked list in forward order
otherwise display in reverse order

Is program ki output sahi nahi arahi


😭😭😭😭😭
Output ye arahi han 7 1 2 3 4 5 6 reverse
mein nahi ja raha forward mein theak han🤕 🤕
🤕

//write a program that push and number of


values from the linked list into the stack

#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;
}

Node* newNode = new Node(x);


newNode->next = top;
top = newNode;

A.K
U>M
133

count++;
}

void pop() {
if (top == NULL) {
cout << "Stack Underflow" << endl;
return;
}

Node* temp = top;


top = top->next;
delete temp;
count--;
}

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

head->next = new Node(2);


head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new
Node(5);
cout << "Creating a stack with a maximum
size of 5" << endl;
Stack s(5);
cout << "Pushing the values from the
linked list into the stack:" << endl;
Node* temp = head;
while (temp != NULL) {
s.push(temp->data);
cout << "Pushed value: " << temp-
>data << endl;
temp = temp->next;
}

A.K
U>M
136

cout << "Displaying the values in the


stack:" << endl;
while (!s.isEmpty()) {
int val = s.peek();
cout << val << " ";
s.pop();
}
cout << endl;

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

You might also like