In: Computer Science
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.
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: