0% found this document useful (0 votes)
6 views

Project

Uploaded by

Muskan Jangra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Project

Uploaded by

Muskan Jangra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

INDEX

Sr. No. Program Page No. Sign


1. Design, Develop and Implement a menu- driven Program in C++ for the following
Array operations
1. Creating an Array of N Integer Elements
2. Display of Array Elements with Suitable Headings
3. Inserting an Element(ELEM) at a given valid Position(POS)
4. Deleting an Element at a given valid Position(POS)
5. Exit.
2. Design, Develop and Implement a Program in C++ for the following operations on
Strings
Read a main String(STR), a Pattern String(PAT) and a Replace String(REP)
Perform Pattern Matching Operation: Find and Replace all occurences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR. Support the program with functions for each of the above
operations. Don’t use Built-in functions.

3. Design, Develop and Implement a menu driven Program in C++ for the following
operations on STACK of Integers( Array Implementation of Stack with maximum
size MAX).
Push an Element on to Stack
Pop an Element from Stack
Demonstrate how Stack can be used to check Palindrome
Demonstrate Overflow and Underflow situations on Stack
Display the status of Stack

4. Design, Develop and Implement a Program in C++ for converting an Infix


Expression to Postfix Expression. Program should support for both parenthesized
and free parenthesized expressions with the operators: +, -, *, /, %(Remainder),
^(Power) and alphanumeric operands.

5. Design, Develop and Implement a Program in C++ for the following Stack
Applications ,Evaluation of Suffix expression with single-digit operands and
operators: +, -, *, /, %, ^
Solving Tower of Hanoi problem with n disks

6. Design, Develop and Implement a menu driven Program in C++ for the following
operations on Circular Queue of Characters (Array Implementation of Queue
with maximum size MAX)
Insert an Element on to Circular QUEUE
Delete an Element from Circular QUEUE
Demonstrate Overflow and Underflow situations on Circular QUEUE
Display the status of Circular QUEUE

7. Design, Develop and Implement a Program in C++ for the following operations on
Singly Circular Linked List (SCLL) with header nodes
Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z).

2|Page
9. Design, Develop and Implement a Program in C++ for the following operations on
Singly Circular Linked List (SCLL) with header nodes
Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-
4yz5+3x3yz+2xy5z-2xyz3 Find the sum of two polynomials
POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z).

10. Design, Develop and Implement a menu driven Program in C++ for the following
operations on Binary Search Tree(BST) of Integers
Create a BST of N Integers: 6,9,5,2,8,15,24,14,7,8,5,2
Traverse the BST in Inorder, Preorder and Post Order
Search the BST for a given element (Key) and report the appropriate message
Exit
11. Design, Develop and Implement a Program in C++ for the following operations on
Graph(G) of Cities
Create a Graph of N cities using Adjacency Matrix.
Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method

12. . Given a File of N employee records with a set K of Keys(4-digit) which uniquely
determine the records in file F. Assume that file F is maintained in memory by a
Hash Table(HT) of m memory locations with L as the set of memory addresses( 2-
digit) of locations in HT.
Let the keys in K and addresses in L are Integers. Design and develop a program
in C++ that uses Hash function H:K→ L as H(K)=K mod m ( remainder method),
and implement hashing technique to map a given key K to the address space L.
Resolve the collision(if any) using linear probing.

3|Page
1. Design, Develop and Implement a menu- driven Program in C++ for the following Array operations
1. Creating an Array of N Integer Elements
2. Display of Array Elements with Suitable Headings
3. Inserting an Element(ELEM) at a given valid Position(POS)
4. Deleting an Element at a given valid Position(POS)
5. Exit. Roll No. 24MCA/66*/

#include <iostream>
using namespace std;

void createArray(int arr[], int &size) {


cout << "\nEnter the number of elements (N): ";
cin >> size;
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Array created successfully!\n";
}

void displayArray(const int arr[], int size) {


if (size == 0) {
cout << "\nArray is empty.\n";
return;
}
cout << "\nArray Elements: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}

void insertElement(int arr[], int &size, int elem, int pos) {


if (pos < 1 || pos > size + 1) {
cout << "\nInvalid position! Please enter a valid position between 1 and " << size + 1 << ".\n";
return;

4|Page
}
for (int i = size; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = elem;
size++;
cout << "\nElement inserted successfully!\n";
}

void deleteElement(int arr[], int &size, int pos) {


if (pos < 1 || pos > size) {
cout << "\nInvalid position! Please enter a valid position between 1 and " << size << ".\n";
return;
}
for (int i = pos - 1; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
cout << "\nElement deleted successfully!\n";
}

int main() {
const int MAX_SIZE = 100;
int arr[MAX_SIZE], size = 0;
int choice, elem, pos;

cout << "\nRoll No: 24MCA/66\n";


cout << "Menu:\n";
cout << "1. Create Array\n";
cout << "2. Display Array\n";
cout << "3. Insert Element\n";
cout << "4. Delete Element\n";
cout << "5. Exit\n";

do {
5|Page
cout << "\nEnter your choice: ";
cin >> choice;

switch (choice) {
case 1:
createArray(arr, size);
break;

case 2:
displayArray(arr, size);
break;

case 3:
cout << "\nEnter the element to insert: ";
cin >> elem;
cout << "Enter the position to insert at: ";
cin >> pos;
insertElement(arr, size, elem, pos);
break;

case 4:
cout << "\nEnter the position to delete element from: ";
cin >> pos;
deleteElement(arr, size, pos);
break;

case 5:
cout << "\nExiting program. Thank you!\n";
break;

default:
cout << "\nInvalid choice! Please try again.\n";
}
} while (choice != 5);

6|Page
return 0;
}

7|Page
2. Design, Develop and Implement a Program in C++ for the following operations on Strings
a. Read a main String(STR), a Pattern String(PAT) and a Replace String(REP)
b. Perform Pattern Matching Operation: Find and Replace all occurences of PAT in STR with REP if
PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don’t use Built-in functions.
Roll No. 24/MCA/66*/
#include <iostream>
#include <string>
using namespace std;

// Function to read the strings


void readStrings(string &STR, string &PAT, string &REP) {
cout << "Enter the main string (STR): ";
getline(cin, STR);
cout << "Enter the pattern string (PAT): ";
getline(cin, PAT);
cout << "Enter the replace string (REP): ";
getline(cin, REP);
}

// Function to perform pattern matching and replacement


void findAndReplace(string &STR, const string &PAT, const string &REP) {
bool found = false;
size_t pos = 0;

// Loop through the string to find all occurrences of PAT


while ((pos = STR.find(PAT, pos)) != string::npos) {
found = true;
STR.replace(pos, PAT.length(), REP);
pos += REP.length(); // Move past the replacement
}

// Display the result or an error message


if (found) {
cout << "\nThe modified string is: " << STR << endl;
} else {
cout << "\nPattern string (PAT) not found in the main string (STR)." << endl;
}
}

// Main function
int main() {
string STR, PAT, REP;

// Read inputs
readStrings(STR, PAT, REP);

// Perform pattern matching and replacement


findAndReplace(STR, PAT, REP);

return 0;
}

18 | P a g e
3. Design, Develop and Implement a menu driven Program in C++ for the following operations on STACK
of Integers( Array Implementation of Stack with maximum size MAX).
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Roll No. 24/MCA/66.*/

#include<iostream>
using namespace std;
#define MAX 100
int stack[MAX];
int top=-1;
void push(int element) {
if (top==MAX-1) {
cout << "Stack Overflow - Cannot push element onto the stack.\n";
} else {
stack[++top] = element;
cout << "Element pushed onto the stack: " << element << endl;
}
}
void pop() {
if (top==-1) {
cout << "Stack Underflow - Cannot pop element from an empty stack.\n";
} else {
cout << "Element popped from the stack: " << stack[top--] << endl;
}
}
void display() {
if (top==-1) {
cout << "Stack is empty.\n";
} else {
cout << "Elements in the stack: ";
for (int i=0;i<=top;i++) {
cout << stack[i] << " ";
}
cout << endl;
}
19 | P a g e
}
bool isPalindrome() {
int i = 0, j = top;
while (i<j) {
if (stack[i]!= stack[j]) {
return false;
}
i++;
j--;
}
return true;
}
void displayStatus() {
cout << "Stack Status:\n";
cout << "Elements " << top+1 << endl;
cout << "Is Palindrome: " << (isPalindrome()? "Yes" : "No") << endl;
}
int main() {
int choice, element;
cout<<"Roll No. 23/MCA/86"<<endl;
do {
cout << "\nMenu:\n";
cout << "1. Push an Element onto Stack\n";
cout << "2. Pop an Element from Stack\n";
cout << "3. Check Palindrome using Stack\n";
cout << "4. Display Stack Status\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter element to push onto the stack: ";
cin >> element;
push(element);
break;
case 2:
pop();
break;
case 3:
if (isPalindrome()) {
cout << "The stack elements form a palindrome.\n";
} else {
cout << "The stack elements do not form a palindrome.\n";
}
break;
case 4:
displayStatus();
break;
case 5:
cout << "Exiting the program.\n";

110 | P a g
e
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 5);
return 0;
}
Output:

4. Design, Develop and Implement a Program in C++ for converting an Infix Expression to Postfix
Expression. Program should support for both parenthesized and free parenthesized expressions with the
operators: +, -, *, /, %(Remainder), ^(Power) and alphanumeric operands.
Roll No. 24/MCA/66. */
#include <iostream>
#include <stack>
#include <cctype>
#include <cmath>
#include <string>
using namespace std;

// Function to return precedence of operators


int precedence(char op) {
if (op == '^')
return 3;
if (op == '*' || op == '/' || op == '%')
return 2;
if (op == '+' || op == '-')
return 1;
return 0;
}

// Function to check if the character is an operator

111 | P a g
e
bool isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^';
}

// Function to convert infix expression to postfix


string infixToPostfix(const string &infix) {
stack<char> operators;
string postfix = "";

for (char c : infix) {


if (isspace(c)) {
continue; // Ignore whitespace
}

if (isalnum(c)) {
// If operand, add to postfix expression
postfix += c;
} else if (c == '(') {
// Push '(' onto the stack
operators.push(c);
} else if (c == ')') {
// Pop and add to postfix until '(' is found
while (!operators.empty() && operators.top() != '(') {
postfix += operators.top();
operators.pop();
}
if (!operators.empty()) {
operators.pop(); // Remove the '(' from stack
}
} else if (isOperator(c)) {
// Operator found
while (!operators.empty() && precedence(operators.top()) >= precedence(c)) {
postfix += operators.top();
operators.pop();
}
operators.push(c);
} else {
cerr << "Invalid character encountered: " << c << endl;
exit(1);
}
}

// Pop all remaining operators from the stack


while (!operators.empty()) {
if (operators.top() == '(') {
cerr << "Mismatched parentheses detected." << endl;
exit(1);
}
postfix += operators.top();
operators.pop();
}

return postfix;
}

112 | P a g
e
int main() {
string infix;
cout << "Enter an infix expression: ";
getline(cin, infix);

try {
string postfix = infixToPostfix(infix);
cout << "Postfix expression: " << postfix << endl;
} catch (const exception &e) {
cerr << "Error: " << e.what() << endl;
}

return 0;
}
OUTPUT:

113 | P a g
e
5. Design, Develop and Implement a Program in C++ for the following Stack Applications
a. Evaluation of Suffix expression with single-digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks
Roll No. 24/MCA/66*/

//a. Evaluation of Suffix expression


#include <iostream>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;
int performOperation(char operation, int operand1, int operand2) {
switch (operation) {
case '+':
return operand1 + operand2;
case '-':
return operand1 - operand2;
case '*':
return operand1 * operand2;
case '/':
return operand1 / operand2;
case '%':
return operand1 % operand2;
case '^':
return static_cast<int>(pow(operand1, operand2));
default:
cerr << "Invalid operator: " << operation << endl;
exit(EXIT_FAILURE);
}
}
int evaluateSuffixExpression(const string& suffixExpression) {
stack<int> operandStack;
for (size_t i=0;i<suffixExpression.length();i++) {
char ch = suffixExpression[i];
if (isdigit(ch)) {
operandStack.push(ch - '0');
} else if (ch == ' ') {
continue;
} else {
int operand2 = operandStack.top();
operandStack.pop();
int operand1 = operandStack.top();
operandStack.pop();
int result = performOperation(ch, operand1, operand2);
operandStack.push(result);
}
}
return operandStack.top();
}
int main() {
string suffixExpression;
cout<<"Roll No. 24/MCA/66"<<endl;
cout << "Enter the suffix expression: ";
getline(cin, suffixExpression);

114 | P a g
e
int result = evaluateSuffixExpression(suffixExpression);
cout << "Result: " << result << endl;
return 0;
}

//b. Solving Tower of Hanoi problem with n disks.

#include<iostream>
#include<stack>
using namespace std;
struct Move {
int n;
char source, auxiliary, destination;
Move(int n, char source, char auxiliary, char destination)
: n(n), source(source), auxiliary(auxiliary), destination(destination) {}
};
void towerOfHanoi(int numDisks, char source, char auxiliary, char destination) {
stack<Move> moveStack;
moveStack.push(Move(numDisks, source, auxiliary, destination));
while (!moveStack.empty()) {
Move move = moveStack.top();
moveStack.pop();
int n = move.n;
char src = move.source;
char aux = move.auxiliary;
char dest = move.destination;
if (n==1) {
cout << "Move disk 1 from " << src << " to " << dest << endl;
} else {
moveStack.push(Move(n - 1, aux, src, dest));
moveStack.push(Move(1, src, aux, dest));
moveStack.push(Move(n - 1, src, dest, aux));
}
}
}
int main() {
int numDisks;
cout<<"Roll No. 24/MCA/66"<<endl;
cout << "Enter the number of disks for Tower of Hanoi: ";
cin >> numDisks;

115 | P a g
e
cout << "Steps to solve Tower of Hanoi with " << numDisks << " disks:\n";
towerOfHanoi(numDisks, 'A', 'B', 'C');
return 0;}
OUTPUT:

6. Design, Develop and Implement a menu driven Program in C++ for the following operations on
Circular Queue of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit.
Roll No. 24/MCA/66. */

#include <iostream>
using namespace std;
const int MAX = 5;
char queue[MAX];
int front = -1, rear = -1;
bool isEmpty() {
return (front == -1 && rear == -1);
}
bool isFull() {
return (front == (rear + 1) % MAX);
}
void enqueue(char element) {
if (isFull()) {
cout << "Circular Queue Overflow! Cannot enqueue.\n";
} else {
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
queue[rear] = element;
cout << "Element " << element << " enqueued.\n";
}
}
void dequeue() {
if (isEmpty()) {
cout << "Circular Queue Underflow! Cannot dequeue.\n";
} else {
116 | P a g
e
cout << "Element " << queue[front] << " dequeued.\n";
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}
void display() {
if (isEmpty()) {
cout << "Circular Queue is empty.\n";
} else {
cout << "Circular Queue elements: ";
int i = front;
do {
cout << queue[i] << " ";
i = (i + 1) % MAX;
} while (i != (rear + 1) % MAX);
cout << "\n";
}
}
int main() {
int choice;
char element;
cout<<"Roll No. 24/MCA/66"<<endl;

do {
cout << "\nMenu:\n";
cout << "1. Insert an Element on to Circular QUEUE\n";
cout << "2. Delete an Element from Circular QUEUE\n";
cout << "3. Demonstrate Overflow and Underflow situations on Circular QUEUE\n";
cout << "4. Display the status of Circular QUEUE\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the element to enqueue: ";
cin >> element;
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
cout << "Current status:\n";
display();
cout << "Attempting to enqueue...\n";
enqueue('X'); // Attempting to enqueue to demonstrate overflow
cout << "Attempting to dequeue...\n";
dequeue(); // Attempting to dequeue to demonstrate underflow
break;
case 4:
display();
break;
117 | P a g
e
case 5:
cout << "Exiting the program.\n";
break;

default:
cout << "Invalid choice. Please enter a valid option.\n";
break;
}
} while (choice != 5);
return 0;
}
Output:

118 | P a g
e
7. Design, Develop and Implement a menu driven Program in C++ for the following operations on Singly
Linked List(SLL) of Student Data with the fields: USN, Name, Branch, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion/ Deletion at End of SLL
d. Perform Insertion/ Deletion at Front of SLL(Demonstration of stack)
e. Exit
Roll No. 24/MCA/66. */

#include<iostream>
#include<cstring>
#include<limits>
using namespace std;
struct Student
{
char USN[20];
char Name[50];
char Branch[20];
int Sem;
long long PhNo;
Student* next;
};
// Function to create a new Student node
Student* createStudentNode() {
Student* newStudent = new Student;
cout << "Enter USN: ";
cin >> newStudent->USN;
cout << "Enter Name: ";
cin.ignore();
cin.getline(newStudent->Name, 50);
cout << "Enter Branch: ";
cin >> newStudent->Branch;
cout << "Enter Semester: ";
cin >> newStudent->Sem;
cout << "Enter Phone Number: ";
cin >> newStudent->PhNo;
newStudent->next = NULL; // Replace nullptr with NULL
return newStudent;
}
// Function to clear the input buffer
void clearInputBuffer()
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
}
// Function to insert a Student node at the front of the list
void insertAtFront(Student*& head) {
Student* newStudent = createStudentNode();
newStudent->next = head;
head = newStudent;
cout << "Student data inserted at the front.\n";
}
// Function to insert a Student node at the end of the list
void insertAtEnd(Student*& head) {

119 | P a g
e
Student* newStudent = createStudentNode();
if (!head) {
head = newStudent;
} else {
Student* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newStudent;
}
cout << "Student data inserted at the end.\n";
}
// Function to display the status of the linked list and count the number of nodes
void displayAndCountNodes(Student* head) {
cout << "Student Data in the Linked List:\n";
Student* temp = head;
int count = 0;
while (temp) {
cout << "USN: " << temp->USN << ", Name: " << temp->Name << ", Branch: " << temp->Branch
<< ", Semester: " << temp->Sem << ", Phone Number: " << temp->PhNo << "\n";
temp = temp->next;
count++;
}
cout << "Number of nodes in the Linked List: " << count << "\n";
}
// Function to delete a Student node from the front of the list
void deleteFromFront(Student*& head) {
if (!head) {
cout << "Linked List is empty. Cannot delete from front.\n";
return;
}
Student* temp = head;
head = head->next;
delete temp;
cout << "Student data deleted from the front.\n";
}
// Function to delete a Student node from the end of the list
void deleteFromEnd(Student*& head) {
if (!head) {
cout << "Linked List is empty. Cannot delete from end.\n";
return;
}
if (!head->next) {
delete head;
head = NULL; // Replace nullptr with NULL
cout << "Student data deleted from the end.\n";
return;
}
Student* temp = head;
while (temp->next->next) {
temp = temp->next;
}
delete temp->next;
temp->next = NULL; // Replace nullptr with NULL
cout << "Student data deleted from the end.\n";
20 | P a g e
}
// Function to delete all nodes from the linked list
void deleteLinkedList(Student*& head) {
while (head) {
Student* temp = head;
head = head->next;
delete temp;
}
cout << "Linked List deleted.\n";
}
int main() {
Student* head = NULL; // Replace nullptr with NULL
char choice;
cout<<"Roll No. 23/MCA/86"<<endl;
do {
cout << "\nMenu:\n";
cout << "a. Create a SLL of N Students Data by using front insertion.\n";
cout << "b. Display the status of SLL and count the number of nodes in it.\n";
cout << "c. Perform Insertion at End / Deletion from End of SLL.\n";
cout << "d. Perform Insertion at Front / Deletion from Front of SLL (Demonstration of stack).\n";
cout << "e. Exit.\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a':
int n;
cout << "Enter the number of students: ";
cin >> n;
for (int i=0;i<n;i++) {
insertAtFront(head);
}
break;
case 'b':
displayAndCountNodes(head);
break;
case 'c':
char subChoiceC;
cout << "c. Perform Insertion at End / Deletion from End of SLL.\n";
cout << "1. Insert at End.\n";
cout << "2. Delete from End.\n";
cout << "Enter your choice: ";
cin >> subChoiceC;
switch (subChoiceC) {
case '1':
insertAtEnd(head);
break;
case '2':
deleteFromEnd(head);
break;
default:
cout << "Invalid choice.\n";
break;
}
break;
case 'd':
21 | P a g e
char subChoiceD;
cout << "d. Perform Insertion at Front / Deletion from Front of SLL (Demonstration of stack).\n";
cout << "1. Insert at Front.\n";
cout << "2. Delete from Front.\n";
cout << "Enter your choice: ";
cin >> subChoiceD;
switch (subChoiceD) {
case '1':
insertAtFront(head);
break;
case '2':
deleteFromFront(head);
break;
default:
cout << "Invalid choice.\n";
break;
}
break;
case 'e':
deleteLinkedList(head);
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 'e');

return 0;
}
Output:

22 | P a g e
8. Design, Develop and Implement a menu driven Program in C++ for the following operations on Doubly
Linked List(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue
f. Exit
Roll No. 24/MCA/66. */

#include<iostream>
#include<limits>
using namespace std;
// Employee structure
struct Employee {
int SSN;
string Name;
string Dept;
string Designation;
double Sal;
long long PhNo;
Employee* next;
Employee* prev;
};
// Doubly Linked List class
class DoublyLinkedList {
private:
Employee* head;
public:
// Constructor
DoublyLinkedList() {
head = NULL; // Replace nullptr with NULL
}
// Function to create a DLL of N Employees Data by using end insertion
void createDLL(int N) {
for (int i = 0; i < N; ++i) {
Employee* newEmployee = new Employee;
cout << "Enter Employee " << i + 1 << " Data:" << endl;
cout << "SSN: "; cin >> newEmployee->SSN;
cout << "Name: "; cin.ignore(); getline(cin, newEmployee->Name);
cout << "Dept: "; getline(cin, newEmployee->Dept);
cout << "Designation: "; getline(cin, newEmployee->Designation);
cout << "Salary: "; cin >> newEmployee->Sal;
cout << "Phone Number: "; cin >> newEmployee->PhNo;
// Input validation for phone number
while (true) {
cout << "Phone Number: ";
if (cin >> newEmployee->PhNo) {
break; // Valid input, exit the loop
} else {
cout << "Invalid input. Please enter a valid integer for the phone number.\n";
cin.clear(); // Clear error flag
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Discard invalid input
}
23 | P a g e
}
newEmployee->next = NULL; // Replace nullptr with NULL
newEmployee->prev = NULL; // Replace nullptr with NULL
if (head == NULL) { // Replace nullptr with NULL
head = newEmployee;
} else {
Employee* temp = head;
while (temp->next != NULL) { // Replace nullptr with NULL
temp = temp->next;
}
temp->next = newEmployee;
newEmployee->prev = temp;
}
}
}

// Function to display the status of DLL and count the number of nodes in it
void displayAndCount() {
cout << "\nDoubly Linked List Status:\n";
Employee* temp = head;
int count = 0;
while (temp != NULL) { // Replace nullptr with NULL
cout << "SSN: " << temp->SSN << ", Name: " << temp->Name << ", Dept: " << temp->Dept
<< ", Designation: " << temp->Designation << ", Salary: " << temp->Sal
<< ", Phone Number: " << temp->PhNo << endl;
temp = temp->next;
count++;
}
cout << "Number of nodes in the Doubly Linked List: " << count << endl;
}
// Function to perform Insertion at End of DLL
void insertAtEnd() {
Employee* newEmployee = new Employee;
cout << "Enter Employee Data to Insert at End:\n";
cout << "SSN: "; cin >> newEmployee->SSN;
cout << "Name: "; cin.ignore(); getline(cin, newEmployee->Name);
cout << "Dept: "; getline(cin, newEmployee->Dept);
cout << "Designation: "; getline(cin, newEmployee->Designation);
cout << "Salary: "; cin >> newEmployee->Sal;
cout << "Phone Number: "; cin >> newEmployee->PhNo;
newEmployee->next = NULL; // Replace nullptr with NULL
newEmployee->prev = NULL; // Replace nullptr with NULL
if (head == NULL) { // Replace nullptr with NULL
head = newEmployee;
} else {
Employee* temp = head;
while (temp->next != NULL) { // Replace nullptr with NULL
temp = temp->next;
}
temp->next = newEmployee;
newEmployee->prev = temp;
}
cout << "Employee Inserted at End Successfully!\n";
}
// Function to perform Deletion at End of DLL
24 | P a g e
void deleteAtEnd() {
if (head == NULL) { // Replace nullptr with NULL
cout << "DLL is empty. Deletion not possible.\n";
} else if (head->next == NULL) { // Replace nullptr with NULL
delete head;
head = NULL; // Replace nullptr with NULL
cout << "Last Employee Deleted Successfully!\n";
} else {
Employee* temp = head;
while (temp->next->next != NULL) { // Replace nullptr with NULL
temp = temp->next;
}
delete temp->next;
temp->next = NULL; // Replace nullptr with NULL
cout << "Last Employee Deleted Successfully!\n";
}
}
// Function to perform Insertion at Front of DLL
void insertAtFront() {
Employee* newEmployee = new Employee;
cout << "Enter Employee Data to Insert at Front:\n";
cout << "SSN: "; cin >> newEmployee->SSN;
cout << "Name: "; cin.ignore(); getline(cin, newEmployee->Name);
cout << "Dept: "; getline(cin, newEmployee->Dept);
cout << "Designation: "; getline(cin, newEmployee->Designation);
cout << "Salary: "; cin >> newEmployee->Sal;
cout << "Phone Number: "; cin >> newEmployee->PhNo;
newEmployee->next = head;
newEmployee->prev = NULL; // Replace nullptr with NULL
if (head != NULL) { // Replace nullptr with NULL
head->prev = newEmployee;
}
head = newEmployee;
cout << "Employee Inserted at Front Successfully!\n";
}
// Function to perform Deletion at Front of DLL
void deleteAtFront() {
if (head == NULL) { // Replace nullptr with NULL
cout << "DLL is empty. Deletion not possible.\n";
} else {
Employee* temp = head;
head = head->next;
if (head != NULL) { // Replace nullptr with NULL
head->prev = NULL; // Replace nullptr with NULL
}
delete temp;
cout << "First Employee Deleted Successfully!\n";
}
}
// Function to demonstrate how this DLL can be used as Double Ended Queue
void doubleEndedQueueDemo() {
int choice;
do {
cout << "\nDouble Ended Queue Operations:\n";
cout << "1. Insert at Front\n";
25 | P a g e
cout << "2. Delete at Front\n";
cout << "3. Insert at End\n";
cout << "4. Delete at End\n";
cout << "5. Display and Count\n";
cout << "6. Back to Main Menu\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
insertAtFront();
break;
case 2:
deleteAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
deleteAtEnd();
break;
case 5:
displayAndCount();
break;
case 6:
cout << "Exiting Double Ended Queue Operations...\n";
break;
default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 6);
}
};

// Main function
int main() {
DoublyLinkedList employeeList;
int choice, N;
cout<<"Roll No. 24/MCA/66"<<endl;
do {
cout << "\nMenu:\n";
cout << "1. Create DLL of N Employees\n";
cout << "2. Display and Count DLL\n";
cout << "3. Insert at End\n";
cout << "4. Delete at End\n";
cout << "5. Insert at Front\n";
cout << "6. Delete at Front\n";
cout << "7. Use DLL as Double Ended Queue\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the number of employees (N): ";
cin >> N;
employeeList.createDLL(N);
26 | P a g e
break;
case 2:
employeeList.displayAndCount();
break;
case 3:
employeeList.insertAtEnd();
break;
case 4:
employeeList.deleteAtEnd();
break;
case 5:
employeeList.insertAtFront();
break;
case 6:
employeeList.deleteAtFront();
break;
case 7:
employeeList.doubleEndedQueueDemo();
break;
case 8:
cout << "Exiting program...\n";
break;
default:
cout << "Invalid choice! Please enter a valid option.\n";
}
} while (choice != 8);

return 0;
}

27 | P a g e
Output:

9. Design, Develop and Implement a Program in C++ for the following operations on Singly Circular Linked
List (SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in
POLYSUM(x,y,z).
Roll No. 24/MCA/66. */

#include <iostream>
#include <cmath>
using namespace std;
// Structure for representing each term in the polynomial
struct Term {
int coefficient;
int x_exponent;
int y_exponent;
int z_exponent;
Term* next;
};
// Function to insert a term into the polynomial
void insertTerm(Term*& header, int coeff, int x_exp, int y_exp, int z_exp) {
Term* newTerm = new Term;
28 | P a g e
newTerm->coefficient = coeff;
newTerm->x_exponent = x_exp;
newTerm->y_exponent = y_exp;
newTerm->z_exponent = z_exp;
// Find the correct position to insert the term
Term* current = header;
while (current->next != header &&
current->next->x_exponent <= x_exp) {
current = current->next;
}
// Insert the term after the current position
newTerm->next = current->next;
current->next = newTerm;
}
// Function to display the polynomial
void displayPolynomial(Term* header) {
Term* current = header->next;
while (current != header) {
cout << current->coefficient << "x^" << current->x_exponent
<< "y^" << current->y_exponent << "z^" << current->z_exponent;
current = current->next;
if (current != header) {
cout << " + ";
}
}
cout << endl;
}
// Function to evaluate the polynomial for given values of x, y, z
int evaluatePolynomial(Term* header, int x, int y, int z) {
int result = 0;
Term* current = header->next;
while (current != header) {
result += current->coefficient * pow(x, current->x_exponent)

29 | P a g e
* pow(y, current->y_exponent) * pow(z, current->z_exponent);
current = current->next;
}
return result;
}

// Function to add two polynomials and store the result in a new polynomial
Term* addPolynomials(Term* header1, Term* header2) {
Term* resultHeader = new Term;
resultHeader->next = resultHeader;
Term* current1 = header1->next;
Term* current2 = header2->next;
while (current1 != header1 && current2 != header2) {
if (current1->x_exponent == current2->x_exponent &&
current1->y_exponent == current2->y_exponent &&
current1->z_exponent == current2->z_exponent) {
insertTerm(resultHeader, current1->coefficient + current2->coefficient,
current1->x_exponent, current1->y_exponent, current1->z_exponent);
current1 = current1->next;
current2 = current2->next;
} else if (current1->x_exponent < current2->x_exponent ||
(current1->x_exponent == current2->x_exponent &&
current1->y_exponent < current2->y_exponent) ||
(current1->x_exponent == current2->x_exponent &&
current1->y_exponent == current2->y_exponent &&
current1->z_exponent < current2->z_exponent)) {
insertTerm(resultHeader, current1->coefficient,
current1->x_exponent, current1->y_exponent, current1->z_exponent);
current1 = current1->next;
} else {
insertTerm(resultHeader, current2->coefficient,
current2->x_exponent, current2->y_exponent, current2->z_exponent);
current2 = current2->next;
}
}
// Add any remaining terms from poly1 or poly2
while (current1 != header1) {
insertTerm(resultHeader, current1->coefficient,
current1->x_exponent, current1->y_exponent, current1->z_exponent);
current1 = current1->next;
}
while (current2 != header2) {
insertTerm(resultHeader, current2->coefficient,
current2->x_exponent, current2->y_exponent, current2->z_exponent);
current2 = current2->next;
}
return resultHeader;
}
// Function to delete all nodes in the linked list
void deleteLinkedList(Term*& header) {
Term* current = header->next;
while (current != header) {
Term* temp = current;
current = current->next;
delete temp;
30 | P a g e
}
delete header;
header = NULL; // Use NULL instead of nullptr
}
int main() {
Term* poly1Header = new Term;
poly1Header->next = poly1Header;
cout<<"Roll No. 24/MCA/66"<<endl;
// Represent and Evaluate Polynomial P(x,y,z) = 6x^2y^2z - 4yz^5 + 3x^3yz + 2xy^5z - 2xyz^3
insertTerm(poly1Header, 6, 2, 2, 1);
insertTerm(poly1Header, -4, 0, 1, 5);
insertTerm(poly1Header, 3, 3, 1, 1);
insertTerm(poly1Header, 2, 1, 5, 1);
insertTerm(poly1Header, -2, 1, 1, 3);
cout << "Polynomial P(x,y,z): ";
displayPolynomial(poly1Header);
Term* poly2Header = new Term;
poly2Header->next = poly2Header;
// Represent and Evaluate another Polynomial POLY2(x,y,z) = 2x^3yz - y^4z^2 + 5xyz
insertTerm(poly2Header, 2, 3, 1, 1);
insertTerm(poly2Header, -1, 0, 4, 2);
insertTerm(poly2Header, 5, 1, 1, 1);
cout << "Polynomial POLY2(x,y,z): ";
displayPolynomial(poly2Header);
// Find the sum of two polynomials and store the result in POLYSUM(x,y,z)
Term* polySumHeader = addPolynomials(poly1Header, poly2Header);
cout << "Sum of Polynomials POLY1 and POLY2: ";
displayPolynomial(polySumHeader);
// Clean up: Delete linked lists
deleteLinkedList(poly1Header);
deleteLinkedList(poly2Header);
deleteLinkedList(polySumHeader);
return 0;
}
Output:

31 | P a g e
10. Design, Develop and Implement a menu driven Program in C++ for the following operations on Binary
Search Tree(BST) of Integers
a. Create a BST of N Integers: 6,9,5,2,8,15,24,14,7,8,5,2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (Key) and report the appropriate message
d. Exit
Roll No. 24/MCA/66. */

#include <iostream>
using namespace std;
// Structure to represent a node in the BST
struct Node {
int data;
Node* left;
Node* right;
};
// Function to create a new node in the BST
Node* createNode(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->left = NULL; // Replace nullptr with NULL
newNode->right = NULL; // Replace nullptr with NULL
return newNode;
}
// Function to insert a value into the BST
Node* insert(Node* root, int value) {
if (root == NULL) { // Replace nullptr with NULL
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
// Function to perform Inorder traversal of the BST
void inorderTraversal(Node* root) {
if (root != NULL) { // Replace nullptr with NULL
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
}

// Function to perform Preorder traversal of the BST


void preorderTraversal(Node* root) {
if (root != NULL) { // Replace nullptr with NULL
cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}
// Function to perform Postorder traversal of the BST

32 | P a g e
void postorderTraversal(Node* root) {
if (root != NULL) { // Replace nullptr with NULL
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";
}
}
// Function to search for a key in the BST
bool searchBST(Node* root, int key) {
if (root == NULL) { // Replace nullptr with NULL
return false;
}
if (key == root->data) {
return true;
} else if (key < root->data) {
return searchBST(root->left, key);
} else {
return searchBST(root->right, key);
}
}
// Function to display menu options
void displayMenu() {
cout << "\nMenu:\n";
cout << "a. Create a BST of N integers\n";
cout << "b. Traverse the BST in Inorder, Preorder, and Postorder\n";
cout << "c. Search the BST for a given element (KEY)\n";
cout << "d. Exit\n";
}
int main() {
Node* root = NULL; // Replace nullptr with NULL
cout<<"Roll No. 24/MCA/66"<<endl;
char choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 'a': {
int n;
cout << "Enter the number of integers: ";
cin >> n;
cout << "Enter the integers: ";
for (int i = 0; i < n; ++i) {
int value;
cin >> value;
root = insert(root, value);
}
cout << "BST created successfully.\n";
break;
}
case 'b': {
cout << "Inorder Traversal: ";
inorderTraversal(root);
cout << "\nPreorder Traversal: ";
preorderTraversal(root);
33 | P a g e
cout << "\nPostorder Traversal: ";
postorderTraversal(root);
cout << endl;
break;
}
case 'c': {
int key;
cout << "Enter the element to search: ";
cin >> key;
if (searchBST(root, key)) {
cout << key << " found in the BST.\n";
} else {
cout << key << " not found in the BST.\n";
}
break;
}
case 'd': {
cout << "Exiting the program.\n";
break;
}
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
} while (choice != 'd');

return 0;
}
Output:

11. Design, Develop and Implement a Program in C++ for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method
Roll No. 24/MCA/66*/

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Function to add a directed edge between two cities

34 | P a g e
void addEdge(vector<vector<bool> >& adjacencyMatrix, int from, int to) {
adjacencyMatrix[from][to] = true;
}
// Function to print all nodes reachable from a given starting node using DFS
void DFS(vector<vector<bool> >& adjacencyMatrix, vector<bool>& visited, vector<string>& cityNames, int
startNode) {
cout << cityNames[startNode] << " ";
visited[startNode] = true;
for (int i = 0; i < cityNames.size(); ++i) {
if (adjacencyMatrix[startNode][i] && !visited[i]) {
DFS(adjacencyMatrix, visited, cityNames, i);
}
}
}
// Function to print all nodes reachable from a given starting node using BFS
void BFS(vector<vector<bool> >& adjacencyMatrix, vector<string>& cityNames, int startNode) {
vector<bool> visited(cityNames.size(), false);
queue<int> bfsQueue;
cout << "BFS traversal starting from " << cityNames[startNode] << ": ";
visited[startNode] = true;
bfsQueue.push(startNode);
while (!bfsQueue.empty()) {
int current = bfsQueue.front();
bfsQueue.pop();
cout << cityNames[current] << " ";
for (int i = 0; i < cityNames.size(); ++i) {
if (adjacencyMatrix[current][i] && !visited[i]) {
visited[i] = true;
bfsQueue.push(i);
}
}
}
cout << endl;
}
int main() {
int numCities;
cout<<"Roll No. 24/MCA/66"<<endl;
cout << "Enter the number of cities: ";
cin >> numCities;
// Create an adjacency matrix to represent the graph
vector<vector<bool> > adjacencyMatrix(numCities, vector<bool>(numCities, false));
// Vector to store city names
vector<string> cityNames(numCities);
// Adding cities to the graph

35 | P a g e
for (int i = 0; i < numCities; ++i) {
cout << "Enter the name of city " << i + 1 << ": ";
cin >> cityNames[i];
}
// Adding edges between cities (example edges for demonstration)
addEdge(adjacencyMatrix, 0, 1);
addEdge(adjacencyMatrix, 0, 2);
addEdge(adjacencyMatrix, 1, 3);
addEdge(adjacencyMatrix, 2, 1);
addEdge(adjacencyMatrix, 3, 2);
// Printing all nodes reachable from a given starting node using DFS
int startNode;
cout << "Enter the starting node (index): ";
cin >> startNode;
cout << "DFS traversal starting from " << cityNames[startNode] << ": ";
vector<bool> visitedDFS(numCities, false);
DFS(adjacencyMatrix, visitedDFS, cityNames, startNode);
cout << endl;
// Printing all nodes reachable from a given starting node using BFS
BFS(adjacencyMatrix, cityNames, startNode);
return 0;
}

Output:

36 | P a g e
12. Given a File of N employee records with a set K of Keys(4-digit) which uniquely determine the records
in file F. Assume that file F is maintained in memory by a Hash Table(HT) of m memory locations with L as
the set of memory addresses( 2-digit) of locations in HT.
Let the keys in K and addresses in L are Integers. Design and develop a program in C++ that uses Hash
function H:K→ L as H(K)=K mod m ( remainder method), and implement hashing technique to map a
given key K to the address space L. Resolve the collision(if any) using linear probing.
Roll No. 24/MCA/66. */
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

// Define the size of the hash table (m memory locations)


#define TABLE_SIZE 10

// Class to represent the hash table


class HashTable {
private:
vector<int> table; // Hash table to store keys
vector<bool> isOccupied; // To track occupied slots

public:
// Constructor to initialize the hash table
HashTable() {
table.resize(TABLE_SIZE, -1); // Initialize table with -1 (indicating empty)
isOccupied.resize(TABLE_SIZE, false);
}

// Hash function: H(K) = K mod TABLE_SIZE


int hashFunction(int key) {
return key % TABLE_SIZE;
}

// Function to insert a key into the hash table


void insert(int key) {
int index = hashFunction(key);

// Linear probing to resolve collision


int startIndex = index;
while (isOccupied[index]) {
index = (index + 1) % TABLE_SIZE; // Move to the next index
if (index == startIndex) {
cout << "Hash Table is full. Cannot insert key: " << key << endl;
return;
}
}

table[index] = key;
isOccupied[index] = true;
cout << "Inserted key " << key << " at index " << index << endl;
}

// Function to search for a key in the hash table


int search(int key) {

37 | P a g e
int index = hashFunction(key);

// Linear probing to find the key


int startIndex = index;
while (isOccupied[index]) {
if (table[index] == key) {
return index;
}
index = (index + 1) % TABLE_SIZE;
if (index == startIndex) {
break; // Key not found
}
}

return -1; // Key not found


}

// Function to display the hash table


void display() {
cout << "\nHash Table:\n";
cout << setw(5) << "Index" << setw(10) << "Key\n";
for (int i = 0; i < TABLE_SIZE; i++) {
cout << setw(5) << i << setw(10);
if (isOccupied[i]) {
cout << table[i] << endl;
} else {
cout << "-" << endl;
}
}
}
};

int main() {
HashTable ht;
int choice, key;

do {
cout << "\nMenu:\n";
cout << "1. Insert Key\n";
cout << "2. Search Key\n";
cout << "3. Display Hash Table\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter a 4-digit key to insert: ";
cin >> key;
ht.insert(key);
break;

case 2:
cout << "Enter a 4-digit key to search: ";
cin >> key;

38 | P a g e
int index;
index = ht.search(key);
if (index != -1) {
cout << "Key " << key << " found at index " << index << endl;
} else {
cout << "Key " << key << " not found in the hash table.\n";
}
break;

case 3:
ht.display();
break;

case 4:
cout << "Exiting program. Goodbye!\n";
break;

default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);

return 0;
}
Output:

39 | P a g e

You might also like