Question

In: Computer Science

I have a programming project due tonight that I have already done once but my professor...

I have a programming project due tonight that I have already done once but my professor sent back because he said I did it wrong. He wants the following using linked lists but i only know how to do it with Arrays. Please try to explain your answer to me and not just give it to me.

For this assignment you must write a program that implements a stack of integers. The stack should be implemented using classes and must support the push, pop, and peek stack operations, the isEmpty method, and overload operator<<. Your stack implementation should also handle popping or peeking at data from an empty stack without generating a runtime error. Your program should test the correctness of your implementation in function main. You should provide test case to verify your stack works for the following cases:

1. Pushing an element of data onto a newly created stack

2. Pushing an element of data onto a non-empty stack

3. Pushing an element of data onto an empty stack (Note: a stack is empty when it is newly created and after the last element is popped off the stack)

4. Popping a value from a newly created stack

5. Popping a value from a non-empty stack

6. Popping a value from an empty stack

7. Performing a sequence of consecutive pushes and pops

8. Peeking at the top of a newly create stack with peek

9. Peeking at the top of a non-empty stack

10. Peeking at the top of an empty stack.

Files Expected:

1. Main.cpp – File containing function main

2. Stack.h - File containing the Stack and Node class structures and definitions.

Solutions

Expert Solution

Hi, Please find the solution and rate the answer. Everything is working great.

//

// Stack.hpp

// StackUsingLinkedList

//

#ifndef Stack_hpp

#define Stack_hpp

#include <stdio.h>

#include "iostream"

template <typename T>

class Node {

T val;

Node<T> *next;

  

public:

Node(T val):val(val){

next = nullptr;

}

void setNext(Node<T> *item){

next = item;

}

Node<T>* getNext(){

return next;

}

T getVal(){

return val;

}

};

template <typename X>

class Stack{

public:

Node<X> *head;

void push(Node<X> *newNode){

if(head==nullptr){

head=newNode;

newNode->setNext(nullptr);

return;

}

newNode->setNext(head);

head = newNode;

  

}

Node<X>* pop(){

if(head==nullptr){

return nullptr;

}

Node<X> *temp = head;

head = temp->getNext();

temp->setNext(nullptr);

return temp;

}

Node<X>* peek(){

return head;

}

  

void printStack(){

Node<X> *temp = head;

std::cout<<std::endl;

while(temp->getNext()!=nullptr){

std::cout<<temp->getVal()<<", ";

temp=temp->getNext();

}

}

};

#endif /* Stack_hpp */

Nothing in the Stack.cpp file.

//

// main.cpp

// StackUsingLinkedList

//

#include <iostream>

#include "Stack.hpp"

using namespace std;

int main(int argc, const char * argv[]) {

srand(time(0));

Node<int> *node = new Node<int>(rand()%1000);

Node<int> *node1 = new Node<int>(rand()%1000);

Node<int> *node2 = new Node<int>(rand()%1000);

Node<int> *node3 = new Node<int>(rand()%1000);

Node<int> *node4 = new Node<int>(rand()%1000);

  

Stack<int> st;

st.push(node);

st.push(node1);

st.push(node2);

cout<<"After pushing some elements:"<<endl;

st.printStack();

cout<<endl<<"Popping 2 elements now & printing"<<endl;

st.pop();

st.pop();

st.printStack();

cout<<endl<<"Peeking an element:"<<st.peek()->getVal();

  

st.printStack();

  

  

}

Please find sample Run:


Related Solutions

I have a project to make and my professor wants me to make a Synopsis. She...
I have a project to make and my professor wants me to make a Synopsis. She wants me to put together a synopsis of the SBA grant EIDL loan program and payroll protection program. The criteria that the small businesses have to meet, what potential benefit would these small businesses receive and so on and so forth. What is the best format for this Synopsis? Thank you.
I already finish the argumentative topic about abortion, but my professor does not want me to...
I already finish the argumentative topic about abortion, but my professor does not want me to write about it. Can someone help me write 500 words about this argumentation "Education should be free for everyone".
I am not aware of GR, but due to curiosity i have a question in my...
I am not aware of GR, but due to curiosity i have a question in my mind. Please let me know if it is inappropriate to ask here. My question is about singularity. I am under the assumption that singularity means in mathematical terms equivalent to a discontinuity in a function. My question is what type of discontinuity are the ones corresponding to Penrose-Hawking singularity theorems and also to the the naked singularity ? by type i mean removable (left...
FOR THE SCENARIO BELOW...I need to know if my hypotheses are directional or non-directional.(I already have...
FOR THE SCENARIO BELOW...I need to know if my hypotheses are directional or non-directional.(I already have the hypotheses) I know I need to use a two sample T test but need help with the calculations. (Provide a sample size and critical values in relation to the hypothesis.) -Discuss what the statistical analysis will do in answering the hypotheses and question(s) for the client. Also discuss any potential problems to watch out for, including an appropriate sample size to meet the...
I have done some of this on my own but just cant seem to be able...
I have done some of this on my own but just cant seem to be able to finish this correctly. Dual-energy X-ray absorptiometry (DXA) is a technique for measuring bone health. One of the most common measures is total body bone mineral content (TBBMC). A highly skilled operator is required to take the measurements. Recently, a new DXA machine was purchased by a research lab, and two operators were trained to take the measurements. TBBMC for eight subjects was measured...
I have to finish the code given by my diff eq professor to analyze the lorenz...
I have to finish the code given by my diff eq professor to analyze the lorenz function at different initial conditions and different values for p. I am not sure how to input the lorenz function in the way the code is set up. Here is the code provided for matlab: function lorenz s = 10; b = 8/3; p = 0.15; y = @(t,x) [ ; ; ]; % you are responsible for entering the lorenz system here T...
I have a problem with the code for my project. I need the two clients to...
I have a problem with the code for my project. I need the two clients to be able to receive the messages but the code I have done only the server receives the messages. I need the clients to be able to communicate with each other directly not throught the server. The requirements of this "local chat" program must meet are: 1. The chat is performed between 2 clients and a server. 2. The server will first start up and...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
*********Please do not answer the question partially. I have already done step #1, now just needing...
*********Please do not answer the question partially. I have already done step #1, now just needing to complete all of the other steps (2-10)  an explantion included would be nice as well, I would appriciate it! :) *If you can not help completely or entirely; to the furthest extent please do not leave an answer******* Question 1        Required: #1. Prepare journal entries to record the December transactions in the General Journal Tab in the excel template file "Accounting Cycle Excel Template.xlsx"....
I have three different homework questions I cannot seem to get My hw is due at...
I have three different homework questions I cannot seem to get My hw is due at 12 so if anyone can help that would be helpful Thank you 1)   For a car moving with speed v, the force of air drag is proportional to v2. If the power output of the car's engine is quadrupled, by what factor does the speed of the car increase? 2)Tarzan (m = 76 kg) commutes to work swinging from vine to vine. He leaves...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT