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...
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"....
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
(I have to inteview a nurse for school. Due to the virus my teacher allowed us...
(I have to inteview a nurse for school. Due to the virus my teacher allowed us to interview them online. Thank you!) 1- How do you feel about the proliferation of Herbal and Nutritional Supplements? How do you advise individuals seeking an herbal medication? 2- Do prescriptions that are changed to Over-the-Counter affect the role of the pharmacist?    3- What is your opinion in regards to the role of the registered nurse in medication administration and teaching?    4-...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT