Linked List - Stack
Nov 10, 2014 at 11:34pm UTC
Hello, I need help because my destructor is not working, I need some help because it is the only thing left that I have to do and I dont know what is wrong.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include <iostream>
using namespace std;
class LinkedL {
private :
struct node{
int data;
node *link;
};
node *top;
public :
LinkedL();
~LinkedL();
void Push(int );
int Pop();
int RetrieveStack();
bool StackIsEmpty();
void display();
};
LinkedL::LinkedL(){
top = NULL;
}
void LinkedL::Push(int element){
node *p = new node;
p -> data = element;
p -> link = top;
top = p;
}
int LinkedL::Pop(){
if (StackIsEmpty()){}
else {
node *temp = top;
top = temp -> link;
return temp -> data;
}
}
int LinkedL::RetrieveStack(){
if (StackIsEmpty()){}
else {
return top -> data;
}
}
bool LinkedL::StackIsEmpty(){
if (top == NULL){
return true ;
}
else {
return false ;
}
}
void LinkedL::display(){
node *p = top;
if (StackIsEmpty()){}
else {
while (top != NULL){
cout << p -> data;
p = p -> link;
}
}
}
LinkedL::~LinkedL(){
node *cursor;
while (top != NULL){
cursor = top;
top = top->link;
delete cursor;
}
}
int main() {
LinkedL hola;
hola.Push(10);
hola.Push(20);
hola.Push(30);
hola.Push(40);
hola.Push(50);
hola.display();
hola.Pop();
hola.display();
hola.display();
system("Pause" );
return 0;
}
Topic archived. No new replies allowed.