Uploaded by ruchirsharma074

CS110 Assignment: Linked Lists, Stacks, and Queues

advertisement
Assignment for CS110
Linked Lists
1. Add Two Numbers Represented by Linked Lists
Problem Statement​
You are given two non-empty linked lists representing two non-negative integers. The digits are
stored in reverse order, and each of their nodes contains a single digit. Your task is to create
these two linked lists based on the input, then compute their sum and store the result in a new
linked list. Finally, print the resulting linked list.
Instructions
●​ First, create the first linked list from the given input digits​
●​ Then, create the second linked list from its input digits​
●​ Add the two numbers represented by the linked lists​
●​ Store the sum in a new linked list (in reverse order)​
●​ Print the resulting linked list​
Input​
Two sequences of digits representing the numbers in reverse order.​
Each digit is stored in a separate node in a singly linked list.
Output​
Print the resulting linked list representing the sum of the two numbers, also in reverse order.
Example 1​
Input​
l1 = [2, 4, 3]​
l2 = [5, 6, 4]
Output​
708
Explanation​
342 + 465 = 807​
The result is stored in reverse as 7 0 8
Example 2​
Input​
l1 = [0]​
l2 = [0]
Output​
0
Example 3​
Input​
l1 = [9, 9, 9, 9, 9, 9, 9]​
l2 = [9, 9, 9, 9]
Output​
89990001
Constraints
●​ 1 <= length of each linked list <= 100​
●​ Each digit is between 0 and 9​
●​ The input does not contain leading zeros, except for the number 0 itself
2.Rotate a Linked List to the Right by k Places
Problem Statement​
You are given the head of a singly linked list. Rotate the list to the right by k places. That
means each node moves k positions forward, and the nodes at the end wrap around to the
beginning.
Instructions
●​ First, create a linked list from the input values​
●​ Then rotate the list to the right by k positions​
●​ Print the final rotated list​
Input
●​ A singly linked list​
●​ An integer k, the number of rotations​
Output​
Print the resulting linked list after rotating it k places to the right
Example 1​
Input​
head = [1, 2, 3, 4, 5], k = 2
Output​
45123
Example 2​
Input​
head = [0, 1, 2], k = 4
Output​
201
Explanation​
In the second example, k is greater than the list length, so you rotate the list by k % length
positions
Constraints
●​ 0 <= number of nodes <= 500​
●​ -100 <= value of each node <= 100​
●​ 0 <= k <= 2 billion
Stack
1.Stack Operations – Build Target Array
You are given an integer array target and an integer n.
You have an empty stack with the two following operations:
●​ "Push": pushes an integer to the top of the stack.​
●​ "Pop": removes the integer on the top of the stack.​
You also have a stream of the integers in the range [1, n].
Use the two stack operations to make the numbers in the stack (from the bottom to the top)
equal to target. You should follow the following rules:
●​ If the stream of the integers is not empty, pick the next integer from the stream and push
it to the top of the stack.​
●​ If the stack is not empty, pop the integer at the top of the stack.​
●​ If, at any moment, the elements in the stack (from the bottom to the top) are equal to
target, do not read new integers from the stream and do not do more operations on
the stack.​
Print the stack operations needed to build target following the mentioned rules. If there are
multiple valid answers, print any of them.
Example 1:
Input: target = [1,3], n = 3​
Output: ["Push","Push","Pop","Push"]​
Explanation: Initially the stack s is empty. The last element is the top of the stack.​
Read 1 from the stream and push it to the stack. s = [1].​
Read 2 from the stream and push it to the stack. s = [1,2].​
Pop the integer on the top of the stack. s = [1].​
Read 3 from the stream and push it to the stack. s = [1,3].
Example 2:
Input: target = [1,2,3], n = 3​
Output: ["Push","Push","Push"]​
Explanation: Initially the stack s is empty.​
Read 1 from the stream and push it to the stack. s = [1].​
Read 2 from the stream and push it to the stack. s = [1,2].​
Read 3 from the stream and push it to the stack. s = [1,2,3].
Example 3:
Input: target = [1,2], n = 4​
Output: ["Push","Push"]​
Explanation: Initially the stack s is empty.​
Read 1 from the stream and push it to the stack. s = [1].​
Read 2 from the stream and push it to the stack. s = [1,2].​
Since the stack is equal to target, we stop. Reading 3 from the stream is not allowed.
2.Prefix to Postfix Conversion
Problem Statement​
You are given a string that represents the prefix form of a valid mathematical expression. Your
task is to convert it to its equivalent postfix form and print the output.
You may assume:
●​ The expression contains only uppercase alphabets as operands (A to Z)​
●​ Valid operators include +, -, *, and /​
●​ The prefix expression is guaranteed to be valid​
Input​
A single string representing a prefix expression
Output​
Print the equivalent postfix expression
Example 1​
Input​
*-A/BC-/AKL
Output​
ABC/-AK/L-*
Explanation​
The prefix expression -A/BC-/AKL becomes ABC/-AK/L- in postfix after placing operands and
operators according to postfix rules
Constraints
●​ Expression length is at most 100​
●​ Only single-character operands​
●​ The expression is syntactically correct and unambiguous
Queue
1. Implement First Unique Character in a Stream
Problem​
You're given a stream of lowercase letters. At each point, print the first character that hasn’t
repeated yet.​
Use a simple queue and frequency array.
Input​
Stream: a a b c c d
Output​
a -1 b b b d
2. Reverse First K Elements of Queue
Problem​
Reverse the first k elements of the queue, keep the rest in order.​
Use a stack for the first k and queue for the rest.
Input​
Queue = 1 2 3 4 5, k = 3
Output​
32145
Implementation of stack and queue using array
and linked list
1. Implement a Stack using Array
Problem​
Implement a stack with the following operations using an array:
●​ push(int x)​
●​ pop()​
●​ peek()​
●​ isEmpty()​
●​ isFull()​
Input Example​
Push 10, 20, 30 → Pop → Peek
Output​
Pushed: 10​
Pushed: 20​
Pushed: 30​
Popped: 30​
Top element: 20
2. Implement a Stack using Linked List
Problem​
Use a singly linked list where each node contains an integer. Implement:
●​ push(int x)​
●​ pop()​
●​ peek()​
●​ isEmpty()​
Input Example​
Push 5, 8 → Pop → Peek
Output​
Pushed: 5​
Pushed: 8​
Popped: 8​
Top element: 5
3. Implement Two Stacks in One Array
Problem​
Use a single array to implement two stacks. Ensure no memory overlap.​
Implement:
●​ push1, push2​
●​ pop1, pop2​
Input Example​
push1(10), push2(20), push1(30), pop1()
Output​
Stack1 push: 10​
Stack2 push: 20​
Stack1 push: 30​
Stack1 pop: 30
4. Implement a Queue using Array
Problem​
Use a circular array to implement a queue with:
●​ enqueue(int x)​
●​ dequeue()​
●​ peek()​
●​ isEmpty(), isFull()​
Input Example​
Enqueue 1, 2, 3 → Dequeue → Peek
Output​
Enqueued: 1​
Enqueued: 2​
Enqueued: 3​
Dequeued: 1​
Front: 2
5. Implement a Queue using Linked List
Problem​
Use a linked list with front and rear pointers. Implement:
●​ enqueue(int x)​
●​ dequeue()​
●​ peek()​
●​ isEmpty()​
Input Example​
Enqueue 11, 22 → Dequeue → Peek
Output​
Enqueued: 11​
Enqueued: 22​
Dequeued: 11​
Front: 22
6. Reverse a Queue using Stack
Problem​
Given a queue, reverse its elements using a stack.
Input​
Queue: 10 20 30 40
Output​
Reversed: 40 30 20 10
Download