Hi guys,
Not sure if this should be in the beginner section. Perhaps it should be.
I have this code that I need to memorize for my final. Memorizing code is easy for me, but I'm trying pretty hard to fundamentally understand the functions, and what they are doing (even using pen and paper, to draw and trace).
I understand what linked lists do and what they are, but some parts of the code kind of confuse me. For example, in the push function below, I understand everything, except why I'm setting ptr = p. I feel like p should be equal to NULL, then the next node I push should be equal to p, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
Stack & Stack::push(double x)
{
Node * p = NULL;
try
{
p = new Node;
}
catch(const bad_alloc &)
{
die("Stack::push: Alloc. failure");
}
p->data = x;
p->next = ptr;
ptr = p;
return *this;
}
|
The next function I'm trying to understand is the pop function. We are returning a double. I'm confused as to why we need to create a temporary Node * p, and then delete it.
1 2 3 4 5 6 7 8 9 10 11 12
|
double Stack::pop()
{
if (ptr == NULL) die("Stack::pop: underflow");
Node * p = ptr;
double x = ptr->data;
ptr = ptr->next;
delete p;
return x;
}
|
Also, are LL Queues that hard to implement once you can do them w/stacks - That will probably be something I have to code for my final, as well.
Below is the full code for my Stack class.
Thanks.
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Stack
{
public:
Stack();
~Stack();
Stack & push(double x);
double pop();
double top() const;
const Stack & show() const;
unsigned size() const;
private:
struct Node
{
double data;
Node * next;
};
Node * ptr;
};
void die(const string & msg);
int main()
{
Stack s;
s.push(2.2).push(3.3).push(4.4);
s.show();
s.pop();
cout << endl;
s.show();
return 0;
}
Stack::Stack()
{
ptr = NULL;
}
Stack::~Stack()
{
for (Node * p; ptr; ptr = p)
p = ptr->next;
delete ptr;
}
Stack & Stack::push(double x)
{
Node * p = NULL;
try
{
p = new Node;
}
catch(const bad_alloc &)
{
die("Stack::push: Alloc. failure");
}
p->data = x;
p->next = ptr;
ptr = p;
return *this;
}
double Stack::top() const
{
if (ptr == NULL) die("Stack::top: underflow");
return ptr->data;
}
double Stack::pop()
{
if (ptr == NULL) die("Stack::pop: underflow");
Node * p = ptr;
double x = ptr->data;
ptr = ptr->next;
delete p;
return x;
}
unsigned Stack::size() const
{
unsigned count = 0;
for (Node * p = ptr; ptr != NULL; p = p->next)
count++;
return count;
}
const Stack & Stack::show() const
{
for( Node * p = ptr; p != NULL; p = p->next ){
cout << p->data << endl;
}
return *this;
}
void die(const string & msg)
{
cerr << "Fatal error: " << msg;
exit(EXIT_FAILURE);
}
|