0% found this document useful (0 votes)
7 views3 pages

Stack and Queue

The document contains five programming questions related to stack operations using a single linked list. The tasks involve pushing integers, sorting stacks, merging two stacks, checking for common elements, and checking for palindromes. Each question includes specific input and output examples to illustrate the requirements.

Uploaded by

23520999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Stack and Queue

The document contains five programming questions related to stack operations using a single linked list. The tasks involve pushing integers, sorting stacks, merging two stacks, checking for common elements, and checking for palindromes. Each question includes specific input and output examples to illustrate the requirements.

Uploaded by

23520999
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question 1.

Implement a Stack and its operators using a Single Linked List. Each element is an integer. Do
the following requirements:
- Push a sequence of integers into the stack. Insertion stops when encountering the "0" element.
- Use stack operators to sort the stack in the order such that the even numbers are below the odd
numbers in the result stack and keep the relative positions among the odd and even numbers unchanged.
Input: - Sequence of integers separated by a new line.
Output:
• Print out elements of the stack separated by a space.
• Print the stack after sorting.
Examples

Input Output
6 1422356
5 1354226
3
2
2
4
1
0

Question 2.
Given a string consisting of opening and closing square brackets, find the length of the
longest valid square brackets substring.
Examples

Input Output
[[]][][[[ 6

Question 3:
Implement a Stack and its operators using a Single Linked List. Each element is an integer.
Do the following requirements:
- Push a sequence of descending integers into the stack. The insertion is stopped when encountering the
"0" element.
- Given two stacks whose elements are in ascending order, merge these two stacks such
that the result stack's elements are in ascending order.
Note: use stack operators only.
Input:
- Elements of the first stack separated by a new line.
- Elements of the second stack separated by a new line.
Output:
• Print out elements of two stacks, separated by a space.
• Print the result stack after merging two stacks, separated by a space.
Examples

Input Output
15 5 8 9 11 12 15
12 3 5 8 10 13
11 3 5 5 8 8 9 10 11 12 13 15
9
8
5
0
13
10
8
5
3
0

Question 4:
Given two stacks whose elements are distinct and ordered in ascending order.
Given a positive integer n, check whether two stacks have a common block of n elements or not
using stack operators only.
Examples

Input Output
- Stack 1: 3 5 6 8 9 11 12 yes
- Stack 2: 1 2 4 6 8 9 10 15
- n: 3

Question 5:
Given a positive number n, write a program to push all digits of this number to a stack in the right-to-left
order.
Check if the string of digits inside the stack is palindrome without using the original number

Input Output
123456 0
456654 1

You might also like