Linked List Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 9
Reputation: dirnthelord is an unknown quantity at this point 
Solved Threads: 0
dirnthelord dirnthelord is offline Offline
Newbie Poster

Linked List Problem

 
0
  #1
Sep 29th, 2009
I was trying to implement a queue using linked list and the problem is when i try to call dequeue() method, nothing shows up and no errors too. just NULL values for all nodes. take a look @ this code and tell me if there is anything wrong...

ps. Do i have to call the Linked list call when implementing the Queue?
how can I access the same Queue using many windows forms?
please help me b/c i am new to this field


  1. --MQueue.cpp--
  2.  
  3. #include "stdafx.h"
  4. #include "LLQueue.h"
  5.  
  6. void LnkQueue::enqueue(int id, char *str,char *state, int priority)
  7. {//Add new node to rear
  8. LinkNode* newNode= new LinkNode(id,str,state,priority);//Create a node for new element
  9. if(isEmpty())
  10. front = newNode;//Q is empty
  11. //q.insertFront(id,str,state,priority);
  12. else
  13. rear = newNode;//Q isn't empty
  14. //q.insertBack(id,str,state,priority);
  15. }
  16.  
  17. int LnkQueue::dequeue()
  18. {//Remove a node from front
  19. if(isEmpty())
  20. {
  21. MessageBox::Show("Queue is empty","Error",MessageBoxButtons::OK,MessageBoxIcon::Error);
  22. return 0;
  23. }
  24. else
  25. {
  26. LinkNode *temp = front;
  27. front = front->next;
  28. delete temp;
  29. return 1;
  30. }
  31. }
  32.  
  33. LinkNode* LnkQueue::peekFront(){
  34. if(isEmpty())
  35. {
  36. MessageBox::Show("Queue is empty","Error",MessageBoxButtons::OK,MessageBoxIcon::Error);
  37. return NULL;
  38. }
  39. else
  40. {
  41. return front;
  42. }
  43. }
  44.  
  45. -----------------
  46. LLQueue.h
  47.  
  48. #include <iostream>
  49. #include <string.h>
  50.  
  51. using namespace std;
  52. using namespace System::Windows::Forms;
  53. using namespace System;
  54.  
  55. class LinkNode
  56. {
  57. public:
  58. int job_id;
  59. char jstr[100];
  60. int priority;
  61. char jstatus[20];
  62. LinkNode * next;//pointer to next node
  63. LinkNode * prev;//pointer to previous node
  64. LinkNode(int id, char *str,char *state, int ppriority);
  65. LinkNode();
  66. };
  67.  
  68. class LinkedList
  69. {
  70. public:
  71. LinkNode * firstNode; //pointer to front of list
  72. LinkNode * lastNode; //pointer to rear of list
  73.  
  74. LinkedList()
  75. {
  76. firstNode = NULL;
  77. lastNode = NULL;
  78. }
  79.  
  80. void insertFront(int Jid, char *str,char *state, int Jpriority);
  81. void insertBack(int Jid, char *str,char *state, int Jpriority);
  82. void insertBefore(int id, char *str,char *state, int priority, LinkNode *nodeB, LinkNode * newNode) ;
  83. void insertAfter(int id,char *str,char *state, int priority, LinkNode *nodeA, LinkNode * newNode) ;
  84.  
  85. //Removing a node is easier, only requiring care with the firstNode and lastNode:
  86. LinkNode * removeFront();
  87. LinkNode * removeBack();//FIFO Mode - no use f removing from back
  88.  
  89. LinkNode * removeNode(LinkNode *newNode);
  90.  
  91. void printDListFront();
  92. void printDListBack();
  93. };
  94.  
  95. class LnkQueue
  96. {
  97. private:
  98. LinkNode* front; //Pointer to 1st node
  99. LinkNode* rear; //Pointer to last node
  100. int nItems;
  101. public:
  102. LnkQueue(){ //Constructor
  103. front = NULL;
  104. rear = NULL;
  105. nItems = 0;
  106. }
  107.  
  108. void enqueue(int id, char *str,char *state, int priority);
  109. int dequeue();
  110. LinkNode* peekFront();
  111.  
  112. bool isEmpty(){
  113. return (front == NULL);
  114. }
  115.  
  116. ~LnkQueue()//Destructor
  117. {
  118. LinkNode * current;
  119. while(current == NULL){
  120. current = front->next;
  121. delete front;
  122. front = current;
  123. }
  124. }
  125. };
  126. ------LLQueue.cpp-----
  127. #include "stdafx.h"
  128. #include "LLQueue.h"
  129.  
  130. LinkNode::LinkNode(int id, char *str,char *state, int ppriority)
  131. {
  132. job_id = id;
  133. strcpy_s(jstr, str);
  134. strcpy_s(jstatus,state);
  135. priority = ppriority;
  136. }
  137. //insert a node before the front node
  138. void LinkedList::insertFront (int id,char *str,char *state, int priority)
  139. {
  140. LinkNode *newNode;
  141. newNode = new LinkNode(id,str,state,priority);
  142. if(this->firstNode==NULL)
  143. {
  144. this->firstNode = newNode;
  145. this->lastNode = newNode;
  146. newNode->prev = NULL;
  147. newNode->next = NULL;
  148. }
  149. else
  150. insertBefore(id, str, state, priority, this->firstNode,newNode );
  151. }
  152.  
  153. //insert a node after the last node
  154. void LinkedList::insertBack(int Jid, char *str,char *state, int Jpriority)
  155. { LinkNode *newNode;
  156. newNode = new LinkNode(Jid,str,state,Jpriority);
  157.  
  158.  
  159. if(this->lastNode==NULL)
  160. insertFront(Jid, str, state, Jpriority);
  161. else
  162. insertAfter(Jid, str, state, Jpriority, this->lastNode,newNode);
  163. }
  164.  
  165. //insert a node before nodeB
  166. void LinkedList::insertBefore(int id, char *str,char *state, int priority, LinkNode *nodeB, LinkNode * newNode)
  167. {
  168. newNode = new LinkNode(id,str,state,priority);
  169. newNode->prev=nodeB->prev;
  170. newNode->next =nodeB;
  171. if(nodeB->prev==NULL)
  172. this->firstNode =newNode;
  173. else
  174. nodeB->prev->next=newNode;
  175. nodeB->prev=newNode;
  176. }
  177. //insert a node after nodeB
  178. void LinkedList::insertAfter(int id,char *str,char *state, int priority, LinkNode *nodeA, LinkNode * newNode)
  179. {
  180. newNode = new LinkNode(id,str,state,priority);
  181. newNode->prev = nodeA;
  182. newNode->next = nodeA->next ;
  183. if(nodeA->next==NULL)
  184. this->lastNode = newNode;
  185. else
  186. nodeA->next = newNode;
  187. nodeA->next = newNode;
  188. }
  189.  
  190. //remove the front node
  191.  
  192. LinkNode * LinkedList::removeFront()
  193. {
  194. return removeNode(this->firstNode);
  195. }
  196.  
  197.  
  198. //remove a back node
  199. LinkNode * LinkedList::removeBack ()
  200. {
  201. return removeNode(this->lastNode);
  202. }
  203.  
  204. LinkNode * LinkedList::removeNode(LinkNode *nodeToRemove)
  205. {
  206. if(nodeToRemove->prev == NULL)
  207. this->firstNode = nodeToRemove->next;
  208. else
  209. nodeToRemove->prev->next = nodeToRemove->next;
  210.  
  211. if (nodeToRemove->next == NULL)
  212. this->lastNode = nodeToRemove->prev;
  213. else
  214. nodeToRemove->next->prev = nodeToRemove->prev;
  215. return nodeToRemove;
  216. delete nodeToRemove;
  217.  
  218. }
Last edited by dirnthelord; Sep 29th, 2009 at 2:59 am. Reason: CODE tag
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Linked List Problem

 
0
  #2
Sep 29th, 2009
I don't see where your enqueue function sets the next and prev members of the node.

When the queue is empty, front and rear should both be NULL.

When you add the first node, front and rear should point to the new node and its next and prev should be NULL.

When you add the second node, the next from the first node should point to the new node and the queue's rear should point to the new node. If you're going to keep the list doubly linked (with both next and prev pointers) the prev pointer for the new node should point to the previously added node.

Then when you 'dequeue' the first node, the front pointer can be set to the 'next' pointer from the node you are removing.

I'm not sure I'm being clear, but the point is that as written, your first node never gets a pointer to the next node (which is what forms the linked list). So there is no 'link' to the next node when you go to remove it.

Hope that helps some, if not, ask for more details and I'll try to explain it better.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 9
Reputation: dirnthelord is an unknown quantity at this point 
Solved Threads: 0
dirnthelord dirnthelord is offline Offline
Newbie Poster

Re: Linked List Problem

 
0
  #3
Oct 3rd, 2009
Originally Posted by Murtan View Post
I don't see where your enqueue function sets the next and prev members of the node.

When the queue is empty, front and rear should both be NULL.

When you add the first node, front and rear should point to the new node and its next and prev should be NULL.

When you add the second node, the next from the first node should point to the new node and the queue's rear should point to the new node. If you're going to keep the list doubly linked (with both next and prev pointers) the prev pointer for the new node should point to the previously added node.

Then when you 'dequeue' the first node, the front pointer can be set to the 'next' pointer from the node you are removing.

I'm not sure I'm being clear, but the point is that as written, your first node never gets a pointer to the next node (which is what forms the linked list). So there is no 'link' to the next node when you go to remove it.

Hope that helps some, if not, ask for more details and I'll try to explain it better.
I think i saw the problem ....thanks a lot Murtan.
after little thinking, I have realized that I really don't need Queue Class for the Queue Implementation, so I used both Linked List and Node classes and I now have a perfectly working Linked List Queue.

and Thanks again, I could understand your explanation. but there is a little problem.

How can I declare an Global Object in Windows Forms? (which I can access from many forms)
Reply With Quote Quick reply to this message  
Reply

Tags
forms, linkedlist

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC