Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Aswer in python with explanations/comments. Answer question 13 and 15 Implement

ID: 3805555 • Letter: A

Question

Aswer in python with explanations/comments. Answer question 13 and 15

Implement a function, with calling syntax max(L), that returns the maximum

element from a PositionalList instance L containing comparable

elements.

Redo the previously problem with max as a method of the PositionalList

class, so that calling syntax L.max( ) is supported.

13. (This is the question to answer) Update the PositionalList class to support an additional method find(e),

which returns the position of the (first occurrence of ) element e in the list

(or None if not found)

Repeat the previous process using recursion. Your method should not

contain any loops. How much space does your method use in addition to

the space used for L?

15.(Question to be answer) Provide support for a reversed method of the PositionalList class that is similar to the given iter , but that iterates the elements in reversed order.

The positional list is: 15, 22, 25, 29, 36, 23, 53, 11, 42

Explanation / Answer

#include<iostream>
#include<stdlib.h>

using namespace std;

/* A simple stack class with basic stack funtionalities */
class Stack
{
private:
static const int max = 100;
int arr[max];
int top;
public:
Stack() { top = -1; }
bool isEmpty();
bool isFull();
int pop();
void push(int x);
};

/* Stack's member method to check if the stack is iempty */
bool Stack::isEmpty()
{
if(top == -1)
return true;
return false;
}

/* Stack's member method to check if the stack is full */
bool Stack::isFull()
{
if(top == max - 1)
return true;
return false;
}

/* Stack's member method to remove an element from it */
int Stack::pop()
{
if(isEmpty())
{
cout<<"Stack Underflow";
abort();
}
int x = arr[top];
top--;
return x;
}

/* Stack's member method to insert an element to it */
void Stack::push(int x)
{
if(isFull())
{
cout<<"Stack Overflow";
abort();
}
top++;
arr[top] = x;
}

/* A class that supports all the stack operations and one additional
operation getMin() that returns the minimum element from stack at
any time. This class inherits from the stack class and uses an
auxiliarry stack that holds minimum elements */
class SpecialStack: public Stack
{
Stack min;
public:
int pop();
void push(int x);
int getMin();
};

/* SpecialStack's member method to insert an element to it. This method
makes sure that the min stack is also updated with appropriate minimum
values */
void SpecialStack::push(int x)
{
if(isEmpty()==true)
{
Stack::push(x);
min.push(x);
}
else
{
Stack::push(x);
int y = min.pop();
min.push(y);
if( x < y )
min.push(x);
else
min.push(y);
}
}

/* SpecialStack's member method to remove an element from it. This method
removes top element from min stack also. */
int SpecialStack::pop()
{
int x = Stack::pop();
min.pop();
return x;
}

/* SpecialStack's member method to get minimum element from it. */
int SpecialStack::getMin()
{
int x = min.pop();
min.push(x);
return x;
}

/* Driver program to test SpecialStack methods */
int main()
{
SpecialStack s;
s.push(10);
s.push(20);
s.push(30);
cout<<s.getMin()<<endl;
s.push(5);
cout<<s.getMin();
return 0;
}

Space Optimized Version
The above approach can be optimized. We can limit the number of elements in auxiliary stack. We can push only when the incoming element of main stack is smaller than or equal to top of auxiliary stack. Similarly during pop, if the pop off element equal to top of auxiliary stack, remove the top element of auxiliary stack. Following is modified implementation of push() and pop().

/* SpecialStack's member method to insert an element to it. This method

   makes sure that the min stack is also updated with appropriate minimum

   values */

void SpecialStack::push(int x)

{

    if(isEmpty()==true)

    {

        Stack::push(x);

        min.push(x);

    }

    else

    {

        Stack::push(x);

        int y = min.pop();

        min.push(y);

  

        /* push only when the incoming element of main stack is smaller

        than or equal to top of auxiliary stack */

        if( x <= y )

          min.push(x);

    }

}

/* SpecialStack's member method to remove an element from it. This method

   removes top element from min stack also. */

int SpecialStack::pop()

{

    int x = Stack::pop();

    int y = min.pop();

    /* Push the popped element y back only if it is not equal to x */

    if ( y != x )

      min.push(y);

    return x;

}

/* SpecialStack's member method to insert an element to it. This method

   makes sure that the min stack is also updated with appropriate minimum

   values */

void SpecialStack::push(int x)

{

    if(isEmpty()==true)

    {

        Stack::push(x);

        min.push(x);

    }

    else

    {

        Stack::push(x);

        int y = min.pop();

        min.push(y);

  

        /* push only when the incoming element of main stack is smaller

        than or equal to top of auxiliary stack */

        if( x <= y )

          min.push(x);

    }

}

/* SpecialStack's member method to remove an element from it. This method

   removes top element from min stack also. */

int SpecialStack::pop()

{

    int x = Stack::pop();

    int y = min.pop();

    /* Push the popped element y back only if it is not equal to x */

    if ( y != x )

      min.push(y);

    return x;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote