Help with linked list

closed account (Dy7SLyTq)
so im writing a linked list for a symbol table, but im getting a vast amount of errors.

Token.hpp:
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
#ifndef _Token_hpp
#define _Token_hpp

#include <string>

using std::string;

class Token
{
    string Type   = "",
           Name   = "";
    long   Line   = -0,
           Column = -1;

    public:
             Token    ();
        void SetNode  (string, string, long, long);
        void PrintNode();
};

class TokenList
{
    Token     *Value;
    TokenList *Next,
              *Head,
              *Tail;

    public:
        TokenList();

        void PushBack(string, string, long, long);
        void Print   ();
};
#endif 


Token.cpp
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
#include <iostream>
#include   <string>

#include "Token.hpp"

using std::  cout;
using std::  cerr;
using std::  endl;
using std::string;

Token::Token() {}

void Token::SetNode(string type, string name, long line, long column)
{
    this->Type   = type;
    this->Name   = name;
    this->Line   = line;
    this->Column = column;
}

void Token::PrintNode()
{
    cout<<"("<< this->Type <<", "<< this->Name <<", "
        << this->Line <<", "<< this->Column <<")"<< endl;
}

TokenList::TokenList()
{
    Head = 0;
    Tail = 0;
}


void TokenList::PushBack(string Type, string Name, long Line, long Column)
{
    TokenList *Temp = new TokenList;

    Temp->Value->SetNode(Type, Name, Line, Column);
    Temp->Next = 0;

    if(Head != NULL && Tail != NULL)
    {
        Tail->Next = Temp;
        Tail = Temp;
    }

    else
    {
        Head = Temp;
        Tail = Temp;
    }
}

void TokenList::Print()
{
    TokenList* Counter = Head;

    while(Counter != NULL)
    {
        cout<< Counter->Value->PrintNode() << endl;
        Counter = Counter->Next;
    }
}


and then the test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Token.hpp"

int main()
{
    TokenList SymbolTable;

    SymbolTable.PushBack("Nope", "Nope", -1, 0);
    SymbolTable.PushBack("Nope", "Nope", -1, 1);
    SymbolTable.PushBack("Nope", "Nope", -1, 2);
    SymbolTable.PushBack("Nope", "Nope", -1, 3);
    SymbolTable.PushBack("Nope", "Nope", -1, 4);
    SymbolTable.PushBack("Nope", "Nope", -1, 5);
    SymbolTable.PushBack("Nope", "Nope", -1, 6);
    SymbolTable.PushBack("Nope", "Nope", -1, 7);
    SymbolTable.PushBack("Nope", "Nope", -1, 8);
    SymbolTable.PushBack("Nope", "Nope", -1, 9);
    SymbolTable.PushBack("Nope", "Nope", -1, 10);
    SymbolTable.PushBack("Nope", "Nope", -1, 11);
    SymbolTable.PushBack("Nope", "Nope", -1, 12);

    SymbolTable.Print();
}
closed account (Dy7SLyTq)
never mind i figured it out. to those who are curious look at line 60 of Token.cpp
closed account (Dy7SLyTq)
The only problem is now it seg faults. i would appreciate any help
closed account (o1vk4iN6)
Just a suggestion you should move "next" out of the list class. That should be in a node. Just design wise.

1
2
3
4
TokenList *Temp = new TokenList;

// you don't allocate and set Value, invalid pointer being used
Temp->Value->SetNode(Type, Name, Line, Column);


closed account (Dy7SLyTq)
im sorry. could you treat me more like a beginner with this? i understand the concept of a singly linked but i have never tried it before so this is very new to mee
closed account (o1vk4iN6)
1
2
3
4
5
6
7
8
9
10
11
12
13
// doesn't need to know about head or tail
// so don't include it
class Node
{
    Node* next;
    SomeType data;
};

class List
{
    Node* head;
    Node* tail;
};


Anyway the problem isn't the structure though it helps if you don't shove everything into one class. It is clear above what the node is and a list is.

1
2
3
4
TokenList *Temp = new TokenList;

Temp->Value = new Value; // need to init this, could be in TokenList constructor
Temp->Value->SetNode(Type, Name, Line, Column);
closed account (Dy7SLyTq)
thank you! that worked!
Topic archived. No new replies allowed.