Question

Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “po...

Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “pop”.  The method should return a value (in the “popped” node).  Assume the existence of the node references called “TheStack” and “Top”. These references point to the start (or bottom) and top of the stack (or back of the list).

-------

Definition:

            class node {

          boolean data;

          node link;

}          

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

package stackLinkedList;

public class LinkedStack {

private int size;

private Node first;

/**

* Adds the item to this stack.

*/

public void push(Node node) {

node.link = first;

first = node;

size++;

}

/**

* Delete item from stack.

*/

public Node pop() {

if (isEmpty()) {

return null;

}

Node temp = first;

first = first.link;

size--;

return temp;

}

/**

* check stack is empty.

*/

public boolean isEmpty() {

return (first == null);

}

/**

* show all stack data

*/

public void displayStack() {

Node current = first;

while (current != null) {

current.displayNode();

current = current.link;

}

}

public static void main(String[] args) {

LinkedStack linkedStack = new LinkedStack();

linkedStack.push(new Node(true));

linkedStack.push(new Node(true));

linkedStack.push(new Node(false));

linkedStack.push(new Node(true));

linkedStack.push(new Node(true));

linkedStack.push(new Node(true));

linkedStack.displayStack();

System.out.println("Stack Size" + linkedStack.size);

Node popedNode=linkedStack.pop();

while (popedNode!=null) {

System.out.println("Stack Size" + linkedStack.size+"\t"+popedNode.data);

popedNode=linkedStack.pop();

}

}

}

//////////////////////Node class////////////////

package stackLinkedList;

public class Node {

public boolean data;

public Node link;

public Node(boolean data) {

this.data = data;

}

public void displayNode() {

System.out.println(data);

}

}

////////////////////////Output//////////////

true

true

true

false

true

true

Stack Size6

Stack Size5 true

Stack Size4 true

Stack Size3 true

Stack Size2 false

Stack Size1 true

Stack Size0 true

Add a comment
Know the answer?
Add Answer to:
Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “po...
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
  • Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....

    Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • Write the pseudocode for a non-member method“dequeue” which is utilizing a linked list. The list node has 2 attributes:...

    Write the pseudocode for a non-member method“dequeue” which is utilizing a linked list. The list node has 2 attributes: data and next.  The method would take a “front” and “rear” reference and will return a “character” (the data element from the de-queued node). **Special note: DO NOT ASSUME THE EXISTENCE OF ANY OTHER METHODS (i.e. don’t use any other methods).  Use basic definitions and standard naming conventions for the stack and queue data structures, figure out the requirements, inputs &...

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • Write a JAVA contains method for a linked implementation of a sorted list. #This method is...

    Write a JAVA contains method for a linked implementation of a sorted list. #This method is written from the implementation perspective, meaning it would go inside of the LinkedSortedList class. This means we have access to firstNode and numberOfEntries. #write an efficient solution. This will involve directly accessing the linked structure rather than only invoking existing methods. You can assume T is Comparable. #The method header is: public boolean contains(T anEntry)

  • 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...

  • ii. (30) A Stack is a container commonly used to provide a way to keep organized...

    ii. (30) A Stack is a container commonly used to provide a way to keep organized so that the last one pushed' onto the top of the stack is thth one 'popped (removed)- so stacks are called "last-in-first-out". on top of the stack is visible, but after removing the top item, t pushed item is visible, since it is back on top. The following Python co is a recursive definition of the class Stack. Read the code, and answer te...

  • 1. The Operand Stack - opstack The operand stack should be implemented as a Python list....

    1. The Operand Stack - opstack The operand stack should be implemented as a Python list. The list will contain Python integers, strings, and later in Part 2 code arrays. Python integers and lists on the stack represent Postscript integer constants and array constants. Python strings which start with a slash / on the stack represent names of Postscript variables. When using a list as a stack, assume that the top of the stack is the end of the list...

  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

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