Question

In: Computer Science

MAKE node class outside of stack class class Stack{    private:        class node{   ...

MAKE node class outside of stack class

class Stack{
   private:
       class node{
           public:
               node *next;
               int data;
               node(int d,node *n = NULL){
                   data = d;
                   next = n;
               }
       };
       node *start;
      
   public:
       Stack();
       Stack(const Stack& original);
       ~Stack();
      bool isEmpty() const ;
       int top() const;
      int pop() ;
       void push(int);
};

Stack::Stack(){
   start = NULL;
}

Stack::Stack(const Stack& original){
   if (original.isEmpty()) {
       start = NULL;
   } else {
       node* p = original.start; // points to current node on other
       node* tmp = new node(p->data); // make a copy of the first node
       start = tmp;
       node* tail = tmp; // points to last node of this list
       while (p->next != NULL) {
           p = p->next;
           tmp = new node(p->data);
           tail->next = tmp;
           tail = tmp;
       }
   }
}

Stack::~Stack(){
   node * curr = start;
   node *next;
   while(curr!=NULL){
       next = curr->next;
       delete curr;
       curr = next;
   }
}

bool Stack::isEmpty() const{
   if(start==NULL)
       return 1;
   return 0;
}

int Stack::top() const{
   if(isEmpty())
       throw "Stack is Empty";  
   return (start->data);
  
}

void Stack::push(int e){
   node *p = new node(e);
   if(isEmpty()){
       start = p;
   }else{
       p->next = start;
       start = p;
   }
  
}

int Stack::pop() {
   if(isEmpty())
       throw "Stack is Empty";
   else
   {
       node *p = start;
       start = start->next;
       int d = p->data;
       delete p;
       return d;
   }
}

/* A TEST PROGRAM */

#include<iostream>
using namespace std;

class Stack{
   private:
       class node{
           public:
               node *next;
               int data;
               node(int d,node *n = NULL){
                   data = d;
                   next = n;
               }
       };
       node *start;
      
   public:
       Stack();
       Stack(const Stack& original);
       ~Stack();
      bool isEmpty() const ;
       int top() const;
      int pop() ;
       void push(int);
};

Stack::Stack(){
   start = NULL;
}

Stack::Stack(const Stack& original){
   if (original.isEmpty()) {
       start = NULL;
   } else {
       node* p = original.start; // points to current node on other
       node* tmp = new node(p->data); // make a copy of the first node
       start = tmp;
       node* tail = tmp; // points to last node of this list
       while (p->next != NULL) {
           p = p->next;
           tmp = new node(p->data);
           tail->next = tmp;
           tail = tmp;
       }
   }
}

Stack::~Stack(){
   node * curr = start;
   node *next;
   while(curr!=NULL){
       next = curr->next;
       delete curr;
       curr = next;
   }
}

bool Stack::isEmpty() const{
   if(start==NULL)
       return 1;
   return 0;
}

int Stack::top() const{
   if(isEmpty())
       throw "Stack is Empty";  
   return (start->data);
  
}

void Stack::push(int e){
   node *p = new node(e);
   if(isEmpty()){
       start = p;
   }else{
       p->next = start;
       start = p;
   }
  
}

int Stack::pop() {
   if(isEmpty())
       throw "Stack is Empty";
   else
   {
       node *p = start;
       start = start->next;
       int d = p->data;
       delete p;
       return d;
   }
}

Solutions

Expert Solution

Code: Made node class outside of stack class

#include <cstddef>

class node {
public:
   node *next;
   int data;
   node(int d, node *n = NULL) {
       data = d;
       next = n;
   }
};

class Stack {
private:
   node *start;
public:
   Stack();
   Stack(const Stack& original);
   ~Stack();
   bool isEmpty() const;
   int top() const;
   int pop();
   void push(int);
};

Stack::Stack() {
   start = NULL;
}

Stack::Stack(const Stack& original) {
   if (original.isEmpty()) {
       start = NULL;
   }
   else {
       node* p = original.start; // points to current node on other
       node* tmp = new node(p->data); // make a copy of the first node
       start = tmp;
       node* tail = tmp; // points to last node of this list
       while (p->next != NULL) {
           p = p->next;
           tmp = new node(p->data);
           tail->next = tmp;
           tail = tmp;
       }
   }
}

Stack::~Stack() {
   node * curr = start;
   node *next;
   while (curr != NULL) {
       next = curr->next;
       delete curr;
       curr = next;
   }
}

bool Stack::isEmpty() const {
   if (start == NULL)
       return 1;
   return 0;
}

int Stack::top() const {
   if (isEmpty())
       throw "Stack is Empty";
   return (start->data);

}

void Stack::push(int e) {
   node *p = new node(e);
   if (isEmpty()) {
       start = p;
   }
   else {
       p->next = start;
       start = p;
   }

}

int Stack::pop() {
   if (isEmpty())
       throw "Stack is Empty";
   else
   {
       node *p = start;
       start = start->next;
       int d = p->data;
       delete p;
       return d;
   }
}

/* A TEST PROGRAM */

#include<iostream>
using namespace std;

class node {
public:
   node *next;
   int data;
   node(int d, node *n = NULL) {
       data = d;
       next = n;
   }
};

class Stack {
private:
  
   node *start;

public:
   Stack();
   Stack(const Stack& original);
   ~Stack();
   bool isEmpty() const;
   int top() const;
   int pop();
   void push(int);
};

Stack::Stack() {
   start = NULL;
}

Stack::Stack(const Stack& original) {
   if (original.isEmpty()) {
       start = NULL;
   }
   else {
       node* p = original.start; // points to current node on other
       node* tmp = new node(p->data); // make a copy of the first node
       start = tmp;
       node* tail = tmp; // points to last node of this list
       while (p->next != NULL) {
           p = p->next;
           tmp = new node(p->data);
           tail->next = tmp;
           tail = tmp;
       }
   }
}

Stack::~Stack() {
   node * curr = start;
   node *next;
   while (curr != NULL) {
       next = curr->next;
       delete curr;
       curr = next;
   }
}

bool Stack::isEmpty() const {
   if (start == NULL)
       return 1;
   return 0;
}

int Stack::top() const {
   if (isEmpty())
       throw "Stack is Empty";
   return (start->data);

}

void Stack::push(int e) {
   node *p = new node(e);
   if (isEmpty()) {
       start = p;
   }
   else {
       p->next = start;
       start = p;
   }

}

int Stack::pop() {
   if (isEmpty())
       throw "Stack is Empty";
   else
   {
       node *p = start;
       start = start->next;
       int d = p->data;
       delete p;
       return d;
   }
}


Related Solutions

Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); pu
Stack Class //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Queue Class public class Stack { private java.util.ArrayList pool = new java.util.ArrayList(); public Stack() { }    public Stack(int n) { pool.ensureCapacity(n); }    public void clear() { pool.clear(); }    public boolean isEmpty() { return pool.isEmpty(); }    public Object topEl() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.get(pool.size()-1); }    public Object pop() { if (isEmpty()) throw new java.util.EmptyStackException(); return pool.remove(pool.size()-1); }    public void push(Object el) { pool.add(el); }    public String toString() {...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT