Lab 4 Stack Oop
Lab 4 Stack Oop
Lab 4 Stack Oop
Instruction: For each and every assignment, create a project. Define the class, data members and
member functions in .h file. Implementation detail in separate file and driver code in separate file. In
the driver code create an object and perform operations to test the utility of the project.
class stack
int *arr;
int top;
int capacity;
public:
~stack(); // destructor
};
2. Create a class called stack similar member functions as above. Implement stack using linked
list.
3. Modify question 1, design a stack that returns the minimum element in constant time. Use
two stacks, main stack to store elements and auxiliary stack to store the required elements
needed to determine the minimum number in constant time. Other operations on the stack
remain same (push, pop, peek). If the element is removed from the main stack, similar
element should also be removed from the auxiliary stack if present.
4. Optimize the above code to return the minimum element in constant time without using
auxiliary stack.
5. Implement two stacks in a single array. Both the stacks can grow towards each other with no
fixed capacity (array is not divided into two halves, one for each stack).
class Stack
int *arr;
int capacity;
int top1, top2;
public:
// Constructor
Stack(int n)
capacity = n;
top1 = -1;
top2 = n;
void push1(int key); // Function to insert a given element into the first stack
void push2(int key); // Function to insert a given element into the second stack
};
6. Update the assignment 1 with additional member function called sortstack. This function
should be a recursive method to sort a stack in ascending order.
7. Write a program to convert infix expression into postfix and prefix expression using the class
Stack. Stack is of type char.
int main()
{
stack s1;
s1.getInfixExpr(); // function which store expression, "(P+(Q*R)/(S-t))"