0% found this document useful (0 votes)
13 views6 pages

Recursion and Data Structures in C++

The document contains code examples for recursion, stack, and queue data structures in C++. It includes implementations for factorial, Fibonacci, sum of numbers, Tower of Hanoi, stack operations using arrays and STL, postfix evaluation, and queue operations using arrays and STL. Each section provides a main function demonstrating the usage of the respective data structure and its operations.
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)
13 views6 pages

Recursion and Data Structures in C++

The document contains code examples for recursion, stack, and queue data structures in C++. It includes implementations for factorial, Fibonacci, sum of numbers, Tower of Hanoi, stack operations using arrays and STL, postfix evaluation, and queue operations using arrays and STL. Each section provides a main function demonstrating the usage of the respective data structure and its operations.
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

Recursion:

1.​ Factorial
2.​ Fibonacci
3.​ Sum of 10 numbers
4.​ Tower of hanoi

#include <iostream>
using namespace std;
void tower(int n, char beg, char end, char aux) {
if(n == 1){
cout<<beg<<" -> "<<end<<endl;
}
else{
tower(n-1, beg, aux, end);
cout<<beg<<" -> "<<end<<endl;
tower(n-1, aux, end, beg);
}
}
int main() {
int n = 4;
char beg = 'A';
char end = 'B';
char aux = 'C';
tower(n, beg , aux, end);
}

Stack:

1.​ Push pop using Array

#include <bits/stdc++.h>
using namespace std;
int MAX_SIZE=50;
int top = -1;

void push(int stack[], int value) {


if (top == MAX_SIZE - 1) {
cout<<"Stack overflow! Cannot push element"<<value<<endl;
} else {
top++;
stack[top] = value;
}
}

void pop(int stack[]) {


if (top == -1) {
cout<<"Stack underflow! Cannot pop from an empty stack"<<endl;
} else {
top--;
}
}

void printStack(int stack[]) {


if (top== -1) {
cout<<"Stack is empty"<<endl;
} else {
cout<<"Stack elements: ";
for (int i = 0; i <= top; i++) {
cout<< stack[i]<<" ";
}
cout<<endl;
}
}

int main() {
int stack[MAX_SIZE];
push(stack,10);
push(stack,20);
push(stack,30);
printStack(stack);
pop(stack);
printStack(stack);
return 0;
}

2.​ Using STL

#include <bits/stdc++.h>
using namespace std;

void display(stack<int>St) {
if ([Link]()) {
cout<<"Stack is empty.";
} else {
cout<<"Stack elements: ";
while(![Link]()){
cout << ' '<< [Link]();
[Link]();
}
cout<<endl;
}
}

int main() {
stack <int> St;
[Link](10);
[Link](20);
[Link](30);
display(St);
[Link]();
display(St);
return 0;
}

3.​ Postfix Evaluation

#include<bits/stdc++.h>
using namespace std;
int evaluatePostfix(string expression) {
stack<int>st;
for (int i=0; i<[Link]();i++) {
if(isdigit(expression[i])) {
[Link](expression[i]-'0'); //Convert char to int
}
else {
// Operator encountered
int A = [Link]();
[Link]();
int B = [Link]();
[Link]();
switch (expression[i]) {
case '+':
[Link](B + A);
break;
case '-':
[Link](B - A);
break;
case '*':
[Link](B * A);
break;
case '/':
[Link](B / A);
break;
default:
cout<<"Invalid operator: "<< endl;
}
}
}
return [Link]();
}

int main() {
string postfix = "562+*94/-";
cout<<"Result : "<<evaluatePostfix(postfix);
return 0;
}

QUEUE:

1.​ Insert Delete using Array

#include <bits/stdc++.h>
#define MAX_SIZE 6
using namespace std;
int front = -1, rear = -1;

void insertion(int queue[],int item) {


if (front==0 && rear == MAX_SIZE - 1 || front==rear+1) {
cout<<"Queue is full. OVERFLOW!"<<endl;
return;
}
if (front == -1) {
front = 0;
rear=0;
}
else if(rear==MAX_SIZE - 1){
rear=0;
}
else
rear++;
queue[rear] = item;
}
void deletion(int queue[]) {
if (front == -1) {
cout<<"Queue is empty. UNDERFLOW!!";
return;
}
if(front==rear){
front= -1;
rear= -1;
}
else if(front==MAX_SIZE - 1){
front=0;
}
else
front++;
}

void display(int queue[]) {


cout<<"Queue elements: ";
if(front<rear){
for (int i = front; i <= rear; i++) {
cout<<queue[i]<<" ";
}
cout<<endl;}
else{
for (int i = 0; i <= rear; i++) {
cout<<queue[i]<<" ";
}
for (int i = front; i <= MAX_SIZE - 1; i++) {
cout<<queue[i]<<" ";
}
}

int main() {
int queue[MAX_SIZE];
insertion(queue,10);
insertion(queue,20);
insertion(queue,30);
insertion(queue,40);
insertion(queue,50);
insertion(queue,60);
display(queue);
deletion(queue);
deletion(queue);
deletion(queue);
display(queue);
insertion(queue,77);
insertion(queue,88);
display(queue);
return 0;
}

4.​ Using STL

#include <bits/stdc++.h>
using namespace std;

void display(queue<int>q) {
if ([Link]()) {
cout<<"Queue is empty.";
} else {
cout<<"Queue elements: ";
while(![Link]()){
cout << ' '<< [Link]();
[Link]();
}
cout<<endl;
}
}

int main() {
queue<int>q;
[Link](10);
[Link](20);
[Link](30);
display(q);
[Link]();
display(q);
return 0;
}

You might also like