Question

1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed:

Stack myStack = new Stack();

myStack.push(20);

myStack.push(40);

myStack.pop();

myStack.push(60);

myStack.push(80);

2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well.

3)Given a Queue implemented with a Double Ended Linked List (this is a linked list with a head and tail node), and an O(1) implementation of insert and remove, show the Double Ended Linked List after the following commands are executed:

Queue myQueue = new Queue();

myQueue.insert(20);

myQueue.insert(40);

myQueue.remove();

myQueue.insert(60);

myQueue.insert(80);

4)If the same commands were used but the Queue was implemented with an Array with a maximum size of 3, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Stack using
Linked List:
myStack.push(20); H->20
myStack.push(40); H->40 -> 20
myStack.pop();     H->20
myStack.push(60); H->60->20
myStack.push(80); H->80->60->20

Array:
myStack.push(20); |20| | | | | | | | | |
myStack.push(40); |20|40| | | | | | | | |
myStack.pop();     |20| | | | | | | | | |
myStack.push(60); |20|60| | | | | | | | |
myStack.push(80); |20|60|80| | | | | | | |


Queue using
DE Linked List:
myQueue.insert(20); H->20<-T
myQueue.insert(40); H->20->40<-T
myQueue.remove();   H->40<-T
myQueue.insert(60); H->40->60<-T
myQueue.insert(80); H->40->60->80<-T

Array:
myQueue.insert(20); |20| | |
myQueue.insert(40); |20|40| |
myQueue.remove();   | |40| |
myQueue.insert(60); | |40|60|
myQueue.insert(80); |80|40|60|

Key:

Array elements are represented by values between two '|'
Bold parts are the outputs in final state

Add a comment
Know the answer?
Add Answer to:
1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You implemented a stack as a singly linked list (you add at the head and remove...

    You implemented a stack as a singly linked list (you add at the head and remove from the head). Perform the following operations on the stack: push(50), push(90), push(30), push(53), push(52), pop(), push(51), pop(), pop(), push(100), and push(15). After all the operations are performed, draw the resulting linked list. Indicate which node is the top of the stack in the resulting linked list.

  • Which of the following terms is NOT associated with a stack? push top get bottom Flag...

    Which of the following terms is NOT associated with a stack? push top get bottom Flag this Question Question 21 pts Which of the following terms is NOT associated with a queue? add front FIFO enqueue pop Flag this Question Question 31 pts A stack exhibits what kind of behavior? Last In, First Out (LIFO) First In, First Out (FIFO) Last In, Last Out (LILO) Read-Only Write-Only Flag this Question Question 41 pts What are the final contents of myQueue...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Any help with this is appriciated Array-Based Linked List Implementation Implement an array-based Linked List in...

    Any help with this is appriciated Array-Based Linked List Implementation Implement an array-based Linked List in Java. Use your Use your Can class as a JAR. You need to create a driver that makes several cans and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE Java's List. You will receive zero points....

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • 4. Given a stack variable stk which provides a push operation that places an integer o...

    4. Given a stack variable stk which provides a push operation that places an integer o top of the stack, and a pop operation which removes and returns the integer from t top of the stack, what would the stack look like, after the following code executed? for(int k 1; k < 10; k++) if(k % 3 :0) stk.push( k+ stk.pop()); else stk.push( k); 25. Given a queue variable que which provides an add operation which places an integer at...

  • Name: Each question is worth 1 point. 20 1. In a linked-chain implementation of a Stack...

    Name: Each question is worth 1 point. 20 1. In a linked-chain implementation of a Stack ADT, the performance of pushing an entry onto the stack is a. 0(2) b. О(n) С. 0(r) Answer: What is the entry returned by the peek method after the following stack operations. push(A), push(R), pop0. push(D), popO, push(L), pop0, pushJ), push(S), pop). pop 2. b.S c. L d. D Answer: n an efficient array-based chain implementation of a Stack ADT, the entry peek returns...

  • Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

    Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT