473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Queue using a linked list in c++ which stores a mway tree node

18
Below is the node for m way tree

Expand|Select|Wrap|Line Numbers
  1. class Node
  2. {
  3.     public: Node(int imway);
  4.             ~Node();
  5.             int noofKeys;
  6.             int *keys;
  7.             Node **childPointer;
  8.  
  9.     private:
  10.             int Nodemway;
  11.  
  12. };
  13.  
  14. Below is the node for the queue .
  15. struct QueueNode
  16. {
  17.             Node *storedNode;
  18.             struct QueueNode *next;
  19.             QueueNode(int mway)
  20.             {
  21.                 storedNode = new Node(mway);
  22.                 storedNode = NULL;
  23.             }
  24. };
  25.  
  26. Below is the queue class which will implement enqueue and dequeue methods which will enqueue / dequeue QueueNodes  which will have a single mway tree node stored in it.
  27. class Dq
  28. {
  29.     public:
  30.             Dq(int mway);
  31.             ~Dq();
  32.             void Enqueue(Node *root);
  33.             Node* Dequeue(void);
  34.             bool isEmpty(void);
  35.     private:
  36.             int Nodemway;
  37.             struct QueueNode *front,*rear;
  38. };
  39.  
  40. Dq::Dq(int mway)
  41. {
  42.     front = rear = NULL;
  43.     Nodemway = mway;
  44. }
  45.  
  46. Dq::~Dq()
  47. {
  48.     front = rear = NULL;
  49. }
  50.  
  51. void Dq::Enqueue(Node *root)
  52. {
  53.     struct QueueNode *ptr;
  54.  
  55.     ptr->storedNode = root;
  56. /*
  57.     ptr->next = NULL;
  58.  
  59.     if(front == NULL)
  60.     {
  61.         cout<<"NULL FRONT";
  62.         front = ptr;
  63.     }
  64.     else
  65.         rear->next = ptr;
  66.     rear = ptr;
  67.     */
  68. }
  69.  
  70. Node* Dq::Dequeue(void)
  71. {
  72.     if(!isEmpty())
  73.     {
  74.         struct QueueNode *tmp;
  75.         tmp = front;
  76.         front = front->next;
  77.         return tmp->storedNode;
  78.     }
  79.     else
  80.         return NULL;
  81. }
  82.  
  83. bool Dq::isEmpty()
  84. {
  85.     if(front == NULL)
  86.         return true;
  87.     else
  88.         return false;
  89. }
  90.  
Here I am facing problem at
ptr->storedNode = root; in Enqueue function and I am getting segmentation fault.......... Can anybody please clarify where I am going wrong??

Or is it not possible to create a queue which will store a node as a data in it.?

Other suggestions are also welcome.
Oct 24 '09 #1

✓ answered by Banfa

In Dq::Enqueue you are creating QueueNodes (Dq is the queue manager). However your current code does not create the, it uses a pointer without creating them. You should be using new to allocate QueueNodes.

If you are allocating QueueNodes with new then you need to be deallocating them with delete. This needs to be node in Dq::Dequeue and Dq::~Dq.

4 4127
gskbond
18
What I am trying to do is to have a queue implemented for level by level traverse of tree..... I want to push the node for tree on queue as data ....But I am getting error in above implementation.... please suggest where its going wrong..
Oct 24 '09 #2
Banfa
9,065 Expert Mod 8TB
Fistly

21 storedNode = new Node(mway);
22 storedNode = NULL;

is an instant memory leak every time you allocate a Node. You allocate memory and store in in a pointer the immediately clear the pointer without deallocating the memory. Line 21 is not required.

Are you aware that the whole of your Dq and QueueNode classes could be simple replaced with an STL <deque> or <list> class, for example

#include <deque>
std::deque<Node *>queue;

Hwever assuming that you are writing this for an exercise and can't use the stl then

void Dq::Enqueue(Node *root) causes a segment fault because you de-reference (use * or -> operators) ptr when it does not point anywhere. You never initialise ptr before using it. Presumable you needed to allocate memory for it.

Assuming you did allocate memory for your Dq QueueNodes then in #
# Node* Dq::Dequeue(void) you would get a memory leak because you never deallocate the memory allocated to the QueueNode you remove from the list.
Oct 24 '09 #3
gskbond
18
I am aware of stl...can not use it....have to implement my own queue....
I still did not get what exactly needs to be done and where???
Is it at destructor??
Oct 24 '09 #4
Banfa
9,065 Expert Mod 8TB
In Dq::Enqueue you are creating QueueNodes (Dq is the queue manager). However your current code does not create the, it uses a pointer without creating them. You should be using new to allocate QueueNodes.

If you are allocating QueueNodes with new then you need to be deallocating them with delete. This needs to be node in Dq::Dequeue and Dq::~Dq.
Oct 24 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
3
by: darth | last post by:
Does anyone have heap sorting with queue(doublely linked list) in C? I have heap sort with array but with queue, it's somewhat hard.
10
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
4
by: dabbakal | last post by:
Hello, am a new member and this is my first posting. Having benefited from lot of posting i decided to join. But currently am trying to solve an exercise and it is proven difficult for me. I have...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
1
by: unknowncute | last post by:
can you help me make a code about division of polynomials using linked list? thnx!
5
by: basavaraj linga | last post by:
Hi aal, I am trying to draw different shapes in BREW MP. it’s a mob OS by Qualcomm. The programming is in C. The operation is I’ve to draw different shapes on screen based on the selected button....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.