0% found this document useful (0 votes)
76 views

Practice Tasks (Linked List + Stacks+ Queues)

The document contains 12 practice tasks related to data structures including linked lists, stacks, queues. The tasks involve operations like creating and traversing linked lists, searching elements, maximizing a stack value using push/pop, checking bracket balancing, converting between infix/prefix notation, sorting arrays using a stack, checking palindrome in a linked list, swapping nodes in a linked list, moving the last element of a linked list to the front, and reversing the first k elements of a queue.

Uploaded by

Hamza Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Practice Tasks (Linked List + Stacks+ Queues)

The document contains 12 practice tasks related to data structures including linked lists, stacks, queues. The tasks involve operations like creating and traversing linked lists, searching elements, maximizing a stack value using push/pop, checking bracket balancing, converting between infix/prefix notation, sorting arrays using a stack, checking palindrome in a linked list, swapping nodes in a linked list, moving the last element of a linked list to the front, and reversing the first k elements of a queue.

Uploaded by

Hamza Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICE TASKS (LINKED LIST + STACKS+ QUEUES)

1. Write a program in C to create a singly linked list of n nodes and display it in reverse order.

Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 6
Input data for node 3 : 7
Expected Output :

Data entered in the list are :


Data = 5
Data = 6
Data = 7

The list in reverse are :


Data = 7
Data = 6
Data = 5
2. Write a program in C to search an existing element in a singly linked list.
Test Data and Expected Output :

Input the number of nodes: 3

Input data for node 1: 2


Input data for node 2: 5
Input data for node 3: 8

Data entered in the list are:


Data = 2
Data = 5
Data = 8

Input the element to be searched: 5


Element found at node 2
3. You are given a stack of N integers. In one operation, you can either pop an element from the
stack or push any popped element into the stack. You need to maximize the top element of
the stack after performing exactly K operations. If the stack becomes empty after performing
K operations and there is no other way for the stack to be non-empty, print -1.
Sample Input:
64
124335
Sample Output:
4

4. A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].

Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs
to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of
matched pairs of brackets: [], {},and ().

A matching pair of brackets is not balanced if the set of brackets it encloses are not matched. For
example, {[(])} is not balanced because the contents in between { and } are not balanced. The
pair of square brackets encloses a single, unbalanced opening bracket, (, and the pair of
parentheses encloses a single, unbalanced closing square bracket, ].

By this logic, we say a sequence of brackets is balanced if the following conditions are met:

It contains no unmatched brackets.


The subset of brackets enclosed within the confines of a matched pair of brackets is also a
matched pair of brackets.
Given n strings of brackets, determine whether each sequence of
brackets is balanced. If a string is balanced, return YES. Otherwise, return NO.

5. Infix : An expression is called the Infix expression if the operator appears in between the
operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)
Prefix : An expression is called the prefix expression if the operator appears in the expression
before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Given a Prefix expression, convert it into a Infix expression.
Computers usually does the computation in either prefix or postfix (usually postfix). But for
humans, its easier to understand an Infix expression rather than a prefix. Hence conversion is
need for human understanding.
Examples:
Input : Prefix : *+AB-CD
Output : Infix : ((A+B)*(C-D))

Input : Prefix : *-A/BC-/AKL


Output : Infix : ((A-(B/C))*((A/K)-L))
Algorithm for Prefix to Infix:
• Read the Prefix expression in reverse order (from right to left)
• If the symbol is an operand, then push it onto the Stack
• If the symbol is an operator, then pop two operands from the Stack
Create a string by concatenating the two operands and the operator between them.
string = (operand1 + operator + operand2)
And push the resultant string back to Stack
• Repeat the above steps until end of Prefix expression.
6. 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.
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the below expression: a op1 b op2 c op3 d
If op1 = +, op2 = *, op3 = +
The compiler first scans the expression to evaluate the expression b * c, then again scan t he
expression to add a to it. The result is then added to d after another scan.
The repeated scanning makes it very in-efficient. It is better to convert the expression to
postfix(or prefix) form before evaluation.
The corresponding expression in postfix form is: abc*+d+. The postfix expressions can be
evaluated easily using a stack. We will cover postfix expression evaluation in a separate post .
Algorithm
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1 If the precedence of the scanned operator is greater than the precedence of the operator in
the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal to in precedence
than that of the scanned operator. After doing that Push the scanned operator to the stack. (If
you encounter parenthesis while popping then stop there and push the scanned operator in the
stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered,
and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.

7. Write a program to reverse a stack using recursion. You are not allowed to use loop constructs
like while, for..etc, and you can only use the following ADT functions on Stack S:
isEmpty(S)
push(S)
pop(S)

8. Given an array of elements, task is to sort these elements using stack.


Examples :
Input : 8 5 7 1 9 12 10
Output : 1 5 7 8 9 10 12
Explanation :
Output is sorted element set

Input : 7 4 10 20 2 5 9 1
Output : 1 2 4 5 7 9 10 20
9. Given a singly linked list of characters, write a function that returns true if the given list is a
palindrome, else false.

10. Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be
swapped by changing links. Swapping data of nodes may be expensive in many situations
when data contains many fields.
It may be assumed that all keys in the linked list are distinct.
Examples:
Input: 10->15->12->13->20->14, x = 12, y = 20
Output: 10->15->20->13->12->14

Input: 10->15->12->13->20->14, x = 10, y = 20


Output: 20->15->12->13->10->14

Input: 10->15->12->13->20->14, x = 12, y = 13


Output: 10->15->13->12->20->14
11. Write a function that moves the last element to the front in a given Singly Linked List. For
example, if the given Linked List is 1->2->3->4->5, then the function should change the list to
5->1->2->3->4.
12. Given an integer k and a queue of integers, we need to reverse the order of the first k elements
of the queue, leaving the other elements in the same relative order.
Only following standard operations are allowed on queue.
enqueue(x) : Add an item x to rear of queue
dequeue() : Remove an item from front of queue
size() : Returns number of elements in queue.
front() : Finds front item.
Examples:
Input : Q = [10, 20, 30, 40, 50, 60,
70, 80, 90, 100]
k=5
Output : Q = [50, 40, 30, 20, 10, 60,
70, 80, 90, 100]

Input : Q = [10, 20, 30, 40, 50, 60,


70, 80, 90, 100]
k=4
Output : Q = [40, 30, 20, 10, 50, 60,
70, 80, 90, 100]

You might also like