Help with stacks?

I am writing a program for a C++ class. I have run into an error using stacks, and a push / pop function of mine. I have two problems here.

The first is in the pop function. It will never properly detect if top == NULL.

Second, I'm not sure how to remove top off the stack, and point it back to the previous item. I realize I may be doing the link backwards though, but mainly my issue is that the pop function does not determine if top == NULL, even though I set top to NULL when the program starts.

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
#include<iostream>
using namespace std;

struct stacks
{
    int serialNum;
    char manufactDate;
    int lotNum;
    stacks *link;
};

class linkedStackType
{
private:
stacks *top, *newStack;
public:
void push(int newitem, char newdate, int newlot);
void pop();
};

int main()
{
    int num = 0;
	int choice = 0;
	stacks *newStack, *top;
    top = NULL;
    while(num != 99)
    {
    newStack = new stacks;
    newStack->link = NULL;
    linkedStackType compute;
    cout << "Would you like to add to the inventory list, or remove from the list?" << endl;
    cout << "Enter 1 to add, enter 2 to remove." << endl;
    cin >> choice;
    if(choice == 1)
    {
        cout << "Please enter the new serial number : " << endl;
        cin >> newStack->serialNum;
        cout << "Please enter the new manufacter date : " << endl;
        cin >> newStack->manufactDate;
        cout << "Please enter the new Lot number : " << endl;
        cin >> newStack->lotNum;
        compute.push(newStack->serialNum, newStack->manufactDate, newStack->lotNum);
        if (top == NULL)
        {
            top = newStack;
            top->link = NULL;
        }
        else
        {
		newStack->link = top;
		top = newStack;
        }
    }
    else
        compute.pop();

    cout << "Enter 99 to quit, or anything else to continue." << endl;
    cin >> num;
    }

    while(top != NULL)
    {
        cout << "Serial Number " << top->serialNum << endl;
        cout << "Manufactured Date " << top->manufactDate << endl;
        cout << "Lot Number " << top->lotNum << endl;
        top = top->link;
    }
    return 0;
}

void linkedStackType::push(int newitem, char newdate, int newlot)
{
    stacks *temp;
	temp = new stacks;
    temp->serialNum = newitem;
    temp->manufactDate = newdate;
    temp->lotNum = newlot;
    temp->link = top;
    top = temp;
}

void linkedStackType::pop()
{
    if(top == NULL)
    {
    stacks *temp;
    temp = top;
    top = newStack;
    delete temp;
    }
}
Last edited on
top and newStack in linkedStackType is not the same as top and newStack in main. They are different variables containing different values.
Actually, you're right.

How would I make them all the same value?

Just include top and newStack in the function calls to pass them to the functions?
Last edited on
Topic archived. No new replies allowed.