Lab3
Lab3
Lab3
Problem 1 :
#include <iostream>
using namespace std;
struct Queue
{
int front;
int rear;
int size;
int *arr;
};
int main()
{
Queue *q = new Queue();
q->front = -1, q->rear = -1;
q->size = 100;
q->arr = new int[q->size];
push(q, 10);
push(q, 20);
push(q, 30);
push(q, 40);
push(q, 50);
pop(q);
pop(q);
display(q);
return 0;
}
Output :
Problem 2 :
#include <iostream>
using namespace std;
struct Stack
{
int top;
int size;
int *arr;
};
if (index == st->top/2)
return;
else
{
// swap the first and the last index
swap(st->arr[index], st->arr[st->top - index]);
int main()
{
// Initializing the stack
Stack *st = new Stack();
st->top = -1;
st->size = 100;
st->arr = new int[st->size];
push(st, 10);
push(st, 20);
push(st, 30);
push(st, 40);
push(st, 50);
cout << "Original Stack : " ;
display(st);
reverseStack(st, 0);
return 0;
}
Output :
Problem 3 :
#include <iostream>
using namespace std;
struct Stack
{
int top;
int size;
char *arr;
};
return false;
}
if (isEmpty(st))
ans = i + 1;
}
return ans;
}
int main()
{
// Initializing the stack
Stack *st = new Stack();
st->top = -1;
st->size = 100;
st->arr = new char[st->size];
string s = "<<>><>";
cout << "Given String : " << s << '\n';
cout << "Longest Prefix : " << longestPrefix(st, s);
return 0;
}
Output :
Problem 4 :
#include <iostream>
using namespace std;
struct Stack
{
int top;
int size;
int *arr;
};
int main()
{
// Initializing the stack
Stack *st = new Stack();
st->top = -1;
st->size = 100;
st->arr = new int[st->size];
string s;
cout << "Enter the string : ";
cin >> s;
cout << "Input String : " << s << '\n';
if (checkFood(st, s) == true)
cout << "YES";
else
cout << "NO";
return 0;
}
Output :
Problem 5 :
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int top;
int size;
int *arr;
};
int checkPesticide()
{
// taking all the required inputs
int i;
int n;
cin >> n;
// initializing the values
int ans = 0;
int c = 0;
int num, top = INT_MAX;
// creating a stack of pair of 2 integers denoted as first and
second values
stack <pair<int, int>> st;
return ans;
}
int main()
{
int ans = checkPesticide();
cout << "Days after which no plant dies : " << ans << '\n';
return 0;
}
Output :
Problem 6 :
#include <iostream>
using namespace std;
struct Stack
{
int top;
int size;
char *arr;
};
return false;
}
int main()
{
// Initializing the stack
Stack *st = new Stack();
st->top = -1;
st->size = 100;
st->arr = new char[st->size];
string s;
cout << "Enter the string : ";
cin >> s;
if (checkBracket(st, s) == true)
cout << "YES";
else
cout << "NO";
return 0;
}
Output :
Problem 7 :
#include <bits/stdc++.h>
using namespace std;
struct Stack
{
int top = -1;
int size = 100;
int *arr;
};
return false;
}
int main()
{
// Initializing the stack
// we create three stacks for every rectangular cylinder
Stack *st1 = new Stack();
Stack *st2 = new Stack();
Stack *st3 = new Stack();
st1->arr = new int[st1->size];
st2->arr = new int[st2->size];
st3->arr = new int[st3->size];
// pushing the given values into the stack to form the figure as
shown in the problem
push(st1, 1), push(st1, 1), push(st1, 1), push(st1, 2), push(st1,
3);
push(st2, 2), push(st2, 3), push(st2, 4);
push(st3, 1), push(st3, 4), push(st3, 1), push(st3, 1);
Output :