Linked List/Node ID

Hello there! I have been trying to figure this out all day. I am writing your standard linked list program, but with a twist. I want to write a program that uses two classes for my list. One is a container class that manipulates (which I have written) and the other is a class that stores information on the list.

What I'd like to do is create the information class and enable it so that I can actually assign an ID to each node in my linked list. Then when I print the list on the screen, it also displays the ID of the corresponding node.

Anyways, here is what I have so far:

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

//Queue Class
class queue
{
	public:
		queue();
		void additem();
		void removeitem();
		void print();
		void erase();
		queue *nxt;
		char name[20];
		
	private:
	queue *start_ptr;
	queue *current;
};

queue::queue()
{
		start_ptr = NULL;
}

 void queue::additem()
  {  queue *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new queue;
     cout << "Please enter the name of the person getting in line: ";
     cin >> temp->name;
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
       { start_ptr = temp;
	 current = start_ptr;
       }
     else
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }

void queue::removeitem()
   { 
	 queue *temp;
     temp = start_ptr;
     start_ptr = start_ptr->nxt;
     delete temp;
   }

void queue::erase()
{
	while (start_ptr != NULL)
	{
		queue *temp = start_ptr;
		start_ptr = start_ptr->nxt;
		delete temp;
	}
}

void queue::print()
  {  queue *temp;
     temp = start_ptr;
     cout << endl;
     if (temp == NULL)
       cout << "The line is empty!" << endl;
     else
	    cout << "This is the line!" << endl;

       { while (temp != NULL)

		{  // Display decurrents for what temp points to
              cout << "Name : " << temp->name << " ";
	      if (temp == current)
		cout << " <-- current node";
              cout << endl;
	      temp = temp->nxt;

		}

		cout << "End of the line!" << endl;
       }
  }

void main()
  {  
	 int option = 5;
	 
	 queue myqueue;
     do
	{
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "1. Have a person get in line." << endl;
	  cout << "2. Have a person leave the line." << endl;
	  cout << "3. View the line." << endl;
	  cout << "4. Have everyone leave the line." << endl;
	  cout << "5. Exit the program." << endl; 
      cout << endl << " >> ";
	  cin >> option;

	  switch (option)
	    {
	      case 1 : myqueue.additem(); break;
	      case 2 : myqueue.removeitem(); break;
		  case 3 : myqueue.print(); break;
		  case 4 : myqueue.erase(); break;
       
	    }
	}
     while (option != 5);
  }


Works like a charm. However... I have no idea on how to get the information class set up.. I am thinking it's supposed to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
class QueueItem {
    public:
        QueueItem(char *pData, int id);  // ctor
        void setNext(QueueItem *pItem);
        QueueItem* getNext();
        int getId();
        const char* getData();
    private:
        char mData[30];  // or, use a char* if you want to dynamically alloc memory 
        int mNodeID;
        QueueItem * mpNext;
    };

That is from someone elses code who did something similar to what I would like to do. I see it all infront of me and I can't put it together :(. Any help would be much appreciated. I'm kinda new at this.
Interesting...you've included both the Queue interface and the Node data structure in the same class. Most people use two separate classes, or a class and a struct, to do this...but, hey, if your way works, it works.

The QueueItem class looks like it would replace the 'node' portion of your queue class - that is, it would replace the*nxt and name aspect of the queue public portions.

My question is this: if this is C++ (and it must be, since you are using classes), why are you using character arrays (char mData[30];) instead of std::strings?
>>Works like a charm

Are you sure, what you mean?

At glance you copied the code from another novice C++ programmer (I would say a student).

What it drew my attention is where it declares and creates a "new queue" for "temp" pointer, meaning that everytime you are calling addItem() you are creating a new queue which has a starting point, end point and its own next pointer, uuuffff

You better understand the concept of creating class for such linked list and then start writing for it, I would suggest.

Normally it would be a separate class which maintains each item in the queue, meaning, an item is created and added/removed to/from the queue by another controller class.

It would be a long code writing an example for it, however you check it out in a C++ tutorial.
For a quick view, let me give you an example.

struct Node {

int data;
Struct Node *next; //also *prev if it is a doubly linked list

Node() : data(0), next(NULL) {}
Node(int x) : data(x), next(NULL) {}
};

class Queue {

public:

Queue ist() { list = NULL firstPoint = NULL; }

void addItem(int x);
void removeItem(int x);
void listItems();

private:
Node *node;
Node *firstPoint;
};

void Queue::addItem(int x)
{
if (firstPoint == NULL) // if first time called, obviously empty
{
firstPoint = new Node(x);
}
else {
//bring it to the last (last point of the queue)
list = firstPoint;
do {
node = node->next;
} while (node->next);
// now add the new item at end of queue
node->next = new Node(x);
}

}

void Queue:removeItem(int x)
{
if (firstPoint == NULL) //if list is empty
return;

if (firstPoint->data == x)
{
node = firstPoint;
firstPoint = firstPoint-Next;
delete node; //get rid of it
}

node = firstPoint;
while (node->next->data != x) { // look for the value to be removed
node = node->next; // keep checking / moving
}

if (node->next != NULL && node->next->data == x) // gotcha, the first match
{
node->next = node->next->next; // replace the next
delete node->next;
}
return;
}

void listItems()
{
node = firstItem;

while (node) {
cout << node->data << endl;
node = node->next;
}

return;
}


now you got almost the logic.



Good luck :)

Topic archived. No new replies allowed.