Pointer help

Queue::addItem() is linking the new item on the end ok, but isn't updating pTail to point at the new end. Can someone help with this?

1
2
3
4
5
6
7
8
9
10
11
  void Queue::addItem(char *pData)
{
	QueueItem *pItem = new QueueItem(pData, ++_itemCounter);

	if (0 == _pHead)  // check for empty queue
        _pHead = _pTail = pItem;
    else
    {
		_pTail->setNext(pItem);  //  set current last item to point to new last item.
    }
}
Since you haven't shown the code for setNext, it's impossible to say if it's doing what you expect.
This is the Queue.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include "QueueItem.h"


class Queue {
    public:
        Queue();    // ctor inits a new empty Queue
        ~Queue();   // dtor erases any remaining QueueItems
        void addItem(char *pData);
        void removeItem();
		void Queue::deleteItem();
		void print();
		void erase();

    private:
        QueueItem *_pHead; // always points to first QueueItem in the list
        QueueItem *_pTail; // always points to the last QueueItem in the list
        int _itemCounter;  // always increasing for a unique id to assign to each new QueueItem

};


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
Queue.cpp:

#include "Queue.h"
#include "QueueItem.h"
#include <iostream>
using namespace std;

void Queue::addItem(char *pData)
{
	QueueItem *pItem = new QueueItem(pData, ++_itemCounter);

	if (0 == _pHead)  // check for empty queue
        _pHead = _pTail = pItem;
    else
    {
		_pTail->setNext(pItem);  //  set current last item to point to new last item.
    }
}

void Queue::removeItem()
{
	if(_pHead == 0)
	{
		cout << "Node cannot be deleted from empty list" << endl;
	}
	else
	{
		QueueItem* curr = _pHead;
		_pHead = curr->getNext();
		delete curr;
	}
}
void Queue::print()
{
	QueueItem* temp = _pHead;
	while( temp != NULL)
	{
	cout << temp->getData() << endl;
	temp-> getNext();
	}
}

void Queue::erase()
{
	while(_pHead != 0)
		removeItem();
}

Queue::Queue(void)
{
	_pHead = NULL;
	_pTail = NULL;
}


Queue::~Queue(void)
{
	erase();
}


QueueItem.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

class QueueItem {
public:
	QueueItem(char *pData, int id); // ctor
    void setNext(QueueItem *pItem);
    QueueItem* getNext() const;
    int getId() const;
    const char* getData() const;

private:
    char _data[30];    // data value (null terminated character string)
    const int _itemId; // unique id for item in queue
    QueueItem* _pNext; // next item in queue
};


QueueItem.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
#include "QueueItem.h"
#include <cstring>


QueueItem::QueueItem(char* pData, int id): _itemId(id)
{
	strcpy_s(_data, pData);
	_pNext = NULL;

}

 void QueueItem::setNext(QueueItem *pItem)
 {
	 _pNext = pItem;
 }
    QueueItem* QueueItem::getNext() const
	{
		return _pNext;
	}
    int QueueItem::getId() const
	{
		return _itemId;
	}
    const char* QueueItem::getData() const
	{
		return _data;
	}
Try this: (I have not tested it yet)

1
2
3
4
5
6
7
8
9
10
11
12
void Queue::addItem( char *pData )
{
	QueueItem *pItem = new QueueItem(pData, ++_itemCounter);
	if ( _pHead == NULL ) {  // check for empty queue
		_pHead = pItem;
		_pHead->_pNext = _pTail;
	}
	else {
		pItem->_pNext = NULL;
		_pTail = pItem;
	}
}

Last edited on
Topic archived. No new replies allowed.