Ex.1 Array implementation of List ADT Aim: To implement a simple dynamic array class (ArrayList) in C++ with the following operations 1. Add an element to the list 2. Get the element in the specified position 3. Remove the element in the specified position 4. display all elements in the list. Algorithm: 1. Start. 2. Define the ArrayList class with the following components: ● Private Members: o arr: A pointer to dynamically allocated memory for storing elements. o capacity: The maximum size of the list. o size: The current number of elements in the list. ● Public Members: o Constructor to initialize the array with a specified capacity. o Destructor to release allocated memory. o Functions for adding, retrieving, removing, and displaying elements. 3. Constructor: ● Input: capacity of the list. ● Actions: o Set capacity to the given input. o Initialize size to 0 (empty list). o Allocate memory for the array using the new operator. 4. Destructor: ● Action: Release memory allocated for the array using delete[]. 5. Add Function (add): ● Input: value to be added. ● Actions: o Check if size is less than capacity. ▪ If true, insert value at the current index (size) and increment size by 1. ▪ If false, display "List is full." 6. Get Function (get): ● Input: index to retrieve. ● Actions: o Check if the index is valid (i.e., 0 <= index < size). ▪ If valid, return the element at the given index. ▪ If invalid, display "Index out of bounds" and return -1. 7. Remove Function (remove): ● Input: index to remove. ● Actions: o Check if the index is valid (i.e., 0 <= index < size). ▪ If valid: ▪ Shift all elements from index + 1 to size - 1 one position to the left. ▪ Decrease size by 1. ▪ If invalid, display "Index out of bounds." 8. Display Function (display): ● Actions: o Iterate through the array from index 0 to size - 1. o Print all elements separated by spaces. 9. Main Function: ● Input: capacity for the list, followed by n elements to add, and indices for retrieval and removal operations. ● Actions: o Create an ArrayList object with the specified capacity. o Add n elements to the list using the add function. o Input an index to retrieve an element and display the result using the get function. o Input an index to remove an element using the remove function. o Display the final state of the list using the display function. 10. End. Program: #include <iostream> using namespace std; class ArrayList { private: int *arr; int capacity; int size; public: // Constructor ArrayList(int cap) { capacity = cap; size = 0; arr = new int[capacity]; } ~ArrayList() { delete[ ] arr; } void add(int value) { if (size < capacity) { arr[size++] = value; } else { cout << "List is full!" << endl; } } int get(int index) { if (index >= 0 && index <= size) { return arr[index]; } else { cout << "Index out of bounds!" << endl; return -1; // Error value } } void remove(int index) { if (index >= 0 && index < size) { for (int i = index; i < size - 1; i++) { arr[i] = arr[i + 1]; } size--; } else { cout << "Index out of bounds!" << endl; } } void display() { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; } int getsize( ){ return size; } }; int main( ) { int n, indexToGet, indexToRemove; cin >> n; ArrayList list(n); for (int i = 0; i < n; i++) { int value; cin >> value; list.add(value); } cin >> indexToGet; cout << list.get(indexToGet) << endl; cin >> indexToRemove; list.remove(indexToRemove); list.display(); return 0; } Output: Result: Thus, Array implementation of List ADT is successfully executed Ex.2 Linked list implementation of List ADT Aim: To implement a Singly Linked List in C++ with the following operations: Insertion Deletion Traversal. Algorithm: 1. Start. 2. Define the SinglyLinkedList class with the following components: o Private: ▪ A Node structure containing: ▪ data to store the value of the node. ▪ next to store the pointer to the next node. ▪ A head pointer to represent the start of the list. o Public: ▪ Constructor, Destructor, and member functions for insertion, deletion, and traversal. 3. Constructor: o Initialize the head pointer to nullptr to represent an empty list. 4. Insertion (insert): o Create a new node using dynamic memory allocation. o Assign the input value to the new node’s data field. o Set the new node's next pointer to the current head. o Update the head pointer to point to the new node. 5. Deletion (deleteElement): o Input: value (element to delete). o If the list is empty (head == nullptr), return. o If the value is at the head: ▪ Update the head pointer to point to the next node. ▪ Delete the old head node. ▪ Return. o Traverse the list to find the node with the given value: ▪ Keep track of the previous node. ▪ If the node is found: ▪ Update the next pointer of the previous node to skip the node to be deleted. ▪ Delete the target node. o If the value is not found, exit without making changes. 6. Traversal (traverse): o Use a temporary pointer to iterate through the list. o For each node, display its data field. o Stop when the next pointer is nullptr. 7. Destructor: o Loop through the list: ▪ Delete each node one by one by updating the head pointer to the next node. 8. Main Function: o Input: Number of operations (n). o For each operation: ▪ Input operation (1 for insertion, 2 for deletion) and value. ▪ If operation == 1, insert the value into the list. ▪ If operation == 2, delete the value from the list. o Call the traverse function to display the final list. 9. End. Program: #include<iostream> using namespace std; class SinglyLinkedList { private: struct Node { int data; Node* next; }; Node* head; public: SinglyLinkedList() { head = nullptr; } void insert(int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = head; head = newNode; } void deleteElement(int value) { Node* temp = head; Node* prev = nullptr; if (temp != nullptr&& temp->data == value) { head = temp->next; delete temp; return; } while (temp != nullptr && temp->data != value) { prev = temp; temp = temp->next; } if (temp == nullptr) { return; } prev->next = temp->next; delete temp; } void traverse() { Node* temp = head; while (temp != nullptr) { cout<< temp->data << " "; temp = temp->next; } cout<<endl; } ~SinglyLinkedList() { while (head != nullptr) { Node* temp = head; head = head->next; delete temp; } } }; int main() { int n, operation, value; cin>> n; SinglyLinkedList list; for (inti = 0; i< n; i++) { cin>> operation >> value; if (operation == 1) { list.insert(value); } else if (operation == 2) { list.deleteElement(value); } } list.traverse(); return 0; } Output: Result: Thus, linked list implementation of List ADT is executed successfully. Ex.3 Array implementation of Stack Aim: Write a c++ program to implement stack data structures using arrays. The operations performed on the stack are 1.Push 2.Pop 3.Peek Algorithm: 1. Initialize the Stack Class: ● Create a class Stack with a private member vector<int> stack to hold the stack elements. ● Define public methods for stack operations: push, pop, peek, isEmpty, and printStack. 2. Push Operation: ● Input: An integer element to be added. ● Append the element to the end of the vector using push_back. 3. Pop Operation: ● Check if the stack is not empty using isEmpty(). o If the stack is not empty, remove the last element using pop_back. o If the stack is empty, do nothing. 4. Peek Operation: ● Check if the stack is not empty using isEmpty(). o If the stack is not empty, return the last element of the vector using back. o If the stack is empty, return -1. 5. isEmpty Operation: ● Return true if the vector is empty using empty(), otherwise return false. 6. Print Stack Operation: ● Check if the stack is empty using isEmpty(). o If the stack is empty, print "Empty". o Otherwise, iterate through the stack in reverse order (LIFO order) using a reverse iterator (rbegin to rend) and print each element, separated by spaces. 7. Main Function: ● Input the number of operations n. ● Create an object of the Stack class. ● For each of the n operations: o Input the type of operation (operation): ▪ If operation == 1, input the value value and call push(value). ▪ If operation == 2, call pop(). ▪ If operation == 3, call peek() and print the returned value if it is not -1. ● After all operations, call printStack() to display the final state of the stack. 8 End. Program: #include<iostream> #include<vector> using namespace std; class Stack { private: vector<int> stack; public: // Push operation void push(int element) { stack.push_back(element); } // Pop operation void pop() { if (!isEmpty()) { stack.pop_back(); } } // Peek operation int peek() { if (!isEmpty()) { return stack.back(); } return -1; } // Check if stack is empty bool isEmpty() { return stack.empty(); } // Print the stack void printStack() { if (isEmpty()) { cout<< "Empty"; } else { for (auto it = stack.rbegin(); it != stack.rend(); ++it) { if (it != stack.rbegin()) cout<< " "; cout<< *it; } } cout<<endl; } }; int main() { int n, operation, value; cin>> n; Stack stack; vector<int>peekResults; for (int i = 0; i< n; i++) { cin>> operation; if (operation = = 1) { cin>> value; stack.push(value); } else if (operation = = 2) { stack.pop(); } else if (operation = = 3) { int topElement = stack.peek(); if (topElement != -1) { peekResults.push_back(top); } } } for(int result:peekResults){ cout<<result<<endl; } stack.printStack(); return 0; } Output: Result: Thus, the Array implementation of Stack is successfully executed. Ex.4 Array implementation of Queue Aim: Write a c++ program to implement Queue data structures using arrays. The operations performed on the Queue are 1. Enqueue 2.Dequeue Algorithm: 1 Initialize the Queue: ● Declare a queue q of integers using queue<int>. 2 Input the Number of Operations: ● Read an integer n representing the number of operations to be performed. 3 Perform Operations: ● Loop n times: 1. Read the operation type operation: ▪ If operation == 1: ▪ Read an integer value. ▪ Add value to the back of the queue using q.push(value). ▪ If operation == 2: ▪ Check if the queue is not empty using q.empty(). ▪ If not empty, remove the front element of the queue using q.pop(). 4 Display the Final State of the Queue: ● If the queue is empty (q.empty()): o Print "Empty". ● Otherwise: o While the queue is not empty: ▪ Print the front element of the queue using q.front(). ▪ Remove the front element using q.pop(). ▪ If the queue is not empty after the current element, print a space for formatting. 5 End the Program: ● Print a newline and terminate the program. Program: #include<iostream> #include<queue> using namespace std; int main() { int n; cin>> n; queue<int> q; for (inti = 0; i< n; ++i) { int operation; cin>> operation; if (operation == 1) { int value; cin>> value; q.push(value); } else if (operation == 2) { if (!q.empty()) { q.pop(); } } } if (q.empty()) { cout<< "Empty"; } else { while (!q.empty()) { cout<<q.front(); q.pop(); if (!q.empty()) { cout<< " "; } } } cout<<endl; return 0; } Output: Result: Thus, Array implementation of Queue is executed successfully. Ex 5: Linked list implementation of Stack Aim: Write a c++ program to implement stack data structures using Linked list. The operations performed on the stack are 1. Push 2. Pop 3. Peek Algorithm: 1. Define Node Structure: ● Create a Node struct with: o An integer data to store the value. o A pointer next to point to the next node in the stack. 2. Initialize the Stack: ● Create a class Stack with: o A private pointer top to point to the topmost node in the stack. o A public constructor to initialize top as nullptr. 3. Push Operation: ● Input: An integer value. ● Create a new node using dynamic memory allocation. ● Assign the value to the new node's data. ● Point the new node's next to the current top. ● Update top to the new node. 4. Pop Operation: ● Check if the stack is not empty using isEmpty(): o If not empty: ▪ Store the current top node in a temporary pointer. ▪ Update top to the next node. ▪ Delete the temporary node to free memory. ● If empty, do nothing. 5. Peek Operation: ● Check if the stack is not empty using isEmpty(): o If not empty, return the data of the top node. o If empty, return -1. 6. isEmpty Operation: ● Return true if top == nullptr, otherwise return false. 7. Display Stack: ● If the stack is empty, print "Empty". ● Otherwise, traverse the stack starting from top: o Print the data of each node. o Use -> as a separator for nodes. 8. Main Function: ● Input the number of operations n. ● For each operation: o If the operation is 1, input a value and call push(value). o If the operation is 2, call pop(). o If the operation is 3, call peek() and print the returned value. ● After all operations, call displayStack() to display the final state of the stack. Program: #include<iostream> using namespace std; struct Node { int data; Node* next; }; class Stack { private: Node* top; public: Stack() : top(nullptr) {} void push(int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = top; top = newNode; } void pop() { if (!isEmpty()) { Node* temp = top; top = top->next; delete temp; } } int peek() { if (!isEmpty()) { return top->data; } return -1; } bool isEmpty() { return top == nullptr; } void displayStack() { if (isEmpty()) { cout<< "Empty" <<endl; return; } Node* current = top; while (current != nullptr) { cout<< current->data; if (current->next != nullptr) { cout<< "->"; } current = current->next; } cout<<endl; } } ~stack(){ while(!isEmpty()){ pop(); } } }; int main() { int n; cin>> n; Stack stack; for (int i = 0; i< n; ++i) { int operation; cin>> operation; if (operation == 1) { int value; cin>> value; stack.push(value); } else if (operation == 2) { stack.pop(); } else if (operation == 3) { int topValue=stack.peek(); if(topValue!=-1) { peekResults.push_back(topValue); } } } for(int result : peekResults){ cout<<result<<endl; } stack.displayStack(); return 0; } Output Result: Thus, Linked list implementation of Stack is executed successfully. Ex 6: Linked list implementation of Queue ADT. Aim: Write a c++ program that implements Queue data structures using Linked List. The operations performed on the Queue are 1. Enqueue 2. Dequeue Algorithm: 1. Define Node Structure: ● Create a structure Node with: o An integer data to store the value. o A pointer next to point to the next node in the queue. ● Define a constructor for Node to initialize data and next. 2. Initialize the Queue: ● Create a class Queue with: o A private pointer front to point to the first node in the queue. o A private pointer rear to point to the last node in the queue. o A public constructor to initialize both front and rear as nullptr. 3. Enqueue Operation: ● Input: An integer val. ● Create a new node with the value val. ● If rear is nullptr (queue is empty): o Set both front and rear to the new node. ● Otherwise: o Link the current rear node to the new node (rear->next = newNode). o Update rear to the new node. 4. Dequeue Operation: ● If front is nullptr (queue is empty): o Do nothing and return. ● Otherwise: o Store the current front node in a temporary pointer. o Move front to the next node (front = front->next). o If front becomes nullptr, set rear to nullptr (queue becomes empty). o Delete the temporary node to free memory. 5. Display Queue: ● If front is nullptr (queue is empty): o Print "Empty". ● Otherwise: o Traverse the queue starting from front: ▪ Print the data of each node. ▪ Use -> as a separator for nodes, except after the last node. 6. Main Function: ● Input the number of operations n. ● For each operation: o If the operation is 1, input a value and call enqueue(value). o If the operation is 2, call dequeue(). ● After all operations, call displayQueue() to display the final state of the queue. Program: #include<iostream> using namespace std; struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class Queue { private: Node* front; Node* rear; public: Queue() : front(nullptr), rear(nullptr) {} void enqueue(int val) { Node* newNode = new Node(val); if (rear == nullptr) { front = rear = newNode; return; } rear->next = newNode; rear = newNode; } void dequeue() { if (front == nullptr) { return; } Node* temp = front; front = front->next; if (front == nullptr) { rear = nullptr; delete temp; } } void displayQueue() { if (front == nullptr) { cout<< "Empty" <<endl; return; } Node* temp = front; while (temp != nullptr) { cout<< temp->data; if (temp->next != nullptr) { cout<< "->"; } temp = temp->next; } cout<<endl; } ~Queue(){ while(front!=nullptr){ dequeue(); } } }; int main() { int n; cin>> n; Queue queue; for (int i = 0; i< n; ++i) { int operation; cin>> operation; if (operation == 1) { int value; cin>> value; queue.enqueue(value); } else if (operation == 2) { queue.dequeue(); } } queue.displayQueue(); return 0; } Output: Result: Thus, Linked list implementation of Queue ADT is executed successfully.