Labsheet2 3
Labsheet2 3
Stack
1. Design a Stack ADT, given the skeleton as follows:
class STACK {
private:
int num[SIZE];
int top;
public:
STACK(); //defualt constructor
int push(int);
int pop();
int isEmpty();
int isFull();
void displayItems();
};
Save it as MyStack.java (can be .cpp also). You may test the code by writing a separate
driver program.
2. Write a driver program “Print.java” to print the elements of the stack from bottom to
top, such that the elements are still present in the stack without their order being
changed in the stack.
3. Write a program “MySort.java” to sort the integers of a stack in ascending order
using another temporary stack.
4. Assume that you are asked to reverse the numbers stored in an array. Comment
whether you can use a stack and an array to do so. For the implementation, import the
MyStack that you defined in Qn 1Write the driver program “Reverse.java” that
utilizes MyStack and do the operation.
5. Include Mystack.java. Now input an infix expression in the form of string str from the
user. Convert this infix expression to postfix expression.
Infix expression: The expression of the form a op b. When an operator is in-between
every pair of operands.
Postfix expression: The expression of the form a b op. When an operator
is followed for every pair of operands. Note: The order of precedence is: ^
greater than * equals to / greater than + equals to -. The associativity of
various operators must be taken into consideration. Save the file as
InfixConvertor. Java.
6. Include the files MyStack.java and InfixConvertor.java to implement the task of
evaluating the infix expression. Your input would be the infix expression.
You need to display the postfix expression and show the result of the
postfix expression.
7. Include MyStack.cpp and write a program to check if the string of
parenthesis (may include ([{ ) are balanced or not. Save the file as
parenth.java.
8. Now include MyStack.java and parenth.java, to answer the following question:
(i) Given a string containing just the characters '(' and ')', return the length of
the longest valid (well-formed) parentheses substring.
Example 1:
Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
Example 2:
Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
Linked list
class SinglyLinkedList {
Node* head;
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtBegin(5);
list.traverse(); // Output: 5 10 20 30
list.insertAtPosition(15, 2);
list.traverse(); // Output: 5 10 15 20 30
list.deleteAtBegin();
list.traverse(); // Output: 10 15 20 30
2. Use the SinglyLinkedList ADT and store some values. The task is to find the middle of
the linked list. If the number of nodes is even, then there would be two middle nodes, so
return the second middle node.
(i) How will you accomplish the task if the length of the linked list is calculated as part of
the implementation.
(ii) How will you accomplish the task if you are using two additional pointers and modify
the traversals.
Write the justifications of both questions neatly in the notebook and attach it as an image
during submission. Implement considering the two conditions.
3. Assume the same SinglyLinkedList ADT consisting of N nodes. Write a driver program
to remove duplicates (nodes with duplicate values) from the given list (if exists).
Note: Try not to use extra space. The nodes are arranged in a sorted way.
4. Given two polynomial numbers represented by a linked list. Write a function that add
these lists means add the coefficients who have same variable powers.
5. Complete and submit the Leet code Qn 2 (Add two Numbers)
6. Complete and submit the Leet code Qn 21( Merge two sorted lists)