Stack of Tokens will not "pop()"

Hi, I am currently writing a program that converts an infix notation expression to postfix notation. Part of the program involves using a linked list to temporarily store Token objects (of type char or double). The code I am using seems to properly push() the tokens onto the stack. I am also able to return the top token from the stack using top(). However, whenever I make a call to pop() to remove the head node from the stack, my program craps out. I have been at this for weeks now trying to find solutions online and I am getting tired. I finally worked up the courage to post a request for help here since I often find myself frequently visiting this website.

The code here is definitely condensed. It does not contain my Expression class (which takes in an equation and converts it to postfix notation) and few other functions. However, regardless the program still does not work when trying to manipulate the stack using pop().

Thanks for any help, it is greatly appreciated.

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353

#include <iostream>
#include <string>
#include <queue>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;

/********************************************************************************/

/*************************************************
*
*
* This is the start of Token.h
*
*
*/
#ifndef TOKEN_H
#define TOKEN_H

enum TokenType {opType, valType};

class Token
{
    friend std::ostream& operator<<(std::ostream& o, Token t);
    public:
        Token(char c);
        Token(double d);
        bool isOp() {return type == opType;}
        bool isVal() {return type == valType;}
        char getOp() {return op;}
        double getVal() {return val;}
        int precedence();
    protected:
    private:
        TokenType type;
        char op;    /// if type == op, this field is valid
        double val; /// if type == number, this field is valid
};

#endif // TOKEN_H

/*
*
* This is the End of Token.h
*
*
*******************************************************/

/*****************************************************
*
*
*This is the start of Token.cpp
*
*
*/

Token::Token(char c)
{
    op = c;           // store the operator
    type = opType;    // set the type to be an operator
}

Token::Token(double d)
{
    val = d;          // store the value
    type = valType;   // set the type to be a value
}

// overloaded output operator
std::ostream& operator<<(std::ostream& o, Token t)
{
    if (t.isOp())
    {
        std::cout << "Operator: " << t.getOp();
    }
    else if (t.isVal())
    {
        std::cout << "Number:   " << t.getVal();
    }
}

int Token::precedence()
{
    switch (op)
    {
        case '!': return 8;
        case '^': return 7;
        case '*':
        case '/': return 6;
        case '+':
        case '-': return 5;
        //case gt,ge,lt,le: return 4;
        //case eq, ne : return 3;
        //case '&' : return 2;
        //case '|' : return 1;
        case '(': return 0;
  }
}


/*
*
*
* This is the end of Token.cpp
*
***********************************************************/



/**************************************************************************/

/****************************************************************************/

/*
*
*
* This is the beginning of LinkedList.h
*
*
*/
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

struct Node
{
    Node *next;
    Token *data;
};

class LinkedList
{
    friend ostream& operator<<(ostream &o, LinkedList ll);
    public:
        LinkedList();
        virtual ~LinkedList();
        void head_insert(Token s);
        int count();
        Token head_delete();
        bool empty() {return !head;}

    protected:
        Node *head;
        Node *tail;
    private:
        int count(Node *);

};

#endif // LINKEDLIST_H

/*
*
*
* This is the end of LinkedList.h
*
*
***************************************************************/

/****************************************************************************/
/*
*
*
* This is the beginning of LinkedList.cpp
*
*
*/

LinkedList::LinkedList()
{
    head = tail = NULL;
}

LinkedList::~LinkedList()
{
    //dtor
}

void LinkedList::head_insert(Token s)
{
    Node * n = new Node;



    if (!head)  // empty list
    {
        head = tail = n;
        head->data = &s;
        head->next = NULL;
    }
    else        // non-empty list
    {
        n->next = head;
        head = n;
        n->data = &s;
    }
}

ostream& operator<<(ostream &o, LinkedList ll)
{
    Node *ptr = ll.head;
    while (ptr != NULL)
    {
        o << ptr->data << ", ";
        ptr = ptr->next;
    }
    return o;
}

int LinkedList::count()
{
    return count(head);
}

int LinkedList::count(Node *list)
{
    if (!list)    {
        return 0;
    }
    else
    {
        return 1+count(list->next);
    }
}

Token LinkedList::head_delete()
{
    Token * retToken;

    if (!head)
    {
        cerr << "Attempting to delete from an empty list" << endl;
        exit(1);
    }

    if (head == tail)  // one element
    {
        retToken = head->data;
        delete head;
        head = tail = NULL;
        return *retToken;
    }

    retToken = head->data;
    Node *deleteMe = head;
    head = head->next;
    delete deleteMe;
    return *retToken;
}


/*
*
*
* This is the end of LinkedList.cpp
*
*
*******************************************************************/

/*****************************************************************************/

/************************************************************
*
*
* This is the beginning of Stack.h
*
*
*/
#ifndef STACK_H
#define STACK_H


class Stack : private LinkedList
{
  public:
      Stack();
      void push(Token element) {head_insert(element);}
      Token pop() {return head_delete();}
      Token top() {return *(head->data);}
      bool empty() {return head == NULL;}
  protected:
  private:
};

#endif //STACK_H

/*
*
*
* This is the end of stack.h
*
*
***************************************************************/

/****************************************************************************/

/**************************************************************
*
*
* This is the beginning of stack.cpp
*
*
*/


Stack::Stack()
{
    //ctor
}

/*
*
*
* This is the end of stack.cpp
*
*
***************************************************************/

/************************************************************************/

/**************************************************************
*
*
* This is the beginning of main.cpp
*
*
*/

int main()
{
    Stack s;

    // creating 4 tokens, 2 char, 2 doubles
    Token token1('+');
    Token token2('-');
    Token token3(90.21);
    Token token4(13.78);

    // pushing tokens onto the stack
    s.push(token1);
    s.push(token2);
    s.push(token3);
    s.push(token4);

    // attempting to pop (deletes head node) and display top (should be 90.21)
    s.pop();
    std::cout << s.top() << std::endl;


    return 0;
}
In function void LinkedList::head_insert(Token s) why do this?
1
2
3
4
5
6
    else        // non-empty list
    {
        n->next = head;
        head = n;
        n->data = &s;
    }
Are new nodes appended to the end or inserted at the head of the linked list?

Your linked list has member tail, but it's not set in that insert function.

Finally, why does the node hold a pointer to a Token rather than the Token itself?
Last edited on
New nodes are appended to the head of the linked list. There is a tail node because originally there was a tail insert function that would append them to the end of the list. However, I quickly realized that my function tail_insert() was not needed so I removed it. I did not however remove the node tail (must of slipped my mind).

I have tried to have the node hold the token itself (instead of a pointer) but it ended up creating more problems. At:

1
2
3
4
5
struct Node
{
    Node *next;
    Token *data;
};


When declaring a Token I have to initialize it as a char or a double, such as:

 
Token data('*');


Otherwise I cannot just simply declare it as:

 
Token data;


That is why I used a pointer.
The default copy of Token will do the right thing. (And even if it didn't you would make it so.) So the Node can hold a Token, and not just a pointer to a Token.

head_delete() uses tail, but it's not maintained in head_insert(), so there is some inconsistency there.

The link list is fine, my mistake.

The problem is pop works, but you call top after you've popped the stack. That's why you loose the value.
Yes, you were correct. I added a default Token constructor and removed the pointer for data and it worked (after a few other minor changes). Thanks a bunch for the help, I will mark as solved.
Topic archived. No new replies allowed.