0% found this document useful (0 votes)
3 views17 pages

cpp3

The document contains C++ programs for various data structure operations, including converting infix expressions to postfix and prefix, implementing simple and circular queue operations, and managing double-ended queues with both input and output restrictions. Each program includes code snippets demonstrating the functionality of the respective data structures. The document serves as a practical guide for implementing these algorithms in C++.

Uploaded by

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

cpp3

The document contains C++ programs for various data structure operations, including converting infix expressions to postfix and prefix, implementing simple and circular queue operations, and managing double-ended queues with both input and output restrictions. Each program includes code snippets demonstrating the functionality of the respective data structures. The document serves as a practical guide for implementing these algorithms in C++.

Uploaded by

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

C++ Programming Language

Journal Ques:
13. Write Program to convert Infix to Postfix Expression.

Code
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <unordered_map>
using namespace std;
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
string infixToPostfix(const string& expression) {
string output;
stack<char> s;

for (char ch : expression) {


if (isalnum(ch)) {
output += ch;
}
else if (ch == '(') {
s.push(ch);
}
else if (ch == ')') {
while (!s.empty() && s.top() != '(') {
output += s.top();
s.pop();
}
s.pop();
}
else {
while (!s.empty() && precedence(s.top()) >= precedence(ch)) {
output += s.top();
s.pop();
}
s.push(ch);
}
}
while (!s.empty()) {
output += s.top();
s.pop();
}
return output;
}
int main() {
string infixExpression;
cout << "Enter an infix expression: ";
cin >> infixExpression;

string postfixExpression = infixToPostfix(infixExpression);


cout << "Postfix Expression: " << postfixExpression << endl;

return 0;
}
14. Write Program to convert Infix to Prefix Expression.

Code
#include <iostream>
#include <stack>
#include <string>
#include <algorithm>

// Function to check operator precedence


int precedence(char op) {
if (op == '+' || op == '-') {
return 1;
} else if (op == '*' || op == '/') {
return 2;
} else if (op == '^') {
return 3;
}
return 0;
}
std::string infixToPrefix(const std::string& infix) {
std::stack<char> stack;
std::string prefix = "";
std::string infixRev = infix;
std::reverse(infixRev.begin(), infixRev.end());

for (char c : infixRev) {


if (c == ' ' || c == '\t') {
continue;
}
if (isalnum(c)) {
prefix += c;
}
else if (c == ')') {
stack.push(c);
}
else if (c == '(') {
while (stack.top() != ')') {
prefix += stack.top();
stack.pop();
}
stack.pop();
}
else {
while (!stack.empty() && precedence(c) <= precedence(stack.top())) {
prefix += stack.top();
stack.pop();
}
stack.push(c);
}
}
while (!stack.empty()) {
prefix += stack.top();
stack.pop();
}
std::reverse(prefix.begin(), prefix.end());

return prefix;
}

int main() {
std::string infix;
std::cout << "Enter infix expression: ";
std::cin >> infix;

std::string prefix = infixToPrefix(infix);


std::cout << "Prefix expression: " << prefix << std::endl;
return 0;
}

15. Write Program to implement Simple Queue Operations like Insert, Delete and
Display.

Code
#include<iostream>
using namespace std;
#define max 5
class Queue{
private:
int arr[max];
int front,rear,size;
public:
Queue():front(0),rear(-1),size(0){}
bool isEmpty(){
return size==0;
}
bool isFull(){
return size==max;
}
void enqueue(int element){
if(isFull()){
cout<<"queue is full!"<<endl;
return;
}
rear=(rear+1)%max;
arr[rear]=element;
size++;
cout<<"inserted"<<element<<endl;
}
void dequeue() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return;
}
front = (front + 1) % max;
size--;
cout << "Deleted element" << endl;
}

void display(){
if(isEmpty()){
cout<<"queue is empty"<<endl;
return;
}
cout<<"queue elements:";
for(int i=0;i<size;i++){
cout<<arr[(front+i)%max]<<"";
}
cout<<endl;
}
};
int main(){
Queue q;
int choice,element;
do{
cout<<"queue operations menu:"<<endl;
cout<<"1.insert(enqueue)"<<endl;
cout<<"2.delete(dequeue)"<<endl;
cout<<"3.display"<<endl;
cout<<"4.exit"<<endl;
cout<<"enter your choice:";
cin>>choice;
switch(choice){
case 1:
cout<<"enter elements to insert:";
cin>>element;
q.enqueue(element);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
cout<<"existing program"<<endl;
break;
default:
cout<<"invalid choice! please try again."<<endl;
}
}
while(choice!=4);
return 0;
}

16. Write Program to implement Circular Queue Operations like Insert, Delete and
Display.

Code
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class CircularQueue {
private:
int arr[MAX_SIZE];
int front, rear, size;

public:
CircularQueue() : front(0), rear(0), size(0) {}
void enqueue(int value) {
if (size == MAX_SIZE) {
cout << "Queue is full. Cannot insert." << endl;
return;
}

arr[rear] = value;
rear = (rear + 1) % MAX_SIZE;
size++;
cout << "Inserted: " << value << endl;
}
int dequeue() {
if (size == 0) {
cout << "Queue is empty. Cannot delete." << endl;
return -1;
}

int value = arr[front];


front = (front + 1) % MAX_SIZE;
size--;
cout << "Deleted: " << value << endl;
return value;
}
void display() {
if (size == 0) {
cout << "Queue is empty." << endl;
return;
}
cout << "Queue elements: ";
int temp = front;
for (int i = 0; i < size; i++) {
cout << arr[temp] << " ";
temp = (temp + 1) % MAX_SIZE;
}
cout << endl;
}
bool isEmpty() {
return size == 0;
}
bool isFull() {
return size == MAX_SIZE;
}
};

int main() {
CircularQueue queue;
while (true) {
cout << "\nCircular Queue Operations:" << endl;
cout << "1. Enqueue (Insert)" << endl;
cout << "2. Dequeue (Delete)" << endl;
cout << "3. Display" << endl;
cout << "4. Exit" << endl;
cout << "Enter choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1: {
int value;
cout << "Enter value to insert: ";
cin >> value;
queue.enqueue(value);
break;
}
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}

17. Write Program to implement Double Ended Queue Operations like Insert, Delete
and Display using class and object(To Perform Input Restricted).

Code
#include <iostream>
using namespace std;
const int MAX_SIZE = 5;
class Deque {
private:
int arr[MAX_SIZE];
int front, rear, size;
public:
Deque() : front(0), rear(0), size(0) {}
void insertFront(int value) {
if (size == MAX_SIZE) {
cout << "Deque is full. Cannot insert." << endl;
return;
}
if (front == 0) {
front = MAX_SIZE - 1;
} else {
front--;
}
arr[front] = value;
size++;
cout << "Inserted at front: " << value << endl;
}
void insertRear(int value) {
if (size == MAX_SIZE) {
cout << "Deque is full. Cannot insert." << endl;
return;
}
if (rear == MAX_SIZE - 1) {
rear = 0;
} else {
rear++;
}
arr[rear] = value;
size++;
cout << "Inserted at rear: " << value << endl;
}
int deleteFront() {
if (size == 0) {
cout << "Deque is empty. Cannot delete." << endl;
return -1;
}
int value = arr[front];
if (front == MAX_SIZE - 1) {
front = 0;
} else {
front++;
}
size--;
cout << "Deleted from front: " << value << endl;
return value;
}
int deleteRear() {
if (size == 0) {
cout << "Deque is empty. Cannot delete." << endl;
return -1;
}
int value = arr[rear];
if (rear == 0) {
rear = MAX_SIZE - 1;
} else {
rear--;
}
size--;
cout << "Deleted from rear: " << value << endl;
return value;
}
void display() {
if (size == 0) {
cout << "Deque is empty." << endl;
return;
}
cout << "Deque elements: ";
int temp = front;
for (int i = 0; i < size; i++) {
cout << arr[temp] << " ";
if (temp == MAX_SIZE - 1) {
temp = 0;
} else {
temp++;
}
}
cout << endl;
}
};
int main() {
Deque deque;

while (true) {
cout << "\nDeque Operations:" << endl;
cout << "1. Insert at Front" << endl;
cout << "2. Insert at Rear" << endl;
cout << "3. Delete from Front" << endl;
cout << "4. Delete from Rear" << endl;
cout << "5. Display" << endl;
cout << "6. Exit" << endl;
cout << "Enter choice: ";

int choice;
cin >> choice;

switch (choice) {
case 1: {
int value;
cout << "Enter value to insert at front: ";
cin >> value;
deque.insertFront(value);
break;
}
case 2: {
int value;
cout << "Enter value to insert at rear: ";
cin >> value;
deque.insertRear(value);
break;
}
case 3:
deque.deleteFront();
break;
case 4:
deque.deleteRear();
break;
case 5:
deque.display();
break;
case 6:
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}

18. Write Program to implement Double Ended Queue Operations like Insert, Delete
and Display using class and object(To Perform Output Restricted).

Code
#include <iostream>
using namespace std;

class Deque {
private:
int* arr;
int front, rear, size, capacity;

public:
Deque(int cap) : capacity(cap), size(0), front(-1), rear(0) {
arr = new int[capacity];
}
~Deque() {
delete[] arr;
}

void insertFront(int element) {


if (size == capacity) {
cout << "Deque is full. Cannot insert at the front." << endl;
return;
}
if (size == 0) {
front = 0;
rear = 0;
} else {
front = (front - 1 + capacity) % capacity;
}
arr[front] = element;
size++;
}

void insertRear(int element) {


if (size == capacity) {
cout << "Deque is full. Cannot insert at the rear." << endl;
return;
}
if (size == 0) {
front = 0;
rear = 0;
} else {
rear = (rear + 1) % capacity;
}
arr[rear] = element;
size++;
}

void deleteFront() {
if (size == 0) {
cout << "Deque is empty. Cannot delete from the front." << endl;
return;
}
front = (front + 1) % capacity;
size--;
}

void deleteRear() {
if (size == 0) {
cout << "Deque is empty. Cannot delete from the rear." << endl;
return;
}
rear = (rear - 1 + capacity) % capacity;
size--;
}

void display() {
cout << "Deque elements: ";
for (int i = 0; i < size; i++) {
cout << arr[(front + i) % capacity] << " ";
}
cout << endl;
}
};

int main() {
Deque Dq(5); // Change capacity from 0 to 5
cout << "Inserting elements at the rear:" << endl;
Dq.insertRear(20);
Dq.insertRear(30);
Dq.display();

cout << "Inserting elements at the front:" << endl;


Dq.insertFront(40);
Dq.insertFront(50);
Dq.display();

cout << "Deleting element from the front:" << endl;


Dq.deleteFront();
Dq.display();

cout << "Deleting element from the rear:" << endl;


Dq.deleteRear();
Dq.display();
}

You might also like