Week6_Stack_list
Week6_Stack_list
PreLab
1.what are the stack operations? And how can you determine stack is empty when
implemented with a linked list.
2.How does the linked list implementation enhance the dynamic nature of a stack, and what
advantages does it offer over array-based implementations?
· 4. What is the role of the top pointer in a linked list implementation of a stack?
5.Can you compare the time and space complexity of basic stack operations (push, pop) in a
linked list-based implementation versus an array-based implementation?
In-Lab
1.In the bustling kingdom of CodeLand, a young programmer named Leo was summoned by
the King. The King needed a magical container to organize and manage his collection of
royal decrees. This container, called a "stack," had to work with a special rule: the last decree
added would be the first one removed.
The King also wanted Leo to create two magical spells for this stack:
1. Push: A spell to add a new decree to the top of the stack.
2. Display: A spell to reveal all the decrees in the stack, starting from the top.
Leo, being skilled in the art of linked lists, began crafting his solution:
For the push spell, he created a function that inserted a new decree at the top of the
stack.
For the display spell, he designed a function that traversed the stack and printed all
the decrees from top to bottom, ensuring the King could review them as needed.
Leo tested his spells and was delighted to see them working flawlessly, much to the
King's satisfaction.
Sample Demonstration
Input:
1. Push "Decree A"
2. Push "Decree B"
3. Push "Decree C"
4. Display the stack
Output:
Stack contents:
Decree C
·
Decree B
Decree A
2. A knight records their completed quests in a stack as they are accomplished. You
need to implement a program that allows the knight to:
1. Add (Push) a new quest to the log.
2. Remove (Pop) the most recently completed quest from the log.
3. Display all completed quests in reverse order of completion.
Sample Input
1. Push "Defeat the Dragon"
2. Push "Rescue the Princess"
3. Push "Find the Lost Sword"
4. Display
5. Pop
6. Display
Sample Output
Quest log:
Find the Lost Sword
Rescue the Princess
Defeat the Dragon
Removed: Find the Lost Sword
Quest log:
Rescue the Princess
Defeat the Dragon
3. In a quiet town, there lived an engineer named Clara who loved solving puzzles. One day,
the town's elders came to her with a unique problem. They had a stack of ancient scrolls
arranged in a special order, stored in a magical linked list. Each scroll contained valuable
wisdom, but they needed Clara to find the middle scroll without disturbing the order of the
stack.
The task was tricky because removing or rearranging the scrolls would break the stack's
magical properties. Clara needed to create a program that could traverse the stack, carefully
identify the middle scroll, and leave everything just as it was.
Post Lab
1 Problem Statement:
A palindrome is a word, phrase, number, or sequence of characters that reads the same
backward as forward, ignoring spaces, punctuation, and capitalization. Write a program to
check if a given string is a palindrome using a stack implemented with a linked list.
Requirements:
Input:
Output:
2. Maximum Element
You have an empty sequence, and you will be given queries. Each query is one of these three
types:
1. Push the element x into the stack.
2. Delete the element present at the top of the stack.
·
3. Print the maximum element in the stack.
Link:https://www.hackerrank.com/challenges/maximum-element/problem?isFullScreen=true
Skill
1. Stack using single linked list
Design and implement a stack data structure using a singly linked list to efficiently manage a
collection of elements.
The goal is to create a flexible and dynamic stack that supports standard stack operations
while leveraging the
advantages of a linked list structure.
Sample Input
1
6
push 1
push 2
push 3 pop pop
top
Sample Output
3
2
1
Link:www.hackerrank.com/ds-week6-postlab-programs
3.A movie recommendation system uses a stack to keep track of users' preferences.
Each time a user rates a movie, the information is pushed onto the stack. Users can also undo
their last rating, which pops the last rating from the stack.
Design a C program that simulates this movie recommendation stack system. Your program
should:
* Allow users to rate movies by pushing the movie name and rating onto the stack.
* Allow users to undo their last rating by popping the last rating from the stack.
* Display the current stack of movie ratings after each operation.
Sample Input:
Movie Rating System: 1. Rate a movie 2. Undo last rating 3. Display movie ratings 4. Exit
Enter your choice: 3
Sample output:
No movie ratings available.
4.You are tasked with implementing a simple browser history feature using a stack. The goal
is to simulate the behavior of the "Back" button in a web browser. Each time a user visits a
new webpage, it is added to the stack. When the user clicks the "Back" button, the program
should pop the last visited webpage from the stack and display it. If the stack is empty, the
program should indicate that there is no history.
Write a C program to implement this browser history feature using a stack. The program
should support the following operations:
1. Visit a Webpage: Add a new webpage to the history stack.
2. Go Back: Pop the last visited webpage from the stack and display it.
3. Display Current Page: Display the currently visited webpage without removing it from the
stack.
Implement the stack using a linked list and ensure that the program handles cases where the
user tries to go back when there is no history.
Input and Output Format:
* The user can input commands such as "Visit", "Back", and "Display" along with the
webpage URL.
* The program should display the current webpage or a message indicating no history.
Output Format:
·
* Resultant list with space separated elements after performing merge operation.
Constraints:
* 0 ≤ N1, N2 ≤ 100
* -103 ≤ Data in Node ≤ 103
* Use concept of linked list to solve this problem.
Sample Input:
3 2 (First line consists of N1 and N2 ( Separated by space))
11 22 33 (Second line contains N1 data values in list A ( Space separated))
22 33 (Third line contains N2 data values in list B ( Space separated))
Sample Output 1:
11 22 22 33 33 (Resultant list)