Thread: Help debug this code! data structures: BST + Stack

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Help debug this code! data structures: BST + Stack

    Hi everyone. I am new to this forum and would appreciate if anyone is ready to offer me some help on this one!
    The code works, but crashes when its about to print the List thats attached to each node of the tree, if you could help me that would be great!
    (i haven't yet made one function, I would if this problem is solved first)
    Thank you. =]


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct stackish{
    	char name[25];
    	struct stackish* next;
    };
    
    struct tree{
    	float gpa;
    	struct tree* left;
    	struct tree* right;
    	struct stackish* next;
    };
    
    
    struct tree* initialise(struct tree* x)
    {
    	x=(struct tree*)malloc(sizeof(struct tree));
    	x->next=NULL;
    	x->left=x->right=NULL;
    	x->gpa=2;	
    	return x;
    }
    
    void printlist(struct stackish* p)
    {
    	p=p->next;
    	while(p!=NULL)
    	{
    		puts(p->name);
    		p=p->next;
    	}
    }
    
    struct tree* entering(char c[], float a, struct tree* b)
    {
    	if(b==NULL)
    	{
    		b=(struct tree*)malloc(sizeof(struct tree));
    		b->gpa=a;
    		struct stackish* nametemp=(struct stackish*)malloc(sizeof(struct stackish));
    		strcpy(nametemp->name,c);
    		nametemp->next=b->next;
    		b->next=nametemp;
    		b->left=b->right=NULL;
    	}
    	else if(a==b->gpa)
    	{
    		struct stackish* nametemp=(struct stackish*)malloc(sizeof(struct stackish));
    		strcpy(nametemp->name,c);
    		nametemp->next=b->next;
    		b->next=nametemp;		
    	}
    	else if(a>b->gpa)
    	{
    		b->right=entering(c,a,b->right);
    	}
    	else if(a<b->gpa)
    	{
    		b->left=entering(c,a,b->left);
    	}
    	return b;
    }
    	
    
    struct tree* gpafind(float a, struct tree * q)
    {
    	if(q==NULL)
    	{
    		printf("No Record please");
    	}
    	if(a>q->gpa)
    	{
    		q->right=gpafind(a,q->right);
    	}
    	else if(a<q->gpa)
    	{
    		q->left=gpafind(a,q->left);
    	}
    	else
    	{
    		printf("GPA: %.2f\n",q->gpa);
    		printlist(q->next);
    	}
    	return q;
    }
    
    
    
    int main()
    {
    	struct tree* root=initialise(root);
    	struct tree* she=initialise(she);
    	int j;
    	char name[25];
    	float gpa,y;
    				
    	while(j!=0)
    	{
    		printf(" \tPress 1 to enter name and GPA of that person\n\tPress 2 to print GPA-wise with name(s)\n\tPress 3 to print Name(s) wise with gpa\n\t Press 0 to exit\n");
    		scanf("%d",&j);
    		
    		switch(j)
    		{
    		case 1:
    			printf("Enter Name\n");
    			gets(name);
    			gets(name);
    			printf("Enter GPA (GPA<4, hence take care not to enter higher it will not be recorded)\n");
    			scanf("%f",&gpa);
    			if(gpa>4)
    				printf("Invalid Gpa");
    			else
    				entering(name,gpa,root);
    			printf("\n\n");
    			continue;
    		case 0:
    			break;
    		case 2:
    			printf("Enter gpa to search\n");
    			scanf("%f",&y);
    			gpafind(y,root);
    			continue;
    		case 3:
    			//namefind();
    		default:
    			printf("Invalid entry\n\n");
    			break;
    		}
    		
    	}
    
    
    
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    p=p->next;
    You don't check if p is NULL before dereferencing it. That could be a problem.

    Code:
    gets(name);
    gets(name);
    Who the hell is teaching you to use gets() in this day and age?! And why are you calling it twice?

    Code:
    x=(struct tree*)malloc(sizeof(struct tree));
    You do not need to, nor should you, cast the return value of malloc() in C.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    hi, thank you for replying that quickly!

    i am using gets twice because for some reason when i use it once, it crashes =P .. it was just improvisation on my part. lol. you can check =/

    oh i have tried printing the list (attached to a node) manually and yes you're right that its value is null there - but it shouldn't be null.. i mean am i doing something wrong? according to me, it should save the string there and it should print it =/

    and about the malloc - alright i'll take care of that.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    i am using gets twice because for some reason when i use it once, it crashes =P .. it was just improvisation on my part. lol. you can check =/
    LOL, Change the code randomly and try, if it works, the last change is correct. good debugging skill!
    fflush vs gets

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    changing the code randomly and trying is what i have been doing really ... thanks though! =]

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    could anyone offer an insight?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So how do you ever add people to the tree? You've got if (b==NULL) section of your code, and it creates a new tree struct thing, but you never bother to put it in the tree (i.e., by making sure some other part of the tree links to it).

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    really?
    i think its recursively checking and adding according to the GPAs(which is later then attached to a stack, to avoid duplication)
    The tree part works btw, it traverses and prints the GPAs fine.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No, you're right, you've got that return b down there at the bottom that hooks it in. If I tried to print GPA 2.0, that's when I got a segfault since there wasn't any people there as rags_to_riches pointed out. For the rest it never did print the most recent person due to the p=p->next that automatically happens.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Spotted a few problems.
    1. variable j in main is used un-initialized.
    2. function initialise() parameter is useless. You are just using it as local variable.
    3. Read Question 4.8 and make sure you understand.
    entering() function *CANNOT* modify your 'root' variable in main. unless you assign the return value or ..read above link for other method.
    Edit: unless you're using it as dummy.
    Last edited by Bayint Naung; 12-31-2010 at 11:46 AM.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Good lord, no one's updated that FAQ entry to remove the K&R style function implementation???

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why is your gpafind function trying to modify the tree? E.g.
    Code:
    		q->right=gpafind(a,q->right);
    surely you want the result of the call to just reflect the result of the subcall?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    thank you everyone!
    you guys spotted the mistakes and it helped

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    i am just a student.. so i am still learning

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by iMalc View Post
    Why is your gpafind function trying to modify the tree? E.g.
    Code:
    		q->right=gpafind(a,q->right);
    surely you want the result of the call to just reflect the result of the subcall?
    i agree, you know programming just makes you do stupid mistakes sometimes...
    and my final exam wasn't all that great =[ (that was random, i know)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-08-2010, 11:30 AM
  2. Why my code works only in debug mode?
    By pingpangpang in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 12:39 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread