Lab3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Name : Shivaibhav Dewangan

Roll No : 211020448 DSA Lab 3


Branch : DSAI

Problem 1 :

#include <iostream>
using namespace std;

struct Queue
{
    int front;
    int rear;
    int size;
    int *arr;
};

void push(Queue *q, int data)


{
    // For the push operation, we just simply push the elements
    // in the single Queue from the rear end
    if (q->front == -1)
    {
        q->front = 0;
    }

    // Full Condition of a Single Queue


    if (q->rear == q->size - 1)
    {
        cout << "Stack is Full";
    }

    // Otherwise simply insert the element in the Queue


    q->rear++;
    q->arr[q->rear] = data;
}

void pop(Queue *q)


{  
    // Check for empty stack using Queue condition
    if (q->front > q->rear)
    {
        cout << "Stack is Empty";
        return;
    }
   
    int n = q->rear;

    // Copying the (n-1) elements from the rear end


    for (int i = q->front; i < n; i++)
    {
        q->rear++;
        q->front++;
        q->arr[q->rear] = q->arr[i];
    }

    // Deleting the element from the front end


    q->front++;
}

void display(Queue *q)


{
    cout << "Stack elements are : " << '\n';
    for (int i = q->front; i <= q->rear; i++)
    {
        cout << q->arr[i] << '\n';
    }
}

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;

// Reverse a stack using recursion

struct Stack
{
    int top;
    int size;
    int *arr;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void reverseStack(Stack *st, int index)


{
    // Base Case : When index reaches mid-point, we return the
recursive call

    if (index == st->top/2)  
        return;
    else
    {
        // swap the first and the last index
        swap(st->arr[index], st->arr[st->top - index]);

        // call the recursion again with the next index


        reverseStack(st, index + 1);
    }
}

void display(Stack *st)


{
    for (int i = st->top; i >= 0; i--)
    {
        cout << st->arr[i] << " ";
    }
}

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);

    cout << '\n';


    cout << "Reversed Stack : ";
    display(st);

    return 0;
}

Output :

Problem 3 :

#include <iostream>
using namespace std;

struct Stack
{
    int top;
    int size;
    char *arr;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void pop(Stack *st)


{
    if (st->top == -1)
    {
        cout << "Stack is Empty";
    }
    else
    {
        st->top--;
    }
}

bool isEmpty(Stack *st)


{
    if (st->top == -1)
        return true;

    return false;
}

void display(Stack *st)


{
    for (int i = st->top; i >= 0; i--)
    {
        cout << st->arr[i] << " ";
    }
}

int longestPrefix(Stack *st, string s)


{
    int cnt = 0, ans = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == '<')
        {
            push(st, '<');
        }
        else
        {
            if (st->arr[st->top] == '<')
                pop(st);
            else
                break;
        }

        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;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void pop(Stack *st)


{
    if (st->top == -1)
    {
        cout << "Stack is Empty";
    }
    else
    {
        st->top--;
    }
}

bool isEmpty(Stack *st)


{
    if (st->top == -1)
        return true;
    else
        return false;  
}
bool checkFood(Stack *st, string s)
{
    for (int i = 0; i < s.length(); i++)
    {
        // if we encounter a * push 1 twice to stack
        // Here 1 can be seen as two consecutive food packages
        if (s[i] == '*')
        {
            push(st, 1);
            push(st, 1);
        }
        else
        {
            // check if stack isEmpty, food distribution not possible
            if (isEmpty(st))
            {
                return false;
            }
            else
            {
                // if not empty, we can get a food package, hence pop
                pop(st);
            }
        }
    }
   
    // if all conditions satisfied, answer is possible hence true
    return true;
}

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;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void pop(Stack *st)


{
    if (st->top == -1)
    {
        cout << "Stack is Empty";
    }
    else
    {
        st->top--;
    }
}

bool isEmpty(Stack *st)


{
    if (st->top == -1)
        return true;
    else
        return false;
}

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;

    // iterating over the array


    for (int i = 1; i <= n; i++)
    {
        cin >> num;
        c = 1;
        // if stack not empty and top first greater equal to prev, we
get max ans
        while(!st.empty() && st.top().first >= num)
        {
            c = max(c, st.top().second + 1);
            st.pop();
        }
       
        // conditions for plant to not be dead
        if(num <= top)
        {
            top = num;
            c = 0;
        }
        // else we have an answer
        else
        {
            ans = max(ans,c);
        }
        // push the pair into stack
        st.push({num,c});
    }

    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;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void pop(Stack *st)


{
    if (st->top == -1)
    {
        cout << "Stack is Empty";
    }
    else
    {
        st->top--;
    }
}

bool isEmpty(Stack *st)


{
    if (st->top == -1)
        return true;
    else
        return false;
}

bool checkBracket(Stack *st, string s)


{
    for (int i = 0; i < s.length(); i++)
    {
        // if we encounter a opening bracket, simply push into stack
        if (s[i] == '(' || s[i] == '[' || s[i] == '{')
        {
            push(st, s[i]);
        }
        else
        {
            // now we have encountered a closing bracket
            // check if stack empty then not possible
            if (isEmpty(st))
                return false;

            // if stack top doesnt matches corresponding opening


bracket, return false
            if (s[i] == ')' && st->arr[st->top] != '(')
                return false;
           
            if (s[i] == ']' && st->arr[st->top] != '[')
                return false;
           
            if (s[i] == '}' && st->arr[st->top] != '{')
                return false;
           
            // if matches, pop the top of stack
            pop(st);
        }
    }

    // if the final stack is empty, only then we have an answer


    if (isEmpty(st))
        return true;

    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;
};

void push(Stack *st, int data)


{
    if (st->top == st->size - 1)
    {
        cout << "Stack is Full";
    }
    else
    {
        st->top++;
        st->arr[st->top] = data;
    }
}

void pop(Stack *st)


{
    if (st->top == -1)
    {
        cout << "Stack is Empty";
    }
    else
    {
        st->top--;
    }
}

bool isEmpty(Stack *st)


{
    if (st->top == -1)
        return true;

    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);

    // Precalculate the sum of all the given cylindrical stacks of


rectangles
    int sum1 = 8, sum2 = 9, sum3 = 7;
    int ans = 0;

    // loop until one of the stacks become empty


    while (!isEmpty(st1) and !isEmpty(st2) and !isEmpty(st3))
    {
        // get the maximum sum of the three stacks
        // we firstly remove the top from the stack which has the
maximum sum
        int maxSum = max({sum1, sum2, sum3});
       
        //  check all the conditions for maxSum to be equal to one of
the stacks
        if (sum1 == maxSum)
        {
            int top = st1->arr[st1->top];
            pop(st1);
            sum1 -= top;
        }
        else if (sum2 == maxSum)
        {
            int top = st2->arr[st2->top];
            pop(st2);
            sum2 -= top;
        }
        else if (sum3 == maxSum)
        {
            int top = st3->arr[st3->top];
            pop(st3);
            sum3 -= top;
        }

        // if found, we get the answer else answer is always zero


        if (sum1 == sum2 and sum2 == sum3 and sum3 == sum1)
        {
            ans = sum1;
            break;
        }
    }
   
    cout << "Maximum Possible Height : " << ans << '\n';
}

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy