You are on page 1of 15

Assignment – 3

1. Program to implement Stack using Queue.


// Stack implementation using queue
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};

node *front = NULL;


node *rear = NULL;
// variable to store the current no. of nodes
int count=0;

int push(int x)
{
// creating new node
node *temp = new node;
temp->data = x;
temp->next = NULL;
if(front==NULL && rear == NULL)
{
front=rear = temp;
}
else
{
rear->next = temp;
rear=temp;
}
count++;
return 0;
}

int pop()
{
int p=0;
if(front == NULL)
return 0;
if(front == rear)
front=rear=NULL;
else
{
// iteratively removing element from front and
// storing in a new node at the back of linked list
for(int i=1;i<=count-1;i++)
{
node *temp = new node;
temp -> data = front->data;
temp-> next = NULL;
rear-> next = temp;
rear = temp;
front = front->next;
}
count--;
}
p = front->data;
front = front->next;
return p;
}

int main() {

//pushing elements in stack using a queue


// queue has been implemented using Linked List
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);

// popping last 3 elements of stack


int last1 = pop();
int last2 = pop();
int last3 = pop();
cout<<"The last 3 elements are: \n";
cout<<last1<<"\t"<<last2<<"\t"<<last3;
return 0;
}

Output:
2. Program to reverse a string using recursion.

// Reversing a stack using recursion and displaying


#include <iostream>
using namespace std;
struct stack
{
int size;
int top;
int *s;
};

void push(stack *temp, int x)


{
if(temp->top == temp->size-1)
cout<<"Stack Overflow!!";
else
{
temp->top++;
temp->s[temp->top] = x;
}
}

int pop(struct stack *temp)


{
int x;
if(temp->top==-1)
cout<<"Stack Underflow!!";
else
{
x = temp->s[temp->top];
temp->top--;
}
return x;
}

// Recursive function that reverses the input stack


void recursive(struct stack *st,struct stack *rev)
{
if(st->top!=-1)
{
int z = pop(st);
push(rev,z);
recursive(st,rev);
}
}
int main()
{
struct stack st;
// stack to store the reversed elements
struct stack rev;
cout<<"Enter the number of elements = ";
cin>>st.size;
st.top = -1;
st.s = new int[st.size];
// pushing elements in the intial stack
push(&st,4);
push(&st,5);
push(&st,6);

// Initial order of stack elements according to items placed.


cout << "Stack Initial: " << "\n4 \t5 \t6 \n";

rev.size = st.size;
rev.top = -1;
rev.s = new int[st.size];

// Calling a recursive function to reverse the stack.


recursive(&st,&rev);

cout << "Stack Final: \n";


for(int i=0;i<rev.size;i++)
cout<<rev.s[i]<<"\t";
return 0;
}

Output:
3. Program to print the length of the valid longest
prefix.

// C++ program to print longest valid prefix length


#include <iostream>
using namespace std;

struct stack
{
int top;
int size;
int *s;
};

int main()
{
struct stack st;
string expr;
int t;

cout << "Enter the number of trials = ";


cin >> t;
while (t > 0)
{
cout << "Enter the expression = ";
cin >> expr;
st.size = expr.length();
st.s = (int *)malloc(sizeof(int) * st.size);
st.top = -1;
int ans = 0;
for (int i = 0; i < expr.length(); i++)
{
if (expr[i] == '<')
{
st.top++;
st.s[st.top] = i;
}
else
{
if (st.top >= 0)
{
st.top--;
}
if (st.top >= 0)
{
ans = max(ans, abs(i - st.top));
}
else
{
st.top++;
st.s[st.top] = i;
}
}
}
cout << "Longest Length = "<<ans << endl;
t--;
}
return 0;
}

Output:

4. Program to tell if Drishti was able to distribute


food or not
#include <iostream>
using namespace std;

struct stack
{
int top;
int size;
int *s;
};

int main()
{
string expr;
int k = 0;
cout << "Enter the expression = ";
cin >> expr;
struct stack st;
st.size = expr.length();
st.top = -1;
st.s = (int *)malloc(sizeof(int) * st.size);

for (int i = expr.length() - 1; i >= 0; i--)


{
if (expr[i] == '0')
{
st.top++;
st.s[st.top] = expr[i];
}
else if (expr[i] == '*')
{
if (st.top >= 1)
st.top -= 2;
else
{
int x = st.top;
if (st.s[x] == '0')
st.top--;
}
}
}

if (st.top == -1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}

Output:
5. Program to print the number of days after which
no plant dies.
#include <bits/stdc++.h>
using namespace std;

// DEFINING THE STACK


class Stack
{
public:
int size;
int *arr;
int top;
Stack(int size)
{
this->size = size;
arr = new int[size];
top = -1;
}
void push(int element)
{
if (size > top + 1)
{
top++;
arr[top] = element;
}
else
{
cout << "STACK OVERFLOW" << endl;
}
}
void pop()
{
if (top >= 0)
{
top--;
}
else
{
cout << "STACK UNDERFLOW" << endl;
}
}
int Top()
{
return arr[top];
}
int Size()
{
return top + 1;
}
bool empty()
{
if (top == -1)
{
return true;
}
else
{
return false;
}
}
void print()
{
if (top >= 0)
{
for (int i = 0; i <= top; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
}
};
// DEFINING A STRUCTURE FOR THE STACK TO STORE TWO ELEMENTS
typedef struct element
{
int ele1, ele2;
} element;

int main()
{
int n;
cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++)
{
cin >> vec[i];
vec[i] = -vec[i];
}
stack<element> st;
int ans = 0;
for (int i = 0; i < n; i++)
{
if (st.empty())
{
st.push({vec[i], 0});
}
else
{
element temp = st.top();
if (vec[i] < temp.ele1)
{
int cnt = 1;
ans = max(ans, cnt);
st.push({vec[i], cnt});
}
else
{
element know = st.top();
int present = know.ele2;
while (!st.empty() && know.ele1 <= vec[i])
{
st.pop();
if (st.empty())
{
break;
}
present = max(present, know.ele2);
know = st.top();
}
if (st.empty())
{
st.push({vec[i], 0});
}
else
{
st.push({vec[i], present + 1});
ans = max(ans, present + 1);
}
}
}
}
cout << ans << endl;
return 0;
}

Output:
6. Program to determine whether each sequence of
brackets is balanced.
// C++ program to check for balanced brackets.
#include <iostream>
using namespace std;

struct stack
{
int top;
int size;
int *s;
};

int main()
{
string expr;
struct stack st;

int t;

cout << "Enter the number of trials = ";


cin >> t;
while (t > 0)
{
cout << "Enter any expression = ";
cin >> expr;

st.size = expr.length();
st.s = (int *)malloc(sizeof(int) * st.size);
st.top = -1;
for (int i = 0; i < expr.length(); i++)
{
if (st.top == -1)
{
// If the stack is empty
// just push the current bracket
st.top++;
st.s[st.top] = expr[i];
}
else if ((st.s[st.top] == '(' && expr[i] == ')') ||
(st.s[st.top] == '{' && expr[i] == '}') || (st.s[st.top] == '[' && expr[i]
== ']'))
{

// If we found any complete pair of bracket


// then pop
st.top--;
}
else
{
st.top++;
st.s[st.top] = expr[i];
}
}
if (st.top == -1)
{
// If stack is empty return true
cout << "It is Balanced"<<endl;
}
else
cout << "It is Not Balanced"<<endl;
t--;
}
return 0;
}

Output:

7. Program to find the maximum possible height of


the stacks such that all of the stacks are exactly
the same height.
#include <iostream>
using namespace std;

struct stack
{
int top;
int size;
int *s;
};
int decr(struct stack *st, int sum)
{
int x = st->s[st->top];
sum = sum - x;
st->top--;
return sum;
}

int main()
{
struct stack st1, st2, st3;
int sum1 = 0, sum2 = 0, sum3 = 0;
int maxim;

cout << "Enter the size of all the stacks = ";


cin >> st1.size >> st2.size >> st3.size;
st1.top = st2.top = st3.top = -1;

st1.s = (int *)malloc(sizeof(int) * st1.size);


st2.s = (int *)malloc(sizeof(int) * st2.size);
st3.s = (int *)malloc(sizeof(int) * st3.size);

int i = 0, x;

cout << "Enter elements of stack 1 :";


for (i = 0; i < st1.size; i++)
{
st1.top++;
cin >> x;
st1.s[st1.top] = x;
sum1 += x;
}

cout << "Enter elements of stack 2 :";


for (i = 0; i < st2.size; i++)
{
st2.top++;
cin >> x;
st2.s[st2.top] = x;
sum2 += x;
}

cout << "Enter elements of stack 3 :";


for (i = 0; i < st3.size; i++)
{
st3.top++;
cin >> x;
st3.s[st3.top] = x;
sum3 += x;
}

while (st1.top != -1)


{
maxim = (sum1 >= sum2 ? (sum1 >= sum3 ? sum1 : sum3) : (sum2 >=
sum3 ? sum2 : sum3));
if (sum1 == sum2 && sum2 == sum3)
break;
else if (maxim == sum1)
sum1 = decr(&st1, sum1);
else if (maxim == sum2)
sum2 = decr(&st2, sum2);
else if (maxim == sum3)
sum3 = decr(&st3, sum3);
}
cout<<"Maximum equal height is = "<<sum2 << endl;
return 0;
}

Output:

Thank You!!!

Name - Sanskar Singh


Roll No. – 211020445
Branch - DSAI

You might also like