coupling an int and string together in a node, linkedlist

Hey,
What this program does (the main), is read a txt file and create a node for every line with a for loop. For example
line1
line2
line3

They are all diffrent lines, thus diffrent nodes.
However, what I really want is to pick a random line and display it, after that it depends on the answer of the user wether the line is removed or not.

Now what I wanted to do is, create a random number, couple this number to a line, so
1. line1
2. line2
3. line3

Randomize the number and when for example number 2 is picked, I can print line2.
But this doesnt work, since number 2 is not coupled to line 2.

So to fix my problem: I need to couple number 2 to line2, number 3 to line3 etc, OR find a way to randomize strings without using int at all.

Though I have troubles with both :P

Here is my entire code, have a look if you want. The linkedlist I mainly got from internet, since I'm still learning to write my own :)

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
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


#include <iostream>
#include <cstddef>
#include <fstream>
#include <string>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

using namespace std;




/* definition of the list node class */
class Node
{
    friend class LinkedList;
private:
    string _value;
	int _waarde; /* data, can be any data type, but use integer for easiness */
    Node *_pNext; /* pointer to the next node */
    
public:
    /* Constructors with No Arguments */
    Node(void)
    : _pNext(NULL)
    { }
    
    /* Constructors with a given value */
    Node(string line,int ID)
    : _value(line), _waarde(ID), _pNext(NULL)
    { }
    
    /* Constructors with a given value and a link of the next node */
    Node(string line, int ID, Node* next)
    : _value(line), _waarde(ID), _pNext(next)
    {}
    
    /* Getters */
    string getValue(void)
    { return _value; }

	int getWaarde(void)
	{ return _waarde; } 
		    
    Node* getNext(void)
    { return _pNext; }
};

/* definition of the linked list class */
class LinkedList
{
private:
    /* pointer of head node */
    Node *_pHead;
    /* pointer of tail node */
    Node *_pTail;
    
public:
    /* Constructors with a given value of a list node */
    LinkedList(string line, int ID);
    /* Destructor */
    ~LinkedList(void);
    
    /* Function to append a node to the end of a linked list */
    void tailAppend(string line, int ID);
    
    
    /* Remove a node with a specific value if it exists */
    void remove(string line, int ID);
    
    
    /* Traversing the list and printing the value of each node */
    void traverse_and_print();
    
    void print(string line, int ID);
};

LinkedList::LinkedList(string line, int ID)
{
    /* Create a new node, acting as both the head and tail node */
    _pHead = new Node(line, ID);
    _pTail = _pHead;
}

LinkedList::~LinkedList()
{
    /*
     * Leave it empty temporarily.
     * It will be described in detail in the example "How to delete a linkedlist".
     */
}

void LinkedList::tailAppend(string line, int ID)
{
    /* The list is empty? */
    if (_pHead == NULL) {
        /* the same to create a new list with a given value */
        _pTail = _pHead = new Node(line, ID);
    }
    else
    {
        /* Append the new node to the tail */
        _pTail->_pNext = new Node(line, ID);
        /* Update the tail pointer */
        _pTail = _pTail->_pNext;
    }
}  

void LinkedList::remove(string line, int ID)
{
    Node *pPre = NULL, *pDel = NULL;
    
    /* Check whether it is the head node?
     if it is, delete and update the head node */
    if (_pHead->_waarde == ID) {
        /* point to the node to be deleted */
        pDel = _pHead;
        /* update */
        _pHead = pDel->_pNext;
        delete pDel;
        return;
    }
    
    pPre = _pHead;
    pDel = _pHead->_pNext;
    
    /* traverse the list and check the value of each node */
    while (pDel != NULL) {
        if (pDel->_waarde == ID) {
            /* Update the list */
            pPre->_pNext = pDel->_pNext;
            /* If it is the last node, update the tail */
            if (pDel == _pTail) {
                _pTail = pPre;
            }
            delete pDel; /* Here only remove the first node with the given value */
            break; /* break and return */
        }
        pPre = pDel;
        pDel = pDel->_pNext;
    }
}

void LinkedList::traverse_and_print()
{
    Node *p = _pHead;
    
    /* The list is empty? */
    if (_pHead == NULL) {
        cout << "The list is empty" << endl;
        return;
    }
    
     cout << "LinkedList: ";
    /* A basic way of traversing a linked list */
    while (p != NULL) { /* while there are some more nodes left */
        /* output the value */
        cout << p->_value << " ";
        /* The pointer moves along to the next one */
        p = p->_pNext;
    }
    cout << endl;
}

void LinkedList::print(string line, int ID)
{
	
		ID = 5;
	cout <<"Line " << line << "ID: "<< ID << "\n";
	
	
}

int main(int argc, const char * argv[])
{
	
	
	string line;
	int ID;
   
    LinkedList list(line, ID);
    
    
    

    
    
      
  ifstream myfile("gd.txt");
         
  for(int ID=1;true;ID++)
  {

  	if(!myfile.eof()){          
    getline(myfile,line,'\n');
    cout<<ID<<". "<<line<<"\n";
    list.tailAppend(line, ID);
    }
  	else{
	  break;
	}
	}
	ID = 9;
	cout << ID << line; // It outputs 9Line32
		list.print(line, ID); // It outputs 5Line32, atleast not 9
		
		 // I want as output 9Line9 when I print it.
    


	
	
      
	
    

    
    cout << "Before removing: " << endl; 
    /* output the result */
    list.traverse_and_print();
    
    /* Remode the node with value 3 */
    cout << "Trying to remove the node with value "<< line << endl;
    list.remove(line, 3);
    /* output the result */
    cout << "After removing: " << endl;
    list.traverse_and_print();

    return 0;
}



Last edited on
Topic archived. No new replies allowed.