Ibrahim Iqbal DS Lab Manuals 1 to 7
Ibrahim Iqbal DS Lab Manuals 1 to 7
MANAGEMENT &
TECHNOLOGY
1
Contents
LAB MANUAL #01 (Stacks using arrays).......................................................................................................3
LAB MANUAL#02 (Infix, Prefix & Postfix)...............................................................................................15
Lab Manual#03 (Queue Using Array).................................................................................................32
2
LAB MANUAL #01 (Stacks using arrays)
Lab Task #1: Implement a stack using an array in C++ with the following functions: • push(): Adds an
item to the top of the stack.
• pop(): Removes and returns the top item from the stack.
• search(): Searches for an item in the stack and returns its index.
C++ Code:
#include <iostream>
#include <limits>
class stack{
private:
int top;
int arr[10];
int index;
int temp;
public:
stack(){
top=-1;
3
if(top==10-1){
}else{
top++;
arr[top]=element;
void pop(){
if(top==-1){
}else{
top--;
void traverse(){
if(top==-1){
}else{
for(int i=top;i>=0;i--){
cout<<arr[i]<<" ";
4
}
if(top==-1){
return;
for(int i=0;i<=top;i++){
for(int j=0;j<top-i;j++){
int temp=arr[j];
arr[j]=arr[j + 1];
arr[j+1]=temp;
for(int i=0;i<=top;i++){
cout<<arr[i]<<" ";
cout<<endl;
void search(){
if(top==-1){
}else{
5
cout<<"Enter the index value to search for embedded value (0-9)";
cin>>index;
}else{
void minVal(){
if(top==-1){
}else{
int minValue=numeric_limits<int>::max();
for(int i=0;i<=top;i++){
if(arr[i]<minValue){
minValue=arr[i];
void maxVal(){
if(top==-1){
6
}else{
int maxValue=numeric_limits<int>::min();
for(int i=0;i<=top;i++){
if(arr[i]>maxValue){
maxValue=arr[i];
};
int main(){
stack s;
while(true){
cout<<"Press 1 to push."<<endl;
cout<<"Press 2 to pop."<<endl;
cout<<"Press 3 to traverse."<<endl;
cout<<"Press 9 to exit."<<endl;
7
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
cin>>element;
s.push(element);
break;
case 2:
s.pop();
break;
case 3:
s.traverse();
break;
case 4:
s.sort(false);
break;
case 5:
s.sort(true);
break;
case 6:
s.search();
break;
case 7:
8
s.maxVal();
break;
case 8:
s.minVal();
break;
case 9:
return 0;
break;
default:
Result:
9
Lab Task #2: Write a function to identify and display any duplicate data in the stack along with their
indices.
C++ code:
#include <iostream>
int top=-1;
int arr[6];
if(top<6){
top++;
arr[top]=element;
}else{
cout<<"Stack is full."<<endl;
10
}
void duplicate(){
for(int i=0;i<=top;i++){
for(int j=i+1;j<=top;j++){
if(arr[i]==arr[j]){
int main(){
insert(34);
insert(56);
insert(90);
insert(34);
insert(56);
insert(90);
duplicate();
Results:
11
Lab task #3: Write a C++ program that:
• Stores the sorted elements in another stack named sortedStack such that the smallest elements are
on the top of sortedStack.
C++ Code:
#include <iostream>
int j = i - 1;
12
sortedStack[j + 1] = sortedStack[j];
j--;
sortedStack[j + 1] = temp;
int main() {
int n;
cin >> n;
sortStack(inputStack, n, sortedStack);
13
cout << inputStack[i] << " ";
return 0;
Results:
14
LAB MANUAL#02 (Infix, Prefix & Postfix)
Lab task#1:
Write code for converting an infix expression to a postfix expression using a stack. This program will
prompt the user to enter an infix expression.
C++ Code:
#include <iostream>
class Stack {
private:
int top;
char arr[20];
public:
Stack() {
top = -1;
void push(char a) {
arr[++top] = a;
char pop() {
return arr[top--];
15
char top_element() {
return arr[top];
bool empty() {
};
int precedence(char c) {
if (c == '^')
return 3;
return 2;
return 1;
else
return -1;
void infixToPostfix(char* s) {
Stack st;
char result[20];
int k = 0;
16
for (int i = 0; s[i] != '\0'; i++) {
char c = s[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result[k++] = c;
else if (c == '(') {
st.push('(');
else if (c == ')') {
result[k++] = st.pop();
st.pop();
else {
result[k++] = st.pop();
17
st.push(c);
while (!st.empty()) {
result[k++] = st.pop();
result[k] = '\0';
int main() {
char exp[20];
infixToPostfix(exp);
return 0;
Result:
18
Lab Task#2:
Write C++ program to convert infix expression to a prefix expression using a stack.
C++ Code:
#include <iostream>
class Stack {
private:
int top;
char arr[20];
public:
Stack() {
19
top = -1;
void push(char a) {
arr[++top] = a;
char pop() {
return arr[top--];
char top_element() {
return arr[top];
bool empty() {
};
int precedence(char c) {
if (c == '^')
return 3;
20
return 2;
return 1;
else
return -1;
exp[length - i - 1] = temp;
int len = 0;
len++;
return len;
21
void infixToPrefix(char* s) {
Stack st;
char result[20];
reverse(s, len);
if (s[i] == '(') {
s[i] = ')';
s[i] = '(';
int k = 0;
char c = s[i];
22
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result[k++] = c;
else if (c == '(') {
st.push(c);
else if (c == ')') {
result[k++] = st.pop();
st.pop();
else {
result[k++] = st.pop();
st.push(c);
while (!st.empty()) {
23
result[k++] = st.pop();
result[k] = '\0';
reverse(result, k);
int main() {
char exp[20];
infixToPrefix(exp);
return 0;
Result:
24
Lab Task #3:
C++ Code:
25
#include <iostream>
class Stack {
private:
int top;
char arr[20];
public:
Stack() {
top = -1;
void push(char a) {
arr[++top] = a;
char pop() {
return arr[top--];
char top_element() {
return arr[top];
26
bool empty() {
};
int precedence(char c) {
if (c == '!')
return 5;
return 4;
return 3;
return 2;
return 1;
else
return 0;
27
exp[i] = exp[length - i - 1];
exp[length - i - 1] = temp;
int len = 0;
len++;
return len;
void infixToPrefix(char* s) {
Stack st;
char result[20];
reverse(s, len);
28
if (s[i] == '(') {
s[i] = ')';
s[i] = '(';
int k = 0;
char c = s[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
result[k++] = c;
else if (c == '(') {
st.push(c);
else if (c == ')') {
result[k++] = st.pop();
29
}
st.pop();
else {
result[k++] = st.pop();
st.push(c);
while (!st.empty()) {
result[k++] = st.pop();
result[k] = '\0';
reverse(result, k);
30
int main() {
char exp[20];
infixToPrefix(exp);
return 0;
Result:
_____________________________________________________________________________________
31
Lab Manual#03 (Queue Using Array)
Lab Task 1: Write a C++ program that implements a simple queue with enqueue, dequeue, and display
operations.
Code:
#include <iostream>
#define n 5
class queue{
private:
int* arr;
int front;
int back;
public:
queue(){
front=-1;
back=-1;
arr=new int[n];
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
if(back==n-1){
return;
}else{
32
back++;
arr[back]=a;
if(front==-1){
front++;
void pop(){
if(front==-1 || front>back){
return;
}else{
front++;
cout<<" "<<endl;
void peek(){
if(front==-1 || front>back){
return;
}else{
cout<<arr[front]<<endl;
33
void display(){
cout<<arr[i]<<" ";
};
int main(){
queue obj;
obj.push(8);
obj.push(34);
obj.push(12);
obj.peek();
obj.display();
obj.pop();
obj.display();
Result:
34
Lab task 2: Modify the simple queue to include a shifting mechanism that shifts elements to the
beginning of the array when space is available at the start, maximizing the queue’s usable space.
Code:
#include <iostream>
class Queue {
private:
int* arr;
public:
Queue(int size) {
cout<<”Ibrahim Iqbal”<<endl;
cout<<”23018020022”<<endl;
capacity = size;
~Queue() {
delete[] arr;
bool isEmpty() {
35
}
bool isFull() {
if (isFull()) {
cout << "Queue is full, cannot enqueue " << value << endl;
return;
if (isEmpty()) {
front = rear = 0;
} else {
arr[rear] = value;
void dequeue() {
if (isEmpty()) {
36
cout << "Queue is empty, nothing to dequeue." << endl;
return;
if (front == rear) {
} else {
int peek() {
if (isEmpty()) {
return -1;
return arr[front];
37
void display() {
if (isEmpty()) {
return;
};
int main() {
Queue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.display();
38
q.dequeue();
q.display();
q.enqueue(60);
q.display();
q.dequeue();
q.display();
q.enqueue(70);
q.display();
return 0;
Result:
39
Lab Task 3: Implement a circular queue using a fixed-size array, supporting circular insertion and
deletion.
Code:
#include <iostream>
#define n 5
class cirq{
int* arr;
int front;
int back;
int cap;
public:
cirq(){
40
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
front=-1;
back=-1;
arr=new int[n];
cap=n;
if((back+1)%cap==front){
return;
}else{
if(front==-1){
front=0;
back=(back+1)%cap;
arr[back]=a;
return;
void dequeue(){
if(front==-1){
41
cout<<"Queue is empty."<<endl;
return;
}else if(front==back){
front=-1;
back=-1;
}else{
front=(front+1)%cap;
void peek(){
if(front==-1){
return;
}else{
return;
void display(){
if(front==-1){
cout<<"Queue is empty"<<endl;
return;
}else{
42
cout<<"The elements in queue are: "<<endl;
for(int i=front;i!=back;i=(i+1)%cap){
cout<<arr[i]<<" ";
cout<<arr[back]<<endl;
};
int main(){
cirq obj;
obj.enqueue(12);
obj.display();
obj.enqueue(34);
obj.display();
obj.enqueue(67);
obj.display();
obj.enqueue(56);
obj.display();
obj.enqueue(45);
obj.peek();
obj.display();
obj.enqueue(32);
obj.display();
obj.dequeue();
obj.display();
43
obj.dequeue();
obj.display();
obj.dequeue();
obj.peek();
obj.display();
obj.dequeue();
obj.display();
obj.dequeue();
obj.display();
obj.dequeue();
obj.display();
obj.enqueue(78);
obj.display();
obj.enqueue(98);
obj.display();
obj.enqueue(67);
obj.display();
obj.enqueue(13);
obj.display();
Result:
44
Lab Task 4: Write code for a simple queue that automatically sorts elements during insertion. Ensure
that the queue efficiently uses any available empty space by shifting elements when necessary, and
demonstrate the queue’s sorted behavior by enqueuing and dequeuing elements.
Code:
#include <iostream>
#include <algorithm>
class SortedQueue {
private:
int *arr;
public:
45
SortedQueue(int size) {
cout<<”Ibrahim Iqbal”<<endl;
cout<<”23018020022”<<endl;
capacity = size;
front = -1;
rear = -1;
~SortedQueue() {
delete[] arr;
bool isEmpty() {
bool isFull() {
46
void enqueue(int value) {
if (isFull()) {
cout << "Queue is full. Cannot enqueue " << value << endl;
return;
if (front == -1) {
front = 0;
rear++;
arr[rear] = value;
void dequeue() {
if (isEmpty()) {
return;
47
}
rear--;
if (rear == -1) {
front = -1;
int peek() {
if (isEmpty()) {
return -1;
48
return arr[front];
void display() {
if (isEmpty()) {
return;
int size() {
if (isEmpty()) return 0;
};
49
int main() {
SortedQueue q(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(5);
q.enqueue(25);
q.display();
q.dequeue();
q.dequeue();
q.display();
q.enqueue(15);
50
q.enqueue(12);
q.display();
return 0;
Result:
Lab Task 5: Use your circular queue implementation to simulate three real-life scenarios where circular
queues are commonly used. For each scenario, add comments to explain how the circular queue’s
properties apply to that situation.
Situations used:
3}Printer pool
51
Code:
#include <iostream>
class CircularQueue {
private:
int *arr;
public:
CircularQueue(int size) {
capacity = size;
~CircularQueue() {
delete[] arr;
bool isEmpty() {
52
}
bool isFull() {
if (isFull()) {
cout << "Queue is full. Cannot enqueue " << value << endl;
return;
front = 0;
arr[rear] = value;
void dequeue() {
53
if (isEmpty()) {
return;
if (front == rear) {
} else {
int peek() {
if (isEmpty()) {
return -1;
return arr[front];
54
void display() {
if (isEmpty()) {
return;
int i = front;
while (i != rear) {
i = (i + 1) % capacity;
cout << arr[rear] << endl; // Print the last element at 'rear'
int size() {
if (isEmpty()) return 0;
};
void roundRobinScheduling() {
55
cout << "\nRound Robin CPU Scheduling Simulation:" << endl;
queue.enqueue(1); // Process 1
queue.enqueue(2); // Process 2
queue.enqueue(3); // Process 3
queue.enqueue(4); // Process 4
queue.display();
// Round robin: each process gets CPU for a fixed time, then goes back to the queue.
56
queue.display(); // Check the queue contents after round robin scheduling
void tollBoothManagement() {
queue.display();
57
void printSpooler() {
queue.display();
int main() {
58
// Simulating three different real-life scenarios
return 0;
Result:
59
LAB MANUAL#4(Singly linked list & Circular Singly linked list)
Write C++ Programs for LinkedList (single) that supports the following operations: 1. Add: Add a node
with a specific value to the end of the list. 2. Remove: Remove the first node that contains a specific
value. 3. Display: Print all values in the list. 4. Search by Value: Find the index of the first occurrence of a
specific value. 5. Search by Index: Retrieve the value at a specified index. 6. Back Node: Move to the
previous node (if applicable). 7. Next Node: Move to the next node. 8. Copy List: Create a copy of the
entire linked list. 9. Find Duplicate Node Data: Detects and display the index of duplicate data. 10.
Update a List: Update a list adding the nodes at any index.
Code:
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val){
data=val;
next=NULL;
};
class SinglyLinkedList {
private:
Node* head;
60
public:
SinglyLinkedList(){
cout<<”Ibrahim Iqbal”<<endl;
cout<<”23018020022”<<endl;
head=NULL;
if (!head) {
head = newNode;
} else {
while (temp->next) {
temp = temp->next;
temp->next = newNode;
if (!head) return;
if (head->data == value) {
61
Node* temp = head;
head = head->next;
delete temp;
return;
temp = temp->next;
if (temp->next) {
temp->next = temp->next->next;
delete toDelete;
void Display() {
while (temp) {
temp = temp->next;
62
int SearchByValue(int value) {
int index = 0;
while (temp) {
if (temp->data == value) {
return index;
temp = temp->next;
index++;
return -1;
int currentIndex = 0;
while (temp) {
if (currentIndex == index) {
return temp->data;
temp = temp->next;
currentIndex++;
63
}
return -1;
void BackNode() {
cout << "Back node operation not supported in singly linked list." << endl;
cout << "Next node value: " << currentNode->next->data << endl;
} else {
SinglyLinkedList* CopyList() {
while (temp) {
newList->Add(temp->data);
temp = temp->next;
64
}
return newList;
void FindDuplicateNodeData() {
Node* temp2;
while (temp1) {
temp2 = temp1->next;
while (temp2) {
if (temp2->data == temp1->data) {
cout << "Duplicate value " << temp2->data << " found at index " << index << endl;
temp2 = temp2->next;
temp1 = temp1->next;
if (index == 0) {
newNode->next = head;
65
head = newNode;
return;
int currentIndex = 0;
temp = temp->next;
currentIndex++;
if (temp) {
newNode->next = temp->next;
temp->next = newNode;
} else {
};
int main() {
SinglyLinkedList list;
list.Add(10);
list.Add(20);
list.Add(30);
66
list.Add(40);
list.Display();
list.Remove(20);
list.Display();
list.FindDuplicateNodeData();
list.UpdateList(1, 25);
list.Display();
return 0;
Result:
67
Lab Task 1(B): Perform the above tasks for a circular singly linked list.
Code:
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val){
data=val;
next=NULL;
68
};
class CircularLinkedList {
private:
Node* head;
public:
CircularLinkedList(){
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
head=NULL;
if (!head) {
head = newNode;
newNode->next = head;
} else {
temp = temp->next;
temp->next = newNode;
69
newNode->next = head;
if (!head) return;
if (head->data == value) {
if (head->next == head) {
delete head;
head = nullptr;
} else {
temp = temp->next;
temp->next = head->next;
delete head;
head = temp->next;
return;
70
temp = temp->next;
if (temp->next != head) {
temp->next = temp->next->next;
delete toDelete;
void Display() {
if (!head) return;
do {
temp = temp->next;
int index = 0;
71
do {
if (temp->data == value) {
return index;
temp = temp->next;
index++;
return -1;
int currentIndex = 0;
do {
if (currentIndex == index) {
return temp->data;
temp = temp->next;
currentIndex++;
return -1;
72
void BackNode(Node* currentNode) {
cout << "Back node operation not supported in circular list at head." << endl;
return;
temp = temp->next;
cout << "Back node value: " << temp->data << endl;
if (!head) return;
cout << "Next node value: " << currentNode->next->data << endl;
CircularLinkedList* CopyList() {
do {
73
newList->Add(temp->data);
temp = temp->next;
return newList;
void FindDuplicateNodeData() {
if (!head) return;
do {
if (temp2->data == temp1->data) {
cout << "Duplicate value " << temp2->data << " found at index " << index << endl;
temp2 = temp2->next;
temp1 = temp1->next;
if (index == 0) {
74
if (!head) {
head = newNode;
newNode->next = head;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = head;
head = newNode;
return;
int currentIndex = 0;
temp = temp->next;
currentIndex++;
newNode->next = temp->next;
temp->next = newNode;
} else {
75
cout << "Index out of bounds." << endl;
};
int main() {
CircularLinkedList cll;
cll.Add(10);
cll.Add(20);
cll.Add(30);
cll.Add(40);
cll.Display();
cll.Remove(20);
cll.Display();
cll.FindDuplicateNodeData();
cll.UpdateList(1, 25);
cll.Display();
76
return 0;
Result:
Lab Task 3: Simulate four real-life applications of both linked list types using your implementation. For
each scenario, provide comments to explain how the properties of linked lists make them suitable for
that case.
Result:
77
3. Undo/Redo Operations (Circular Linked List):
o Use Case: Implementing undo and redo functionality in applications like text editors.
o Why Circular Linked List?: Circular linked lists allow the user to go back and forth
seamlessly, providing a circular buffer that helps navigate previous states in both
directions.
_____________________________________________________________________________________
78
Lab Manual 5(Doubly linked list & Circular doubly linked list)
Lab task 1: Write C++ Programs for LinkedList (Doubly) that supports the following operations: 1. Add:
Add a node with a specific value to the end of the list. 2. Remove: Remove the node following FIFO. 3.
Display: Print all values in the list. 4. Search by Index: Retrieve the value at a specified index. 5. Back
Node: Move to the previous node 6. Update a List: Update a list adding the nodes at any index.
Code:
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev;
Node(int val){
data=val;
next=NULL;
prev=NULL;
};
79
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList(){
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
head=NULL;
if (!head) {
head = newNode;
} else {
while (current->next) {
current = current->next;
current->next = newNode;
newNode->prev = current;
80
}
void Remove() {
if (!head) {
return;
head = head->next;
if (head) {
head->prev = nullptr;
delete temp;
if (!head) {
return;
while (current) {
81
current = current->next;
if (index < 0) {
return -1;
int count = 0;
while (current) {
if (count == index) {
return current->data;
current = current->next;
count++;
return -1;
82
int BackNode() const {
if (!head) {
return -1;
while (current->next) {
current = current->next;
return current->data;
if (index < 0) {
return;
if (index == 0) {
newNode->next = head;
if (head) {
83
head->prev = newNode;
head = newNode;
return;
int count = 0;
while (current) {
if (count == index - 1) {
newNode->next = current->next;
if (current->next) {
current->next->prev = newNode;
current->next = newNode;
newNode->prev = current;
return;
current = current->next;
count++;
84
~DoublyLinkedList() {
while (head) {
head = head->next;
delete temp;
};
int main() {
DoublyLinkedList list;
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);
list.Add(50);
list.Display();
list.Remove();
85
cout << "List after removing the first node: ";
list.Display();
int index = 2;
cout << "Value at index " << index << ": " << list.SearchByIndex(index) << endl;
cout << "Value at the last node: " << list.BackNode() << endl;
list.UpdateList(2, 60);
list.Display();
list.Display();
return 0;
Result:
86
Lab Task 2: Implement the above operations (Q1), using double circular linked list. Make display function
dynamic.
Code:
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev;
Node(int val){
87
data=val;
next=NULL;
prev=NULL;
};
class DoublyCircularLinkedList {
private:
Node* head;
public:
DoublyCircularLinkedList(){
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
head=NULL;
if (!head) {
head = newNode;
head->next = head;
88
head->prev = head;
} else {
temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
newNode->next = head;
head->prev = newNode;
void Remove() {
if (!head) {
return;
if (head->next == head) {
delete head;
head = nullptr;
} else {
89
head = head->next;
head->prev = temp->prev;
temp->prev->next = head;
delete temp;
if (!head) {
return;
do {
current = current->next;
if (index < 0) {
90
return -1;
int count = 0;
do {
if (count == index) {
return current->data;
current = current->next;
count++;
return -1;
if (!head) {
return -1;
return head->prev->data;
91
}
if (index < 0) {
return;
if (index == 0) {
newNode->next = head;
newNode->prev = head->prev;
head->prev->next = newNode;
head->prev = newNode;
head = newNode;
return;
int count = 0;
do {
if (count == index - 1) {
newNode->next = current->next;
newNode->prev = current;
92
current->next->prev = newNode;
current->next = newNode;
return;
current = current->next;
count++;
~DoublyCircularLinkedList() {
if (!head) return;
do {
delete temp;
temp = nextNode;
};
int main() {
DoublyCircularLinkedList list;
93
list.Add(10);
list.Add(20);
list.Add(30);
list.Add(40);
list.Add(50);
list.Display();
list.Remove();
list.Display();
int index = 2;
cout << "Value at index " << index << ": " << list.SearchByIndex(index) << endl;
cout << "Value at the last node: " << list.BackNode() << endl;
list.UpdateList(2, 60);
94
cout << "List after updating by adding 60 at index 2: ";
list.Display();
list.UpdateList(0, 5);
list.Display();
return 0;
Result:
95
Lab Manual #06 (Linear & Binary Searching, Bubble sorting)
Code:
#include <iostream>
if (arr[i] == target) {
return i;
return -1;
int main() {
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target;
if (result != -1) {
96
cout << "Target element found at index " << result << endl;
} else {
cout << "Target element not found in the array" << endl;
return 0;
Results:
Code:
#include <iostream>
97
int left = 0;
int right = n - 1;
if (arr[mid] == target) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
return -1;
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int target;
98
for (int i = 0; i < n - 1; i++) {
arr[i] = arr[j];
arr[j] = temp;
if (result != -1) {
cout << "Target element found at index " << result << endl;
} else {
cout << "Target element not found in the array" << endl;
return 0;
Result:
99
Q3. Write c++ code for bubble sorting.
Code:
#include <iostream>
100
arr[j + 1] = temp;
int main() {
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
printArray(arr, n);
bubbleSort(arr, n);
101
printArray(arr, n);
return 0;
Result:
Q4: Write the code for linear searching that returns any duplicate values and counts how many times
they appear in an array.
Code:
#include <iostream>
int count = 1;
102
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
arr[j] = -1;
if (count > 1) {
cout << "Duplicate value: " << arr[i] << " appears " << count << " times." << endl;
int main() {
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
linearSearch(arr, n);
103
return 0;
Q5: Write the code for binary searching that dynamically sorts the data using a sorting algorithm.
Code:
#include <iostream>
104
arr[j] = arr[j + 1];
arr[j + 1] = temp;
int left = 0;
int right = n - 1;
if (arr[mid] == target) {
left = mid + 1;
} else {
right = mid - 1;
int main() {
105
int n;
cin >> n;
bubbleSort(arr, n);
int target;
106
if (result != -1) {
cout << "Target element found at index " << result << endl;
} else {
delete[] arr;
return 0;
Result:
Q6: Implement bubble sort in descending order. The data should consist of the individual characters of
your name.
Code:
#include <iostream>
107
void bubbleSort(char arr[], int n) {
arr[j + 1] = temp;
int main() {
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
bubbleSort(name, n - 1);
cout << "Name in descending order: " << name << endl;
108
return 0;
Result:
Q7: Implement linear searching, binary searching, and bubble sorting in a real-time scenario, such as
searching for a product in a list or sorting a list of students by grades.
Code:
#include <iostream>
#include <string>
struct Product {
string name;
int price;
};
109
int linearSearch(Product products[], int n, string targetName) {
if (products[i].name == targetName) {
return i;
return -1;
int left = 0;
int right = n - 1;
if (products[mid].name == targetName) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
110
return -1;
products[j + 1] = temp;
int main() {
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
Product products[] = {
111
};
if (index != -1) {
cout << "Product found at index " << index << endl;
} else {
if (index != -1) {
cout << "Product found at index " << index << endl;
} else {
bubbleSort(products, n);
cout << products[i].name << ": $" << products[i].price << endl;
112
return 0;
Result:
Q8: Create a C++ program where you simulate the stock prices of a company, and use linear search to
find a particular price, binary search to find prices within a range, and bubble sort to arrange the prices
in ascending or descending order.
Code:
#include <iostream>
113
for (int i = 0; i < MAX_PRICES; i++) {
cout << "Day " << (i + 1) << ": $" << prices[i] << endl;
if (prices[i] == targetPrice) {
return i;
return -1;
int left = 0;
114
if (prices[mid] >= minPrice && prices[mid] <= maxPrice) {
cout << "Price $" << prices[mid] << " found within the range." << endl;
left = mid + 1;
left = mid + 1;
} else {
right = mid - 1;
prices[j + 1] = temp;
int main() {
115
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
int stockPrices[MAX_PRICES];
srand(time(0));
generatePrices(stockPrices);
printPrices(stockPrices);
if (index != -1) {
cout << "Target price $" << targetPrice << " found on day " << (index + 1) << endl;
} else {
cout << "Target price $" << targetPrice << " not found" << endl;
cout << "Prices within the range $" << minPrice << " to $" << maxPrice << ":" << endl;
bubbleSort(stockPrices, true);
116
cout << "Stock Prices in Ascending Order:" << endl;
bubbleSort(stockPrices, true);
printPrices(stockPrices);
bubbleSort(stockPrices, false);
printPrices(stockPrices);
return 0;
117
Lab Manual #7
Q1. Perform the following for Stack Implementation using Linked list:
1. Push Operation
2. Pop Operation
4. Search Operation
5. Remove Operation
Code:
#include <iostream>
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
};
class stack{
public:
node* top;
int size;
118
stack(){
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
top=NULL;
size=0;
if(top==NULL){
top=newnode;
size++;
return;
}else{
node* temp=top;
while(temp!=NULL){
if(a==temp->data){
return;
temp=temp->next;
newnode->next=top;
top=newnode;
size++;
return;
119
}
void pop(){
if(size==0){
return;
}else{
top=top->next;
size--;
return;
void peek(){
if(size==0){
return;
}else{
int mid=size/2;
int n;
node* temp=top;
while(n!=mid){
temp=temp->next;
n++;
120
}
while(temp->next!=NULL){
temp=temp->next;
return;
int index=0;
node* temp=top;
while(temp!=NULL){
if(b==temp->data){
temp=temp->next;
index++;
void remove(){
if(size==0){
return;
}else{
121
cout<<"All values in stack have been deleted."<<endl;
delete top;
top=NULL;
size=0;
return;
void display(){
if(size==0){
return;
}else{
node* temp=top;
while(temp!=NULL){
cout<<temp->data<<endl;
temp=temp->next;
};
int main(){
stack obj;
obj.push(45);
obj.push(34);
obj.push(82);
122
obj.push(8);
obj.display();
obj.push(82);
obj.pop();
obj.display();
obj.peek();
obj.search(82);
obj.remove();
obj.display();
Result:
Q2. Perform the following for Queue Implementation using Linked list:
1. Enqueue Operation
123
2. Dequeue Operation
4. Search Operation
5. Remove Operation
Code:
#include <iostream>
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
};
class queue{
public:
node* front;
node* rear;
int size;
queue(){
cout<<"Ibrahim Iqbal"<<endl;
124
cout<<"23018020022"<<endl;
front=NULL;
rear=NULL;
size=0;
front=rear=newnode;
size++;
return;
}else{
node* temp=front;
while(temp!=NULL){
if(a==temp->data){
return;
temp=temp->next;
rear->next=newnode;
rear=newnode;
size++;
return;
125
}
void dequeue(){
if(size==0){
return;
}else{
front=front->next;
size--;
return;
void peek(){
if(size==0){
return;
}else{
int mid=size/2;
int n;
node* temp=front;
while(n!=mid){
temp=temp->next;
n++;
126
cout<<"The middle value is: "<<temp->data<<endl;
return;
int index=0;
node* temp=front;
while(temp!=NULL){
if(b==temp->data){
temp=temp->next;
index++;
void remove(){
if(size==0){
return;
}else{
delete front;
delete rear;
front=NULL;
127
rear=NULL;
size=0;
return;
void display(){
if(size==0){
return;
}else{
node* temp=front;
while(temp!=NULL){
cout<<temp->data<<endl;
temp=temp->next;
};
int main(){
queue obj;
obj.enqueue(45);
obj.enqueue(34);
obj.enqueue(82);
obj.enqueue(8);
obj.display();
128
obj.enqueue(82);
obj.dequeue();
obj.display();
obj.peek();
obj.search(82);
obj.remove();
obj.display();
Result:
Q3. Identify and implement the appropriate data structure(s) for the given scenarios (with minimum 3
attributes of respective scenario):
Code:
#include <iostream>
129
#include <cstring>
class node{
public:
string data;
node* next;
node(string val){
data=val;
next=NULL;
};
class orders{
public:
node* front;
node* rear;
int size;
orders(){
front=NULL;
rear=NULL;
size=0;
130
node* newnode=new node(a);
front=rear=newnode;
size++;
return;
}else{
node* temp=front;
while(temp!=NULL){
if(a==temp->data){
return;
temp=temp->next;
rear->next=newnode;
rear=newnode;
size++;
return;
void dequeue(){
if(size==0){
return;
}else{
131
cout<<"Order prepared and delivered is: "<<front->data<<endl;
front=front->next;
size--;
return;
void peek(){
if(size==0){
return;
}else{
int mid=size/2;
int n;
node* temp=front;
while(n!=mid){
temp=temp->next;
n++;
return;
132
int index=1;
node* temp=front;
while(temp!=NULL){
if(b==temp->data){
temp=temp->next;
index++;
void remove(){
if(size==0){
return;
}else{
delete front;
delete rear;
front=NULL;
rear=NULL;
size=0;
return;
void display(){
133
if(size==0){
return;
}else{
cout<<"Orders in queue:"<<endl;
node* temp=front;
while(temp!=NULL){
cout<<temp->data<<endl;
temp=temp->next;
};
int main(){
orders obj;
cout<<endl;
obj.enqueue("1x large fajita pizza + 1x medium fries + 1x 1.5L drink : Table #11");
obj.enqueue("3x small peproni pizza + 3x 6 wings meal + 3x 250ml drink : Table #04");
obj.enqueue("1x family meal no.4 + 5x fudge brownie + 6x chef's special sauce : Table #02");
obj.display();
cout<<endl;
obj.enqueue("1x large fajita pizza + 1x medium fries + 1x 1.5L drink : Table #11");
cout<<endl;
134
obj.dequeue();
cout<<endl;
obj.display();
cout<<endl;
obj.peek();
cout<<endl;
cout<<endl;
obj.remove();
obj.display();
Result:
135
2. Priority Task Manager: Design a task manager where high-priority tasks are processed using a
stack, and low-priority tasks are processed using a queue. Write a single code implementation
that manages transitions between the two priorities.
Code:
#include <iostream>
#include <string>
using namespace std;
class TaskNode {
public:
string task;
TaskNode* next;
TaskNode(string t){
task=t;
next=NULL;
}
};
class Stack {
private:
TaskNode* top;
public:
Stack(){
top=NULL;
}
void push(const string& task) {
TaskNode* newNode = new TaskNode(task);
newNode->next = top;
top = newNode;
}
string pop() {
if (top == NULL) {
cout << "Stack is empty!" << endl;
return "";
}
TaskNode* temp = top;
string task = temp->task;
top = top->next;
136
delete temp;
return task;
}
bool isEmpty() {
return top == NULL;
}
string peek() {
if (top == nullptr) return "";
return top->task;
}
};
class Queue {
private:
TaskNode* front;
TaskNode* rear;
public:
Queue(){
front=NULL;
rear=NULL;
}
string dequeue() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return "";
}
TaskNode* temp = front;
string task = temp->task;
front = front->next;
137
if (front == nullptr) {
rear = nullptr;
}
delete temp;
return task;
}
bool isEmpty() {
return front == nullptr;
}
string peek() {
if (front == nullptr) return "";
return front->task;
}
};
class TaskManager {
private:
Stack highPriorityStack;
Queue lowPriorityQueue;
public:
TaskManager(){
cout<<"Ibrahim Iqbal"<<endl;
cout<<"23018020022"<<endl;
}
void processHighPriorityTask() {
if (!highPriorityStack.isEmpty()) {
cout << "Processing high-priority task: " << highPriorityStack.pop() << endl;
} else {
138
cout << "No high-priority tasks to process!" << endl;
}
}
void processLowPriorityTask() {
if (!lowPriorityQueue.isEmpty()) {
cout << "Processing low-priority task: " << lowPriorityQueue.dequeue() << endl;
} else {
cout << "No low-priority tasks to process!" << endl;
}
}
void moveTaskToHighPriority() {
if (!lowPriorityQueue.isEmpty()) {
string task = lowPriorityQueue.dequeue();
highPriorityStack.push(task);
cout << "Moved task to high-priority: " << task << endl;
} else {
cout << "No tasks in the low-priority queue to move!" << endl;
}
}
void moveTaskToLowPriority() {
if (!highPriorityStack.isEmpty()) {
string task = highPriorityStack.pop();
lowPriorityQueue.enqueue(task);
cout << "Moved task to low-priority: " << task << endl;
} else {
cout << "No tasks in the high-priority stack to move!" << endl;
}
}
void displayTasks() {
cout << "High-priority task (stack top): " << highPriorityStack.peek() << endl;
cout << "Low-priority task (queue front): " << lowPriorityQueue.peek() << endl;
}
};
int main() {
TaskManager taskManager;
139
taskManager.addHighPriorityTask("Finish project report");
taskManager.addLowPriorityTask("Clean the desk");
taskManager.addLowPriorityTask("Respond to emails");
taskManager.displayTasks();
taskManager.processHighPriorityTask();
taskManager.processLowPriorityTask();
taskManager.moveTaskToLowPriority();
taskManager.moveTaskToHighPriority();
taskManager.displayTasks();
return 0;
}
Result:
140