So long story short, i am trying to make a linklist of C where i will store "quadrant" which is just a integer and i will use it as a stack storage, but for some reason it is not functioning the way i want. i want the "peeplink()" to give me an array of "quadrant", but it is giving me all 0 0 0 0 0 instead. maybe its just me staring at this thing for a little too long, i need someone to help me spot what the problem is so i can deal with this.... =.= its a crucial part of my grander code. so here it is in its gory details. thanks for all the help i can get here. i also tried to do a linklist modification by passing in a linklist head as parameter, but somehow it didn't do anything to the linklist head, if anyone can explain to me why that is, it would be of a great help.
typedef enum {NW, NE, SW, SE} quadrant;
typedef enum {LEFT, RIGHT, BOTH} direction;
typedef char tName[MAX_NAME_LEN+1];
typedef struct linkList{
struct linkList *next;
quadrant dir;
}Link;
Link *quadNodeStack; /*used for mxCifGenerator to generate id */
Link *binaryNodeStack;
static int quadLevel = 5;
int addBinaryLink(quadrant num){
struct linkList *node;
if(binaryNodeStack == NULL){
/*first node to start it all */
node = malloc(sizeof(struct linkList));
node->dir = num;
node->next = NULL;
binaryNodeStack = node;
return 1;
}else{
/*first in last out */
node = malloc(sizeof(struct linkList));
node->dir = num;
node->next = binaryNodeStack;
binaryNodeStack = node;
return 1;
}
/*failed*/
return -1;
}
int addQuadLink(quadrant num){
struct linkList *node, *curNode;
curNode = quadNodeStack;
if(quadNodeStack == NULL){
/*first node to start it all */
node = malloc(sizeof(struct linkList));
node->dir = num;
node->next = NULL;
quadNodeStack = node;
return 1;
}else{
/*first in last out */
node = malloc(sizeof(struct linkList));
node->dir = num;
node->next = quadNodeStack;
quadNodeStack = node;
return 1;
}
/*failed*/
return -1;
}
void traverseLink(struct linkList *root){
Link *temp = root;
int count =0;
while(temp != NULL){
printf("node# %d --- quadrant: %d \n", count, temp->dir );
count++;
temp = temp->next;
}
}
void clearLinkStack(struct linkList *root){
struct linkList *curNode, *temp;
curNode = root;
while( curNode != NULL){
temp = curNode;
free(temp);
curNode=curNode->next;
}
}
quadrant *peepLink(struct linkList *root){
/*quadrant length will be dependent on quadLevel */
quadrant *peepRet;
int index;
Link *curNode;
peepRet = malloc(sizeof(quadrant)*quadLevel);
curNode = root;
index=0;
printf("tell me i at least got here\n");
if(curNode == NULL)
printf("curNode is NULL\n");
while(curNode != NULL && index < quadLevel){
printf("curNodeDIR is: %d\n", curNode->dir);
peepRet[index] = curNode->dir;
curNode = curNode->next;
index++;
}
/*no node in root */
return peepRet;
}
int main(){
int index;
quadrant *quadz = peepLink(quadNodeStack);
quadrant *binz = peepLink(binaryNodeStack);
addQuadLink(3);
addQuadLink(2);
addQuadLink(1);
addQuadLink(0);
traverseLink(quadNodeStack);
for(index=0; index< quadLevel; index++){
printf(" %d ", quadz[index]);
}
printf("\n");
addBinaryLink(3);
addBinaryLink(2);
addBinaryLink(1);
addBinaryLink(0);
for(index=0; index<quadLevel; index++){
printf(" %d ", binz[index]);
}
return 0;
}
C linklist, not outputting the proper array.
Page 1 of 12 Replies - 107 Views - Last Post: 03 October 2012 - 08:52 AM
Replies To: C linklist, not outputting the proper array.
#2
Re: C linklist, not outputting the proper array.
Posted 03 October 2012 - 08:48 AM
damn!
i found the problem. super stupid me. i am such a fool. now i want to cut my wrist and bleed like a cattle. =.= ................................................................
quadrant *quadz = peepLink(quadNodeStack);
quadrant *binz = peepLink(binaryNodeStack);
addQuadLink(3);
addQuadLink(2);
addQuadLink(1);
addQuadLink(0);
traverseLink(quadNodeStack);
for(index=0; index< quadLevel; index++){
printf(" %d ", quadz[index]);
}
printf("\n");
addBinaryLink(3);
addBinaryLink(2);
addBinaryLink(1);
addBinaryLink(0);
for(index=0; index<quadLevel; index++){
printf(" %d ", binz[index]);
}
quadrant *quadz = peepLink(quadNodeStack);
quadrant *binz = peepLink(binaryNodeStack);
addQuadLink(3);
addQuadLink(2);
addQuadLink(1);
addQuadLink(0);
traverseLink(quadNodeStack);
for(index=0; index< quadLevel; index++){
printf(" %d ", quadz[index]);
}
printf("\n");
addBinaryLink(3);
addBinaryLink(2);
addBinaryLink(1);
addBinaryLink(0);
for(index=0; index<quadLevel; index++){
printf(" %d ", binz[index]);
}
#3
Re: C linklist, not outputting the proper array.
Posted 03 October 2012 - 08:52 AM
61 posts, no code tags? Ridiculous.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|