C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

ques and and level order traversal

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
GrispernMix
Guest





PostPosted: Sun Dec 03, 2006 3:29 am    Post subject: ques and and level order traversal Reply with quote



{ Note: this article has been multi-posted to at least [comp.lang.c++]
and [comp.lang.c++.moderated]. -mod }

//my problem is with my level order function which keeps giving me this
error Unhandled exception //at 0x00402008 in meteor.exe: 0xC0000005:
Access violation writing location 0xbaadf029. I dont //see where i am
stepping out of my boundry, the input file is
//A 8
//B 1
//C 3
//D 5
//E 12
//F 4
//G 2
//any hints would be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

#define TRUE 1
#define FALSE 0

struct info_node
{
char code[8] ;
int weight ;
int level ;
struct info_node* father ;
struct info_node* left ;
struct info_node* right ;
struct info_node* next ;

} ;

struct prqueue
{
struct info_node * prqtop ;
} ;

struct lo_queue
{
struct info_node* front ;
struct info_node* rear ;
} ;

void create(struct prqueue* pq);
int empty(struct prqueue* pq);
void insert(struct prqueue* pq, struct info_node* new_node);
struct info_node* remove(struct prqueue* pq);
int oneleft(prqueue* pque);
void visit(info_node* node);
void qInsert( lo_queue* pdq, info_node* node);
int qEmpty(lo_queue* pdq);
void qCreate(struct lo_queue* pdq);
struct info_node* remove(struct prqueue* pq);
void levelOrderTraversal(lo_queue* pdq,info_node* root, int level);
void levelorder(lo_queue* pdq,info_node* root);
void main(void)
{
FILE* infile;
struct prqueue pque ;
struct lo_queue que ;
struct prqueue* pmain_queue ;
struct info_node* pleaf_location[7] ;
struct info_node* tree_root ;
struct info_node* new_node;
struct info_node* move;
struct info_node* test;
int count=0,level=0;
int numAllow = 2*level;

create(&pque);
qCreate(&que);
new_node = NULL;
test = NULL;
infile = fopen("c:/prq_data.txt", "r");
struct info_node* p1 ; // can be used in the loop which
removes 2
nodes from
struct info_node* p2 ; // your priority queue
new_node = ( struct info_node* ) malloc( sizeof ( struct
info_node )
);
test = ( struct info_node* ) malloc( sizeof ( struct info_node
) );
//fscanf(infile,"%s %d ",new_node->code,&new_node->weight);
while(!feof(infile))
{
new_node = ( struct info_node* ) malloc( sizeof (
struct info_node )
);
fscanf(infile,"%s%d
",new_node->code,&new_node->weight);
pleaf_location[count++] = new_node;
insert(&pque,new_node);
}
move = pque.prqtop;
// remove 2
while(!oneleft(&pque))
{
new_node = ( struct info_node* ) malloc( sizeof (
struct info_node )
);// create new node
p1 = ( struct info_node* ) malloc( sizeof ( struct
info_node ) ); //
create node holder 1
p2 = ( struct info_node* ) malloc( sizeof ( struct
info_node ) );//
create node holder 2
p1 = remove(&pque);// remove first node out of pque
p2 = remove(&pque); // remove second node out of pque
strcpy(new_node->code, p1->code);// copy
new_node->code, from the
first node
strcat(new_node->code, p2->code); // copy
new_node->code, from the
second node
cout << "newNode Code" << new_node->code << endl;
new_node->weight = p1->weight + p2->weight;// add the weight
together
new_node->father = new_node; // initialize the father
cout << "left p1->code = " << p1->code << endl;
cout << "right p2->code = " << p2->code << endl;
new_node->left = p1;// itialize the left
new_node->right = p2; //intialize the right
insert(&pque,new_node);//insert it into the que
}

// the last node will be the root
move = pque.prqtop;
tree_root = remove(&pque);
//tree_root = remove(&pque);
//visit(tree_root);
levelorder(&que, tree_root);

printf("End of program: ");
getch();

} // end of function main()

void create(struct prqueue* pq)
{
pq->prqtop = NULL;
}

void qCreate(lo_queue *pdq)
{
pdq->front = NULL;
pdq->rear = NULL;
}

int empty(struct prqueue* pq)
{
if(pq->prqtop == NULL)
{
return(1);
}
else
{
return(0);
}
}

void insert(struct prqueue* pq, struct info_node* new_node)
{
struct info_node* move;
struct info_node* pre_pntr;

move = pq->prqtop;
pre_pntr = pq->prqtop;
if(empty(pq))//check for empty
{
new_node->next = NULL;
pq->prqtop = new_node;
}
else
{
while(move != NULL && new_node->weight > move->weight)
{
pre_pntr = move;
move = move->next;
}
if(move == pq->prqtop)
{
new_node->next = move;
pq->prqtop = new_node;
}
else if(move == NULL)
{
new_node->next = NULL;
pre_pntr->next = new_node;
}
else
{
new_node->next = move;
pre_pntr->next = new_node;
}
}
}

struct info_node* remove(struct prqueue* pq)
{
struct info_node* remove;
remove = NULL;
if(empty(pq))
{
printf("ERROR EMPTY!!!");
}
else if(pq->prqtop->next == NULL)
{
remove = pq->prqtop;
pq->prqtop = NULL;
}
else
{
remove = pq->prqtop;
pq->prqtop = pq->prqtop->next;
}
return(remove);
//printf("%s %d\n",remove->code,remove->weight);
}

int oneleft(prqueue *pque)
{
info_node* node;
node = remove(pque);
if(empty(pque))
{
insert(pque,node);
return(1);
}
else
{
insert(pque,node);
return(0);
}
}

void visit(info_node* node)
{
if(node != NULL)
printf("%s\n", node->code) ;
}// end of function visit()

info_node* qRemove(lo_queue* pdq)
{
info_node* auxinfo;
if(qEmpty(pdq))
{
cout << "priority que empty" << endl;
}
else if(pdq->front->next == NULL)
{
auxinfo = pdq->front;
pdq->front = NULL;
pdq->rear = NULL;
}
else
{
auxinfo = pdq->front;
pdq->front = pdq->front->next;
}
return(auxinfo);

}

void qInsert(lo_queue* pdq, info_node* node )
{
if(qEmpty(pdq))
{
pdq->front = node;
pdq->rear = node;
}
else
{
pdq->rear->next = node;
pdq->rear = node;
}
}

int qEmpty(lo_queue* pdq)
{
if(pdq->front == NULL && pdq->rear == NULL)
{
return(1);
}
else
{
return(0);
}
}

void levelorder(lo_queue* pdq,info_node* root)
{

int testCounter = 0;
if(root == NULL)
cout << " ";

else{

cout << " ";

// Insert the first node

qInsert(pdq,root);

// The level we're at in the tree

int level = 0;
//cout << "level" << level << endl;

// The number of items we can take out (Binary Tree = 2 *
level)

int numAllow = 2 * level;

while(!qEmpty(pdq)){
while(numAllow < 2*level)
{
info_node* temp = qRemove(pdq);
cout << temp->code << " ";

if(temp->left != NULL)
{

qInsert(pdq, temp->left);
} // end if()

if(temp->right != NULL)
{
qInsert(pdq, temp->right);
} // end if()

// We've output one of our allowed
elements

numAllow++;
//cout << "numallow = " << numAllow << endl;

} // end while()
//cout << "loop" << endl;
cout << "\n ";
level++;

} // while()

cout << " ";

} // end else()
} // end breadthFirstString()


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Sun Dec 03, 2006 10:28 pm    Post subject: Re: ques and and level order traversal Reply with quote



GrispernMix wrote:
Quote:
//my problem is with my level order function which keeps giving me this
error Unhandled exception //at 0x00402008 in meteor.exe: 0xC0000005:
Access violation writing location 0xbaadf029.
[...]
//any hints would be greatly appreciated
#include <stdio.h
#include <stdlib.h
#include <string.h
#include <conio.h
#include <iostream
#include <string.h
using namespace std;

<conio.h> is nonstandard, some other headers are deprecated.

Quote:
#define TRUE 1
#define FALSE 0

C++ has a boolean type, why that fuss?

Quote:
struct info_node
{
char code[8] ;
int weight ;
int level ;
struct info_node* father ;
struct info_node* left ;
struct info_node* right ;
struct info_node* next ;

} ;

- Inconsistent indentation.
- No need for the 'struct' keyword when using struct info_node. Note that
in C this would be needed.
- No documentation about intent and purpose of this type.
- Possibly dangerous use of raw pointers.

Quote:
struct prqueue
{
struct info_node * prqtop ;
} ;

struct lo_queue
{
struct info_node* front ;
struct info_node* rear ;
} ;

void create(struct prqueue* pq);
int empty(struct prqueue* pq);
void insert(struct prqueue* pq, struct info_node* new_node);
struct info_node* remove(struct prqueue* pq);
int oneleft(prqueue* pque);
[...]


This looks like a bunch of C functions that do what one would use
memberfunctions for in C++. In general, this all seems like the code was
converted from C.

Quote:
void main(void)

Okay. You didn't read or care for the FAQ. Why?

[snipped some hundred lines of code]

Some advise here:
- Never use C style casts.
- Use containers classes from the standardlibrary.
- If you assume a pointer to a function is non-null, use assert for that
and document it in the declaration of the function. Do the same for other
invariants. Alternatively, use a reference instead.
- Use new instead of malloc.
- Use a constructor that initialises pointers to zero.
- Think about whether you want to give access to the compiler-generated
copy constructors and assignment operators.
- Check returnvalues of functions that can fail.
- Use prefix increment when you don't need the postfix increment.
- Declare and initialise variables closer to their use in order to keep
their scope minimal.
- Use bool instead of int for boolean values.
- Return is not a function, no need for brackets.
- Using fscanf with %s is just asking for buffer overflows.
- If there is a violation of a function's precondition, don't print an
error and continue, rather print an error and abort.
- Create a minimal example. In your code are two parts that could fail:
file IO and the use of the container. Replace the file IO with hardcoded
values to rule out that possible point of failure.

I suggest you clean up your code and then repost it here. Otherwise, it
would be beneficial to know where exactly the code failed, i.e. get a
backtrace from the debugger. I'd also consider getting a good book on C++,
ACCU's website has a lot of book reviews.

Uli


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dariusz Bismor
Guest





PostPosted: Thu Dec 07, 2006 10:10 am    Post subject: Re: ques and and level order traversal Reply with quote



Ulrich Eckhardt wrote:

Quote:
GrispernMix wrote:
(...)
Some advise here:
- Never use C style casts.
- Use containers classes from the standardlibrary.
- If you assume a pointer to a function is non-null, use assert for that
and document it in the declaration of the function. Do the same for other

Could you explain how is it usually done? Do you mean it should be
documented in comment?

Quote:
invariants. Alternatively, use a reference instead.
- Use new instead of malloc.
- Use a constructor that initialises pointers to zero.
- Think about whether you want to give access to the compiler-generated
copy constructors and assignment operators.
- Check returnvalues of functions that can fail.
- Use prefix increment when you don't need the postfix increment.

That is surely a newbie question, but why should one use only prefix
increment. I thought one should choose one of them (if possible), the one
that suits the best, and use it consequently, with all the consequences.

Quote:
- Declare and initialise variables closer to their use in order to keep
their scope minimal.
(snipped many very usefull advices, but I have them saved)


Be glad for any share of your experience,
Dariusz
--
|\ Dariusz Bismor
/| \ Institute of Automatic Control
/ | \ mailto:Dariusz.Bismor (AT) polsl (DOT) pl
_/__|___\ http://www.ia.polsl.gliwice.pl
~~~~~~~ \________|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Kanze
Guest





PostPosted: Sat Dec 09, 2006 3:00 am    Post subject: Re: ques and and level order traversal Reply with quote

Dariusz Bismor wrote:
Quote:
Ulrich Eckhardt wrote:

[...]
Quote:
- Use prefix increment when you don't need the postfix increment.

That is surely a newbie question, but why should one use only prefix
increment.

Because if you don't, people will spend more time saying that
you should than they will at solving your real problems. In
practice, it doesn't matter two hoots.

Quote:
I thought one should choose one of them (if possible), the one
that suits the best, and use it consequently, with all the consequences.

If you are going to use the return value, you very definitely
should choose the one which returns the value you want. Using
prefix increment when you want the value before incrementation
is not going to work.

If you don't want the return value, it's best to be coherent,
and always use the same form. If you're faced with a body of
existing code, use whichever form it uses, and don't worry about
it. If you're starting new code, and have to decide, you might
as well choose the prefix form, to shut people up. (I
systematically use prefix when posting, since I don't feel like
starting tons of sterile discussion. In my own code, I adopt to
the local rules, using whichever form my collegues prefer.)

--
James Kanze (GABI Software) email:james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.