Linked lists omissions by author, such as not defining NULL

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 4169 Views - Last Post: 10 March 2012 - 11:24 PM Rate Topic: -----

#16 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked lists omissions by author, such as not defining NULL

Posted 08 March 2012 - 08:12 PM

You've lost me now... Have you solved all the problems in LinkedList.h and are now going on to something else, or are you complicating matters by adding another (probably error-filled) class on top of one that definitely contains errors?

You really should compile LinkedList.h by itself to be sure that you've at least corrected all of its syntax errors before trying to use it together with other (unproven) code.

I think I remember you saying that you're using g++. If so, you can call it with
g++ -c LinkedList.h

The -c option means it will just compile that code and not try to link it, so it will work even though there's no main function.

Alternatively, of course, you can write a trivial main function that does nothing but instantiate a LinkedList, and compile in the normal manner.
Was This Post Helpful? 0
  • +
  • -

#17 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Linked lists omissions by author, such as not defining NULL

Posted 08 March 2012 - 08:22 PM

I'm using Visual C++ 2010. Can't say i've heard of g++ in relation to C++.

So basically it seems there might be a problem in LinkedList.h due to num not being declared in the function bdoy definitions.

This post has been edited by mgrex: 08 March 2012 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#18 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked lists omissions by author, such as not defining NULL

Posted 08 March 2012 - 08:30 PM

I guess I was thinking of someone else. g++ is the C++ compiler command for the Gnu Compiler Collection (GCC). That's the compiler most commonly used on Linux systems. It's also widely used on Windows systems in the form called minGW ("minimalist Gnu for Windows").

Anyway, I know there's a way to compile on the command line with Visual C++ but I don't know how offhand. But you can easily put together a simple project containing only your LinkedList.h and a main.cpp file that contains just a very minimal main function that just declares a linked list of any type and returns. That will let you work out the problems in the LinkedList template without any additional complications.

This post has been edited by r.stiltskin: 08 March 2012 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#19 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Linked lists omissions by author, such as not defining NULL

Posted 08 March 2012 - 08:56 PM

View Postmgrex, on 08 March 2012 - 10:22 PM, said:

I'm using Visual C++ 2010. Can't say i've heard of g++ in relation to C++.

So basically it seems there might be a problem in LinkedList.h due to num not being declared in the function bdoy definitions.


You may need to check that the .h file for feet and inches is available to your project. You gave/showed us the .cpp file ... but not the .h file. ( What you labelled above as the .h file for feet and inches ... was actually the .h file for your SLL class :)

And ... as already suggested above, you need to make sure, at FIRST, that your LL class is working, before you use it to test out a new class ... like your feet/inches class ... using it. ( If you already knew that your feet-inches class was working well, using it then, with your LL class ... this could be a further test of your LL class. )

An assured way to check out that new feet/inches class would be to use it with the STL list class ... which we already know ... works well :)

This post has been edited by David W: 08 March 2012 - 09:31 PM

Was This Post Helpful? 1
  • +
  • -

#20 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,212
  • Joined: 23-August 08

Re: Linked lists omissions by author, such as not defining NULL

Posted 09 March 2012 - 04:37 AM

Can you give me an example page with the typos? I've got to see how bad this is for myself.

Never mind, I found them. I'm sorry I doubted you. He's definitely mixing up his classes (LinkedList and NumberList) in section 17.3.

I looked for Errata online and failed to find any. You should complain to the author and the book publisher.

Like I said above, however, the code on the FTP site looks OK, so I would suggest perhaps grabbing the code from there rather than re-typing by hand.

Code from the book (sorry about the line numbers):
// A class template for holding a linked list.
2 #ifndef LINKEDLIST_H
3 #define LINKEDLIST_H
4 #include <iostream>
5 using namespace std;
6
7 template <class T>
8 class LinkedList
9{
10 private:
11    // Declare a structure for the list.
12    struct ListNode
13    {
14
15
16
17
18
19
20 public:
21    // Constructor
22    NumberList()
23       { head = NULL; }
24
25    // Destructor
26    ~NumberList();
27
// For cout and NULL
T value;                // The value in this node
   struct ListNode *next;  // To point to the next node
};
ListNode *head;
// List head pointer
28    // Linked list operations
29    void appendNode(T);
30    void insertNode(T);
31    void deleteNode(T);
32    void displayList() const;
33 }; 34
35
36 //**************************************************
37 // appendNode appends a node containing the value  *
38 // passed into newValue, to the end of the list.   *
39 //**************************************************
40
41 template <class T>
42 void LinkedList<T>::appendNode(T newValue)
43 {
44 ListNode *newNode; // To point to a new node
45    ListNode *nodePtr;  // To move through the list
46
47    // Allocate a new node and store num there.
48    newNode = new ListNode;
49    newNode->value = num;
50    newNode->next = NULL;
51
52    // If there are no nodes in the list
53    // make newNode the first node.
54 if (!head)
55       head = newNode;
56    else  // Otherwise, insert newNode at end.
57 {
58       // Initialize nodePtr to head of list.
59       nodePtr = head;
60
61       // Find the last node in the list.
62       while (nodePtr->next)
63          nodePtr = nodePtr->next;
64
65       // Insert newNode as the last node.
66       nodePtr->next = newNode;
67    }
68 }
69
70 //**************************************************
71 // displayList shows the value                     *
72 // stored in each node of the linked list          *
73 // pointed to by head.                             *
74 //**************************************************
75
76 template <class T>
77 void LinkedList<T>::displayList()
78 {
79    ListNode *nodePtr;  // To move through the list
80
81    // Position nodePtr at the head of the list.
82    nodePtr = head;
17.3A Linked List Template 1023
1024 Chapter 1L7inked Lists
83
84    // While nodePtr points to a node, traverse
85    // the list.
86    while (nodePtr)
87 {
88       // Display the value in this node.
89       cout << nodePtr->value << endl;
90
91       // Move to the next node.
92       nodePtr = nodePtr->next;
93    }
94 }
95
96 //**************************************************
97 // The insertNode function inserts a node with     *
98 // newValue copied to its value member.            *
99 //**************************************************
100
101 template <class T>
102 void LinkedList<T>::insertNode(T newValue)
103 {
104    ListNode *newNode;             // A new node
105    ListNode *nodePtr;             // To traverse the list
106    ListNode *previousNode = NULL; // The previous node
107
108    // Allocate a new node and store num there.
109    newNode = new ListNode;
110    newNode->value = num;
111
112    // If there are no nodes in the list
113    // make newNode the first node.
114    if (!head)
115 {
116       head = newNode;
117       newNode->next = NULL;
118 }
119    else  // Otherwise, insert newNode.
120 {
121
122
123
124
125
126
127
128
129
130
131
132
133
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to NULL.
previousNode = NULL;
// Skip all nodes whose value is less than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
   nodePtr = nodePtr->next;
}
134       // If the new node is to be the 1st in the list,
135       // insert it before all other nodes.
136       if (previousNode == NULL)
137 {
138          head = newNode;
139          newNode->next = nodePtr;
140 }
141       else  // Otherwise insert after the previous node.
142 {
143          previousNode->next = newNode;
144          newNode->next = nodePtr;
145 }
146 }
147 }
148
149 //*****************************************************
150 // The deleteNode function searches for a node        *
151 // with searchValue as its value. The node, if found, *
152 // is deleted from the list and from memory.          *
153 //*****************************************************
154
155 template <class T>
156 void LinkedList<T>::deleteNode(T searchValue)
157 {
158    ListNode *nodePtr;       // To traverse the list
159    ListNode *previousNode;  // To point to the previous node
160
161    // If the list is empty, do nothing.
162    if (!head)
163 return;
164
165    // Determine if the first node is the one.
166    if (head->value == num)
167 {
168       nodePtr = head->next;
169       delete head;
170       head = nodePtr;
171 }
172 else
173 {
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Initialize nodePtr to head of list.
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
   previousNode = nodePtr;
   nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
17.3A Linked List Template 1025

1026 Chapter 1L7inked Lists
   188
   189
   190
   191
   192
   193
   194 }
   195
   196 //**************************************************
   197 // Destructor                                      *
   198 // This function deletes every node in the list.   *
   199 //**************************************************
   200
   201 template <class T>
   202 LinkedList<T>::~LinkedList()
   203 {
   204    ListNode *nodePtr;   // To traverse the list
   205    ListNode *nextNode;  // To point to the next node
206
   207    // Position nodePtr at the head of the list.
   208    nodePtr = head;
209
   210    // While nodePtr is not at the end of the list...
   211    while (nodePtr != NULL)
212 {
   213       // Save a pointer to the next node.
   214       nextNode = nodePtr->next;
215
   216       // Delete the current node.
   217       delete nodePtr;
218
   219       // Position nodePtr at the next node.
   220       nodePtr = nextNode;
    }
}
#endif


Code from the FTP site:
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>     // For cout and NULL
using namespace std;

template <class T>
class LinkedList
{
private:
   // Declare a structure for the list
   struct ListNode
   {
      T value;                // The value in this node
      struct ListNode *next;  // To point to the next node
   }; 

   ListNode *head;   // List head pointer

public:
   // Constructor
   LinkedList()
      { head = NULL; }
      
   // Destructor
   ~LinkedList();
      
   // Linked list operations
   void appendNode(T);
   void insertNode(T);
   void deleteNode(T);
   void displayList() const;
};


//**************************************************
// appendNode appends a node containing the value  *
// pased into newValue, to the end of the list.    *
//**************************************************

template <class T>
void LinkedList<T>::appendNode(T newValue)
{
   ListNode *newNode;  // To point to a new node
   ListNode *nodePtr;  // To move through the list

   // Allocate a new node and store newValue there.
   newNode = new ListNode;
   newNode->value = newValue;
   newNode->next = NULL;

   // If there are no nodes in the list
   // make newNode the first node.
   if (!head)
      head = newNode;
   else  // Otherwise, insert newNode at end.
   {
      // Initialize nodePtr to head of list.
      nodePtr = head;

      // Find the last node in the list.
      while (nodePtr->next)
         nodePtr = nodePtr->next;

      // Insert newNode as the last node.
      nodePtr->next = newNode;
   }
}

//**************************************************
// displayList shows the value                     *
// stored in each node of the linked list          *
// pointed to by head.                             *
//**************************************************

template <class T>
void LinkedList<T>::displayList() const
{
   ListNode *nodePtr;  // To move through the list

   // Position nodePtr at the head of the list.
   nodePtr = head;

   // While nodePtr points to a node, traverse
   // the list.
   while (nodePtr)
   {
      // Display the value in this node.
      cout << nodePtr->value << endl;

      // Move to the next node.
      nodePtr = nodePtr->next;
   }
}

//**************************************************
// The insertNode function inserts a node with     *
// newValue copied to its value member.            *
//**************************************************

template <class T>
void LinkedList<T>::insertNode(T newValue)
{
   ListNode *newNode;             // A new node
   ListNode *nodePtr;             // To traverse the list
   ListNode *previousNode = NULL; // The previous node

   // Allocate a new node and store newValue there.
   newNode = new ListNode;
   newNode->value = newValue;
   
   // If there are no nodes in the list
   // make newNode the first node
   if (!head)
   {
      head = newNode;
      newNode->next = NULL;
   }
   else  // Otherwise, insert newNode
   {
      // Position nodePtr at the head of list.
      nodePtr = head;

      // Initialize previousNode to NULL.
      previousNode = NULL;

      // Skip all nodes whose value is less than newValue.
      while (nodePtr != NULL && nodePtr->value < newValue)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If the new node is to be the 1st in the list,
      // insert it before all other nodes.
      if (previousNode == NULL)
      {
         head = newNode;
         newNode->next = nodePtr;
      }
      else  // Otherwise insert after the previous node.
      {
         previousNode->next = newNode;
         newNode->next = nodePtr;
      }
   }
}

//*****************************************************
// The deleteNode function searches for a node        *
// with searchValue as its value. The node, if found, *
// is deleted from the list and from memory.          *
//*****************************************************

template <class T>
void LinkedList<T>::deleteNode(T searchValue)
{
   ListNode *nodePtr;       // To traverse the list
   ListNode *previousNode;  // To point to the previous node

   // If the list is empty, do nothing.
   if (!head)
      return;
   
   // Determine if the first node is the one.
   if (head->value == searchValue)
   {
      nodePtr = head->next;
      delete head;
      head = nodePtr;
   }
   else
   {
      // Initialize nodePtr to head of list
      nodePtr = head;

      // Skip all nodes whose value member is 
      // not equal to num.
      while (nodePtr != NULL && nodePtr->value != searchValue)
      {  
         previousNode = nodePtr;
         nodePtr = nodePtr->next;
      }

      // If nodePtr is not at the end of the list, 
      // link the previous node to the node after
      // nodePtr, then delete nodePtr.
      if (nodePtr)
      {
         previousNode->next = nodePtr->next;
         delete nodePtr;
      }
   }
}

//**************************************************
// Destructor                                      *
// This function deletes every node in the list.   *
//**************************************************

template <class T>
LinkedList<T>::~LinkedList()
{
   ListNode *nodePtr;   // To traverse the list
   ListNode *nextNode;  // To point to the next node

   // Position nodePtr at the head of the list.
   nodePtr = head;

   // While nodePtr is not at the end of the list...
   while (nodePtr != NULL)
   {
      // Save a pointer to the next node.
      nextNode = nodePtr->next;

      // Delete the current node.
      delete nodePtr;

      // Position nodePtr at the next node.
      nodePtr = nextNode;
   }
}
#endif


Perhaps the FTP site is meant to serve as code errata?
Was This Post Helpful? 1
  • +
  • -

#21 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Linked lists omissions by author, such as not defining NULL

Posted 09 March 2012 - 05:43 PM

Yeah, I just looked,and compared my code of LinkedList.h, with that of the "Code from the FTP site", and things certainly don't match up. As noted in my last post, the compiler stated it couldn't find the identifier 'num'.
Rather than the following stated by the author:
	newNode = new ListNode; 
	newNode->value = num; 
	newNode->next = NULL; 


Your post lists
   newNode = new ListNode;
   newNode->value = newValue;
   newNode->next = NULL;


It seems newValue, is correct because the functon argument of appendNode states:
void LinkedList<T>::appendNode(T newValue) 


After the making the necessary/similar changes in other functions as well, the program was able to execute.

This post has been edited by mgrex: 09 March 2012 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#22 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 07:08 PM

I'm almost through the succeeding chapter, and I've come across an even worse problem.

Program 18-06 is supposed to be a demonstration for Dynamic Queues utilizing a linked list.

Instead of demonstrating this, the author/editor uses Dynamic Stack's Program 18-03) implementation file (.cpp) completely, i.e. totally worthless. Thankfully I came across another text book with similar code, but unfortunately it's only ~85% similar to the programming example.

I have marked the error specifically as comments. Lines 31 & 36 really has me stuck, stating: no suitable constructor exists to convert from int to DynIntQueue::QueueNode

I also added the function body, isFull, based on a previous exercise, since the text book I had to obtain the code from, only had isEmpty, instead of including isFull. Only 75 pages away from being done with this book.

DynIntQueue.cpp
#include <iostream> 
#include "DynIntQueue.h" 
using namespace std; 

//************************
// Constructor.          
//************************
DynIntQueue::DynIntQueue()
{
	front = NULL;
	rear = NULL;   
}

//*****************************************************
// Destructor 
// This function deletes every node in the list.
//*****************************************************
DynIntQueue::~DynIntQueue()
{
	clear();
}

//*****************************************************
// Function enqueue inserts the value in num 
// at the rear of the queue.                 
//*****************************************************
void DynIntQueue::enqueue(int num) 
{ 
	if (isEmpty())
	{
		front = new QueueNode(num);	//ERROR: no suitable constructor exists to convert from int to DynIntQueue::QueueNode
		rear = front;
	}
	else
	{
		rear->next = new QueueNode(num);  //ERROR: no suitable constructor exists to convert from int to DynIntQueue::QueueNode
		rear = rear->next;
	}    
}

//**********************************************
// Function dequeue removes the value at the   
// front of the queue, and copies it into num. 
//**********************************************
void DynIntQueue::dequeue(int &num)
{
	QueueNode *temp;
	if (isEmpty())
	{
		cout << "The queue is empty.\n";
		exit(1);
	}
	else
	{
		num = front->value;
		temp = front;
		front = front->next;
		delete temp;      
	}
}

//*********************************************
// Function isEmpty returns true if the queue 
// is empty, and false otherwise.             
//*********************************************
bool DynIntQueue::isEmpty() const
{
	if (front == NULL)
		return true;
	else 
		return false;
}

//*****************************************************************
// isFull returns true if the queue is full, otherwise false. 
//*****************************************************************
bool DynIntQueue::isFull() const //The other text didn't have this functino, so I had derive it from the previouse program (18-03)
{								
	bool status; 
	
	if (numItems < value) 
		status = false;
	else 
		status = true; 
	
	return status; 
}

//********************************************
// Function clear dequeues all the elements  
// in the queue.                             
//********************************************
void DynIntQueue::clear()
{
	int value;			// Dummy variable for dequeue
	while(!isEmpty())
	dequeue(value);
}


DynIntQueue.h
#ifndef DYNINTQUEUE_H 
#define DYNINTQUEUE_H 

class DynIntQueue 
{ 
private: 
	// Structure for the queue nodes 
	struct QueueNode 
	{ 
		int value;			// Value in a node 
		QueueNode *next;	// Pointer to the next node 
	}; 
	QueueNode *front;	// Tne front of tne queue 
	QueueNode *rear;	// The rear of the queue 
	int numItems;		// Number of items in the queue 

public: 
	// Constructor 
	DynIntQueue(); 

	// Destructor 
	~DynIntQueue(); 

	// Queue operations 
	void enqueue(int); 
	void dequeue(int &); 
	bool isEmpty() const; 
	bool isFull() const; 
	void clear(); 
}; 
#endif 


Pr18-06
// This program demonstrates the DynlntQueue class . 
#include <iostream> 
#include "DynIntQueue.h" 

using namespace std;
int main() 
{ 
	const int MAX_VALUES = 5; 
 
	// Create a DynlntQueue object. 
	DynIntQueue iQueue; 

	// Enqueue a series of numbers. 
	cout << "Enqueuing " << MAX_VALUES << " items...\n"; 
	for (int x = 0; x < 5; x++) 
	iQueue.enqueue(x); 
	
	// Dequeue and retrieve all numbers in the queue 
	cout << "The values in the queue were:\n"; 
	while (!iQueue.isEmpty()) 
	{ 
		int value; 
		iQueue.dequeue(value); 
		cout << value << endl; 
	} 

	system("pause");
	return 0; 
}

This post has been edited by mgrex: 10 March 2012 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#23 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 07:29 PM

Let's take a new approach. Don't get me wrong -- my intention is not to be obnoxious, but I think you need some prodding.

So, what do you think this error message
ERROR: no suitable constructor exists to convert from int to DynIntQueue::QueueNode
means, given that it refers explicitly to this
front = new QueueNode(num);

line of code?

Specifically, what do you see in that line of code?
Was This Post Helpful? 0
  • +
  • -

#24 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 07:46 PM

View Postr.stiltskin, on 10 March 2012 - 09:29 PM, said:

Let's take a new approach. Don't get me wrong -- my intention is not to be obnoxious, but I think you need some prodding.

So, what do you think this error message
ERROR: no suitable constructor exists to convert from int to DynIntQueue::QueueNode
means, given that it refers explicitly to this
front = new QueueNode(num);

line of code?

Specifically, what do you see in that line of code?


Further hint:
What kind of ctor is needed/called by each of the following:
SomeClass obj1;
SomeClass obj2( someVal );

And if one of those kinds of ctors is missing in your class, you may by now, be able to add the code for it?

Once you 'see' the error messages as 'your' friend ... you have graduated from Programming 101

Don't despair, that transition seems close at hand for you :)

This post has been edited by David W: 10 March 2012 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

#25 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 08:31 PM

Also, you may wish to note a logic error in one of your posts above (re. your feet and inches) ...

bool status;

status = !( a && b );

// equivalently coded as ...

status = !a || !b;

This post has been edited by David W: 10 March 2012 - 08:32 PM

Was This Post Helpful? -1
  • +
  • -

#26 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 08:52 PM

@mgrex: Are you there? Are you thinking about my question? Are you completely baffled? Have you lost interest?

I was hoping for a discussion, as opposed to us just telling you the answer.
Was This Post Helpful? 0
  • +
  • -

#27 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 09:02 PM

By the sound of it, it seems you're both talking about a default constructor?

I decided to take an alternate approach to that function by looking at the author's programming example from a previous chapter (17-03). I was able to get better luck. The program compiled and displayed the desired results.
But a break error immediately follows. It seems this may be caused by a lack of proper isFull function, which is enclosed with the comments syntax (along with the one specified in the header), since I don't know the syntax for it.

I've also included an image of the break error message.
Attached Image
I also got the break error message from a previous program (18-05), which executed successfully, but generated the break error when I attempted to exit (either by clicking on x, or pressing 'any key'). With this current program (18-06), the break error generates immediately after generating the results without me pressing any key during the runtime.

I have also included an image of the error message as well as all the code of Program 18-05 (at the very bottom of this post).

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

//************************
// Constructor.          
//************************
DynIntQueue::DynIntQueue()
{
	front = NULL;
	rear = NULL;   
}

//*****************************************************
// Destructor 
// This function deletes every node in the list.
//*****************************************************
DynIntQueue::~DynIntQueue()
{
	clear();
}

//*****************************************************
// Function enqueue inserts the value in num 
// at the rear of the queue.                 
//*****************************************************
void DynIntQueue::enqueue(int num) 
{ 
	QueueNode *newNode;
	
	newNode = new QueueNode;
	newNode->value = num;

	if (isEmpty())
	{
		front = newNode;	
		rear = front;
	}
	else
	{
		rear->next = newNode; 
		rear = rear->next;
	}    
}

//**********************************************
// Function dequeue removes the value at the   
// front of the queue, and copies it into num. 
//**********************************************
void DynIntQueue::dequeue(int &num)
{
	QueueNode *temp;
	if (isEmpty())
	{
		cout << "The queue is empty.\n";
		exit(1);
	}
	else
	{
		num = front->value;
		temp = front;
		front = front->next;
		delete temp;      
	}
}

//*********************************************
// Function isEmpty returns true if the queue 
// is empty, and false otherwise.             
//*********************************************
bool DynIntQueue::isEmpty() const
{
	if (front == NULL)
		return true;
	else 
		return false;
}

//*****************************************************************
// isFull returns true if the queue is full, otherwise false. 
//*****************************************************************
/*bool DynIntQueue::isFull() const //The other text didn't have this functino, so I had derive it from the previouse program (18-03)
{								
	bool status; 
	
	if (numItems < value) 
		status = false;
	else 
		status = true; 
	
	return status; 
	if (front == numItems)
		return true;
	else 
		return false;
}*/

//********************************************
// Function clear dequeues all the elements  
// in the queue.                             
//********************************************
void DynIntQueue::clear()
{
	int value;			// Dummy variable for dequeue
	while(!isEmpty())
	dequeue(value);
}


//Program 18-05
Attached Image

Pr18-05.cpp
// This program demonstrates the IntQueue class. 
#include <iostream> 
#include "IntQueue.h" 
using namespace std; 

int main() 
{ 
	const int MAX_VALUES = 5;		// Max number of values 
 
	// Create an IntQueue to hold the values. 
	IntQueue iQueue(MAX_VALUES); 

	// Enqueue a series of items. 
	cout<< "Enqueuing " << MAX_VALUES << " items...\n"; 
	
	for (int x = 0; x < MAX_VALUES; x++) 
		iQueue.enqueue(x); 
	
	// Attempt to enqueue just one more item. 
	cout << "Now attempting to enqueue again...\n"; 
	iQueue.enqueue(MAX_VALUES); 
	
	// Dequeue and retrieve all items in the queue 
	cout << "The values in the queue were:\n"; 
	while (!iQueue.isEmpty()) 
	{ 
		int value; 
		iQueue.dequeue(value); 
		cout << value << endl; 
	} 

	system("pause");
	return 0; 
} 


IntQueue.h
// Specification file for the IntQueue class 
#ifndef INTQUEUE_H 
#define INTQUEUE_H 

class IntQueue 
{
private: 
	int *queueArray;	// The queue size 
	int queueSize;		// Points to the queue array  
	int front;			// Subscript of the queue front 
	int rear;			// Subscript of the queue rear 
	int numItems;		// Number of items in the queue 

public: 
	// Constructor 
	IntQueue(int); 

	// Copy constructor 
	IntQueue(const IntQueue &); 
	
	// Destructor 
	~IntQueue();

	// Queue operations 
	void enqueue(int); 
	void dequeue(int &); 
	bool isEmpty() const; 
	bool isFull() const; 
	void clear(); 
}; 
#endif 




IntQueue.cpp
// Implementation file for the IntQueue class 
#include <iostream> 
#include "IntQueue.h" 
using namespace std;

//*****************************************************************
// This constructor creates an empty queue of a specified size.
//*****************************************************************
IntQueue::IntQueue(int s) 
{ 
	queueArray = new int(s); 
	queueSize = s; 
	front = -1, 
	rear = -1; 
	numItems = 0; 
}

//*******************************
// Copy constructor
//*******************************
IntQueue::IntQueue(const IntQueue &obj) 
{ 
	// Allocate the queue array. 
	queueArray = new int[obj.queueSize]; 
	
	// Copy the other object's attributes. 
	queueSize = obj.queueSize; 
	front = obj.front; 
	rear = obj.rear; 
	numItems = obj.numItems;

	// Copy the other object's queue array. 
	for (int count = 0; count < obj . queueSize; count++) 
		queueArray[count] = obj.queueArray[count];
} 

//*******************************
// Destructor
//*******************************
IntQueue::~IntQueue() 
{ 
	delete [] queueArray; 
}

//*****************************************************************
// Function enqueue inserts a value at the rear of the queue.
//*****************************************************************
void IntQueue::enqueue(int num) 
{ 
	if (isFull()) 
	cout << "The queue is full.\n"; 
	else 
	{ 
		// Calculate the new rear position 
		rear = (rear + 1) % queueSize; 
		
		// Insert new item 
		queueArray[rear] = num; 
		
		// Update item count 
		numItems++; 
	}
}

//*****************************************************************
// Function dequeue removes the value at the front of the queue 
// and copies t into num. 
//*****************************************************************
void IntQueue::dequeue(int &num) 
{ 
	if (isEmpty()) 
		cout << "The queue is empty.\n"; 
	else 
	{ 
		// Move front 
		front = (front + 1) % queueSize; 
		
		// Retrieve the front item 
		num = queueArray[front]; 
		// update item count 
		numItems--; 
	}
}

//*****************************************************************
// isEmpty returns true if the queue is empty, otherwisel false.
//*****************************************************************
bool IntQueue::isEmpty() const 
{ 
	bool status; 
	
	if (numItems) 
		status = false; 
	else 
		status = true; 
	
	return status; 
}

//*****************************************************************
// isFull returns true if the queue is full, otherwise false. 
//*****************************************************************
bool IntQueue::isFull() const 
{ 
	bool status; 
	
	if (numItems < queueSize) 
		status = false;
	else 
		status = true; 
	
	return status; 
}

//*****************************************************************
// clear sets the front and rear indices, and sets numItems to 0. 
//*****************************************************************
void IntQueue::clear()
{
	front = queueSize - 1; 
	rear = queueSize - 1; 
	numItems = 0; 
} 


This may be due to a corruption of the heap, which indicates a bug in 18_05.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while 18_05.exe has focus.

The output window may have more diagnostic information.
HEAP[18_05.exe]: Invalid address specified to RtlValidateHeap( 00160000, 001647A0 )
Windows has triggered a breakpoint in 18_05.exe.

This may be due to a corruption of the heap, which indicates a bug in 18_05.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while 18_05.exe has focus.

The output window may have more diagnostic information.
The thread 'Win32 Thread' (0x90c) has exited with code -1073741510 (0xc000013a).
The program '[5084] 18_05.exe: Native' has exited with code -1073741510 (0xc000013a).


This post has been edited by mgrex: 10 March 2012 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#28 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 09:05 PM

Wouldn't you like to understand the (very minor) problems in the code you posted before, rather than simply abandoning it in hopes of "better luck"?
Was This Post Helpful? 1
  • +
  • -

#29 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 09:35 PM

View Postr.stiltskin, on 10 March 2012 - 09:05 PM, said:

Wouldn't you like to understand the (very minor) problems in the code you posted before, rather than simply abandoning it in hopes of "better luck"?


In the "other textbook" where I derived the .cpp code from, the header file used a 'friend' keyword declaration, for the similar program which seemed to share 85% in common with the one i'm working on. Also sub class (class within the private) was used rather than a struct, for the linked list initializations.

This post has been edited by mgrex: 10 March 2012 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#30 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: Linked lists omissions by author, such as not defining NULL

Posted 10 March 2012 - 10:06 PM

View Postmgrex, on 10 March 2012 - 11:35 PM, said:

View Postr.stiltskin, on 10 March 2012 - 09:05 PM, said:

Wouldn't you like to understand the (very minor) problems in the code you posted before, rather than simply abandoning it in hopes of "better luck"?


In the "other textbook" where I derived the .cpp code from, the header file used a 'friend' keyword declaration, for the similar program which seemed to share 85% in common with the one i'm working on. Also sub class (class within the private) was used rather than a struct, for the linked list initializations.


I think you mean 'nested' class (es) ... That can be quite a step? It may be best to just keep using a nested struct for now (same thing as class but default is public and not default as private in a class)

Look up constructors and study/try out several examples.
class Vec3d
{
   // friend ... <<  // to be coded //
public:
   // what kind of ctor's are these below ?
   Vec3d() : x(0), y(0), z(0) {}
   Vec3d( int ix, int iy, int iz ) : x(ix), y(iy), z(iz) {}
    // ...
private:
   int x, y, z;
} ;


This post has been edited by David W: 10 March 2012 - 10:12 PM

Was This Post Helpful? -1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3