0% found this document useful (0 votes)
8 views22 pages

FDS Codes

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

FDS Codes

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

Name: Dnyanraj Sonawane Roll no.

:COSB60
Assignment No. 10
INPUT:
#include <iostream>
#include <cstring>
#include <stack> using
namespace std; int
getWeight(char ch)
{
switch (ch)
{
case '/': case '*': return 2; case
'+': case '-': return 1; default:
return 0;
}
}
void infix2postfix(const char infix[], char postfix[], int size)
{
stack<char> s; int
weight;
int i = 0; int k
= 0; char ch;
while (i < size)
{
ch = infix[i]; if
(ch == '(') {
s.push(ch);
} else if (ch ==
')')
{
while (!s.empty() && s.top() != '(')
{
postfix[k++] = s.top();
s.pop();
}
if (!s.empty())
{
s.pop();
}
} else
{
weight = getWeight(ch); if
(weight == 0)
{
postfix[k++] = ch;
} else
{
while (!s.empty() && s.top() != '(' && weight <= getWeight(s.top()))
{
postfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
} i++;
}
while (!s.empty())
{
postfix[k++] = s.top();
s.pop();
} postfix[k] = '\
0';
}
int main()
{
char infix[100]; cout << "Enter the Infix
Expression: "; cin >> infix; int size =
strlen(infix); char postfix[size + 1];
infix2postfix(infix, postfix, size); cout << "\
nInfix Expression: " << infix; cout << "\
nPostfix Expression: " << postfix; cout <<
endl; return 0;
}
OUTPUT:
Enter the Infix Expression: A*(B+C)/D
Infix Expression: A*(B+C)/D
Postfix Expression: ABC+*D/
Name: Dnyanraj Sonawane Roll no.:COSB69
Assignment No. 11
INPUT:
#include <iostream>
#include <string> class
JobQueue
{
std::string* queue; int front, rear,
maxSize, currentSize; public:
JobQueue(int size) : maxSize(size), front(0), rear(0), currentSize(0)
{
queue = new std::string[maxSize];
}
~JobQueue()
{
delete[] queue;
}
bool isFull() const
{
return currentSize == maxSize;
}
bool isEmpty() const
{
return currentSize == 0;
}
void addJob(const std::string& job)
{
if (isFull())
{
std::cout << "\nQueue is full ! Cannot add job.\n"; return;
}
queue[rear] = job; rear = (rear + 1) %
maxSize; currentSize++; std::cout <<
"Job added: " << job << "\n";
}
void deleteJob(const std::string& job)
{
if (isEmpty())
{
std::cout << "\nNo jobs to delete.\n"; return;
} for (int i = 0; i < currentSize; i+
+)
{
int index = (front + i) % maxSize; if
(queue[index] == job)
{
std::cout << "Job deleted: " << queue[index] << "\n";
for (int j = i; j < currentSize - 1; j++) { queue[(front + j) %
maxSize] = queue[(front + j + 1) % maxSize]; }

rear = (rear - 1 + maxSize) % maxSize;


currentSize--; return;
}
}
std::cout << "\nJob not found: " << job << "\n";
}
void displayJobs() const
{
if (isEmpty())
{
std::cout << "\nNo jobs in the queue.\n"; return;
}
std::cout << "Current Jobs:- \n\n"; for (int i = 0; i
< currentSize; i++) { std::cout << queue[(front + i)
% maxSize] << "\n";
}
} };
int main()
{
int maxJobs; std::cout << "Enter maximum
number of jobs: "; std::cin >> maxJobs;
JobQueue queue(maxJobs);
int choice;
std::string job; while
(true)
{
std::cout << "\n1. Add Job\n"
"2. Delete Job\n"
"3. Display Jobs"
"\n\nEnter your choice: ";
std::cin >> choice; switch
(choice)
{
case 1:
std::cout << "\nEnter job description: ";
std::cin.ignore(); std::getline(std::cin,
job); queue.addJob(job); break; case 2:
std::cout << "\nEnter job description to delete: ";
std::cin.ignore(); std::getline(std::cin, job);
queue.deleteJob(job); break; case 3:
queue.displayJobs(); break; default:

std::cout << "Invalid choice.\n";


}
}
}

OUTPUT:
Enter maximum number of jobs: 3
1. Add Job
2. Delete Job
3. Display Jobs
Enter your choice: 1
Enter job description: Data Analyst
Job added: Data Analyst

Enter your choice: 1


Enter job description: Software Tester
Job added: Software Tester

Enter your choice: 1


Enter job description: Security Manager
Job added: Security Manager

1. Add Job
2. Delete Job
3. Display Jobs
Enter your choice: 1 Enter job
description: System Designer
Queue is full ! Cannot add job.

1. Add Job
2. Delete Job
3. Display Jobs

Enter your choice: 3


Current Jobs: -
Data Analyst
Software Tester
Security Manager
1. Add Job
2. Delete Job
3. Display Jobs

Enter your choice: 2


Enter job description to delete: Data Analyst
Job deleted: Data Analyst

Enter your choice: 2


Enter job description to delete: System Designer
Job not found: System Designer
Enter your choice: 3

Current Jobs:- Software


Tester
Security Manager
Name: Dnyanraj Sonawane Roll no.:COSB69
Assignment No. 12
INPUT:
#include <iostream>
#include <stdexcept>
using namespace std; class
Dequeue
{ int* arr; int front, rear,
capacity, size; public:
Dequeue(int cap) : capacity(cap), size(0), front(-1), rear(0)
{
arr = new int[capacity];
}
~Dequeue()
{ delete[]
arr;
}
void addFront(int element)
{
if (size == capacity) throw
std::overflow_error("Dequeue is full !"); front
= (front + 1) % capacity; arr[front] = element;
size++; size++;
if (size == 1) rear = front; cout << "Inserted " << element
<< " at the front." << endl;
}
void addRear(int element)
{
if (size == capacity) throw
std::overflow_error("Dequeue is full !"); rear = (rear - 1
+ capacity) % capacity; arr[rear] = element; size++; if
(size == 1) front = rear; cout << "Inserted " << element
<< " at the rear." << endl;
}
void deleteFront()
{ if (size ==
0)
throw std::underflow_error("Dequeue is empty !");
int deletedElement = arr[front]; front = (front - 1 +
capacity) % capacity; size--; if (size == 0)
{ front = -
1; rear =
0;
}
cout << "Deleted " << deletedElement << " from the front." << endl;
}
void deleteRear()
{
if (size == 0) throw
std::underflow_error("Dequeue is empty !"); int
deletedElement = arr[rear]; rear = (rear + 1) %
capacity; size--; if (size == 0)
{
front = -1; rear
= 0;
}
cout << "Deleted " << deletedElement << " from the rear." << endl;
}
void display()
{
if (size == 0)
{
cout << "Dequeue is empty !" << endl; return;
}
cout << "Dequeue elements: ";
for (int i = 0; i < size; ++i)
{
cout << arr[(rear + i) % capacity] << " ";
}
cout << endl;
} };
int main()
{
int capacity, choice, element; cout << "Enter
the capacity of the Dequeue: "; cin >>
capacity;
Dequeue dequeue(capacity); while
(true)
{
cout << "\nEnter your choice:- "
<< "\n1. Add Front\n" <<
"2. Add Rear\n"
<< "3. Delete Front\n"
<< "4. Delete Rear\n"
<< "5. Display\n"
<< "\nChoice: ";
cin >> choice; try
{
switch (choice)
{
case 1:
cout << "\nEnter element: "; cin
>> element;
dequeue.addFront(element);
break; case 2: cout << "\nEnter
element: "; cin >> element;
dequeue.addRear(element);
break; case 3:
dequeue.deleteFront(); break;
case 4:
dequeue.deleteRear(); break;
case 5: dequeue.display(); break;
default: cout << "Invalid choice."
<< endl;
}
}
catch (const std::exception& e)
{
cerr << e.what() << endl;
}
}
return 0;
}
OUTPUT:
Enter the capacity of the Dequeue: 5
Enter your choice:-
1. Add Front
2. Add Rear
3. Delete Front
4. Delete Rear
5. Display Choice: 1 Enter element: 1
Inserted 1 at the front.

Choice: 1 Enter
element: 2
Inserted 2 at the front.
Choice: 2 Enter
element: 3
Inserted 3 at the rear.
Choice: 2 Enter
element: 4
Inserted 4 at the rear.
Enter your choice:-
1. Add Front
2. Add Rear
3. Delete Front
4. Delete Rear
5. Display
Choice: 5
Dequeue elements: 4 3 1 2

Choice: 3
Deleted 2 from the front.

Choice: 4
Deleted 4 from the rear.

Choice: 5
Dequeue elements: 3 1

Choice: 4
Deleted 3 from the rear.

Choice: 4
Deleted 1 from the rear.
Enter your choice:-
1. Add Front
2. Add Rear
3. Delete Front
4. Delete Rear
5. Display
Choice: 5
Dequeue is empty !

Name: Dnyanraj Sonawane Roll no.:COSB69


Assignment No. 13
INPUT:
#include <iostream>
#include <string> class
CircularQueue
{
private:
std::string* queue; int front, rear,
maxSize, currentSize; public:
CircularQueue(int size) : maxSize(size), front(0), rear(0), currentSize(0)
{
queue = new std::string[maxSize];
}
~CircularQueue()
{
delete[] queue;
}
bool isFull() const
{
return currentSize == maxSize;
}
bool isEmpty() const
{
return currentSize == 0;
}
void placeOrder(const std::string& order)
{
if (isFull())
{
std::cout << "\nQueue is full ! Cannot place order.\n"; return;
}
queue[rear] = order; rear = (rear + 1) %
maxSize; currentSize++; std::cout << "Order
Placed: " << order << "\n";
}
void serveOrder()
{
if (isEmpty())
{
std::cout << "\nNo orders to serve.\n"; return;
}
std::cout << "\nServing Order: " << queue[front] << "\n";
front = (front + 1) % maxSize; currentSize--;
}
void displayOrders() const
{
if (isEmpty())
{
std::cout << "\nNo orders in the queue.\n"; return;
}
std::cout << "\nCurrent Orders:- \n";
for (int i = 0; i < currentSize; i++) { std::cout <<
queue[(front + i) % maxSize] << "\n";
}
} };
int main()
{
int maxOrders; std::cout << "Enter maximum
number of orders: "; std::cin >> maxOrders;
CircularQueue queue(maxOrders);
int choice; std::string
order; while (true)
{
std::cout << "\n1. Place Order\n"
<< "2. Serve Order\n"
<< "3. Display Orders\n"
<< "\nEnter your choice: ";
std::cin >> choice; switch
(choice)
{
case 1:
std::cout << "\nEnter the order description: ";
std::cin.ignore(); std::getline(std::cin, order);
queue.placeOrder(order); break; case 2:
queue.serveOrder(); break;
case 3: queue.displayOrders();
break; default: std::cout <<
"Invalid choice.\n";
}
}
}

OUTPUT:
Enter maximum number of orders: 3
1. Place Order
2. Serve Order
3. Display Orders
Enter your choice: 1
Enter the order description: Margherita Pizza
Order Placed: Margherita Pizza

Enter your choice: 1


Enter the order description: Pepperoni Pizza
Order Placed: Pepperoni Pizza

Enter your choice: 1


Enter the order description: Veggie Pizza
Order Placed: Veggie Pizza
Enter your choice: 1

Enter the order description: Hawaiian Pizza

Queue is full ! Cannot place order.


1. Place Order
2. Serve Order
3. Display Orders
Enter your choice: 3
Current Orders:- Margherita
Pizza Pepperoni Pizza

Veggie Pizza
1. Place Order
2. Serve Order
3. Display Orders
Enter your choice: 2
Serving Order: Margherita Pizza

Enter your choice: 2


Serving Order: Pepperoni Pizza

Enter your choice: 2

Serving Order: Veggie Pizza


Enter your choice: 2
No orders to serve.

You might also like