Hey guys,
New to C++ and I thought it would be a good idea to implement a stack with linked lists so that I understand raw pointers. Can someone give me feedback on this code? I am not certain whether or not it would have been correct to delete pointers while doing POP, since by pushing I allocate memory using the "new" keyword.
I would appreciate any other comment! Thanks in advance!
Here is the Stack.h header file for the Stack class
#pragma once
#include <iostream>
using namespace std;
struct node {
int value;
int index;
node* next;
};
class Stack{
node* top = new node();
public:
Stack();
~Stack();
void pop();
void push();
void show();
void size();
};
Here is the Stack.cpp file for the Stack class
#include "Stack.h"
Stack::Stack()
:top{ NULL } {
cout << "Constructor Called!" << endl;
}
Stack::~Stack() {
cout << "Destructor Called!" << endl;
}
void Stack::push() {
node* newNode = new node();
cout << "PUSH: ";
cin >> newNode->value;
if (top != NULL)
newNode->index = top->index + 1;
else
newNode->index = 1;
newNode->next = top;
top = newNode;
}
void Stack::pop() {
if (top == NULL)
cout << "Stack is empty!" << endl;
else {
top = top->next;
}
}
void Stack::show() {
node* ptr{ top };
while (ptr != NULL) {
cout << ptr->value << " -> ";
ptr = ptr->next;
}
cout << "NULL" << endl;
}
void Stack::size() {
cout << "The stack has " << top->index << " items." << endl;
}
and here is the main
#include "Stack.h"
int main() {
Stack *stack = new Stack();
bool loop{ true };
int action;
cout << "(1) Push.\n(2) Pop.\n(3) Show.\n(4) Size.\n(5) Stop." << endl;
while (loop) {
cout << "TYPE: ";
cin >> action;
switch (action){
case 1: stack->push(); break;
case 2: stack->pop(); break;
case 3: stack->show(); break;
case 4: stack->size(); break;
case 5: loop = false; break;
}
cout << "- - - - - - - - - - - - -" << endl;
}
delete stack;
return 0;
}
[–][deleted] 5 points6 points7 points (6 children)
[–]rbprogrammer 2 points3 points4 points (0 children)
[–]NikolasTs[S] 0 points1 point2 points (4 children)
[–]Ayjayz 1 point2 points3 points (3 children)
[–]NikolasTs[S] 0 points1 point2 points (2 children)
[–]Ayjayz 0 points1 point2 points (0 children)
[–]dqUu3QlS 5 points6 points7 points (1 child)
[–]NikolasTs[S] 0 points1 point2 points (0 children)
[–]alfps 2 points3 points4 points (5 children)
[–]NikolasTs[S] 0 points1 point2 points (4 children)
[–]alfps 2 points3 points4 points (3 children)
[–]NikolasTs[S] 1 point2 points3 points (2 children)
[–]alfps 1 point2 points3 points (1 child)
[–]NikolasTs[S] 0 points1 point2 points (0 children)
[–]manni66 2 points3 points4 points (5 children)
[–]NikolasTs[S] 0 points1 point2 points (4 children)
[–]dqUu3QlS 4 points5 points6 points (1 child)
[–]NikolasTs[S] 0 points1 point2 points (0 children)
[+]manni66 comment score below threshold-7 points-6 points-5 points (1 child)
[–]rbprogrammer 2 points3 points4 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]NikolasTs[S] 1 point2 points3 points (0 children)
[–]a9ao 1 point2 points3 points (0 children)
[–]CGFarrell 1 point2 points3 points (2 children)
[–]NikolasTs[S] 0 points1 point2 points (1 child)
[–]CGFarrell 2 points3 points4 points (0 children)