Question

In: Computer Science

Implement a non-recursive reverse print of linked list using stack and the main function to test:...

Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test.


public class LinkedStack2<T> extends LinkedStack<T> {

    private void revPrint(LLNode<T> listRef) {
        if (listRef != null) {

            revPrint(listRef.getLink());
            System.out.println(" " + listRef.getInfo());
        }
    }

    public void printReversed() {

        revPrint(top);
    }

    /* use stack to implement non-recursive reverse print */
    public void printReversed_nonrecursive() {

    }
public class UseStack2 {

    public static void main(String[] args) {

        LinkedStack2<String> myStack2 = new LinkedStack2<String>();
        myStack2.push("first");
        myStack2.push("second");
        myStack2.push("third");
        myStack2.push("fourth");
        myStack2.printReversed();
        myStack2.printReversed_nonrecursive();
    }
}

Solutions

Expert Solution

public class LinkedStack2<T> extends LinkedStack<T> {

private void revPrint(LLNode<T> listRef) {
if (listRef != null) {

revPrint(listRef.getLink());
System.out.println(" " + listRef.getInfo());
}
}

public void printReversed() {
  
revPrint(top);
}

/* use stack to implement non-recursive reverse print */
public void printReversed_nonrecursive() {
   LinkedStack2<String> revStack = new LinkedStack2<String>(); 'reverse stack
   LLNode<T> listRef=top;
   while(listRef!=null){
       revStack.push(listRef.getInfo());
       listRef=listRef.getLink();
   }
   while(revStack.top!=null){
       listRef=revStack.pop();
   System.out.println(" " + listRef.getInfo());
   }      

}

public class UseStack2 {

public static void main(String[] args) {

LinkedStack2<String> myStack2 = new LinkedStack2<String>();
myStack2.push("first");
myStack2.push("second");
myStack2.push("third");
myStack2.push("fourth");
myStack2.printReversed();
myStack2.printReversed_nonrecursive();
}
}

// Base codes are missing in this class LinkedStack<T> however I have added the line of codes based on stack.

// The idea is loop through the linked list one by one and push it to stack, once completed start another loop of stack and pop it out one by one to print in reverse order.


Related Solutions

using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print Make your unorderedList a list of characters instead of integers Insert ten characters into the list and print it out both ways #include <iostream> #include <string> #include <cstdlib> using namespace std; struct node { int info; node* next; }; class unorderedList { private: int length; node* listPtr; public: unorderedList() {length = 0; listPtr = NULL;} void makeEmpty(); void insertItem(int item); void printList(); bool isFull()...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
/* print_reverse function that prints the nodes in reverse order if the list is empty print...
/* print_reverse function that prints the nodes in reverse order if the list is empty print nothing */ include <bits/stdc++.h> using namespace std; class Node{     public:     int data;     Node *next;     Node *prev;     Node(int d, Node *p=NULL, Node *n=NULL){         data = d;         prev = p;         next = n;     } }; class DLL{     public:     Node *head;     DLL(){head=NULL;}     void push(int d){         Node *pNode = new Node(d,NULL,head);         if (head != NULL)             head->prev = pNode;         head = pNode;     }     void print_forward(){         Node *pCurr = head;...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT