It amazes me how many people do not realize how important dressing nice and keeping personal appearance up to par. I hope after my discussion with them that they realize that, like it or not, people judge you by what you are wearing and how good you keep yourself "Maintained"
Finished up a project in my C programming course. Have a peek. This is pretty much emphasizing structures and pointers using C. The program is a Binary Search Tree that will allow you to insert, search, and traverse the tree. If your knew to programming, take a look, put it into your compiler and check it out.
#include #include // Structure TREEtypedef struct tree{ int data; struct tree *left; struct tree *right;}TREE;// Function Prototypes and Member DeclarationsTREE* insert(TREE *cur, int num);int srcNumber(TREE *cur, int num);int pntTraversal(TREE *root);int cleanup(TREE *cur);TREE *cur = NULL;// Main Function that controls program flowint main(){ int done = 0; int number = 0; char selection; while(!done) { printf("Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: "); scanf("%c", &selection); switch(selection) { case 'i': printf("Enter a number to insert: "); scanf("%d", &number); cur = insert(cur, number); break; case 's': printf("Enter a number to search: "); scanf("%d", &number); srcNumber(cur, number); break; case 't': pntTraversal(cur); printf("\n"); break; case 'q': done = 1; break; default: break; } getchar(); } // Free Memory Before Exit cleanup(cur); // Return a successfull Launch return 0;}/************************************************************* This function will insert a number that the user inputs** @param cur - The current status of the users BST* @param num - The number to be inserted*************************************************************/TREE* insert(TREE* cur, int num){ if(cur == NULL) { cur = (TREE*)malloc(sizeof(TREE)); cur->left = NULL; cur->right = NULL; cur->data = num; return cur; } else if(num == cur->data) { return cur; } else if(num < cur->data) { cur->left = insert(cur->left, num); return cur; } else if(num > cur->data) { cur->right = insert(cur->right, num); return cur; } return 0;}/************************************************************* This function will search for a number within the BST** @param cur - The current status of the users BST* @param num - The number to be found*************************************************************/int srcNumber(TREE* cur, int num){ if(cur == NULL) { printf("%d is not in the tree\n", num); } else if(num == cur->data) { printf("%d is in the tree.\n", cur->data); return num; } else if(num < cur->data) { return srcNumber(cur->left, num); } else if(num > cur->data) { return srcNumber(cur->right, num); } return 0;}/************************************************************* This function will print the BST, in-order** @param root - The current status of the head of the BST*************************************************************/int pntTraversal(TREE* root){ if(root == NULL){ return 0; } pntTraversal(root->left); printf("%d ", *root); pntTraversal(root->right); return 0;}/************************************************************* This function will release all of the nodes within the BST** @param root - The current status of the users BST*************************************************************/int cleanup(TREE* root){ if(root == NULL) { return 0; } cleanup(root->left); cleanup(root->right); free(root); return 0;}
Compiled in Visual Studio 2005, Academic Version
Other then that, I finally got my Dell XPS which is turning out to be one hell of a great laptop. I have been wanting to get a laptop for quite some time and I finally found one that is not only stylish, but seems to hold up to my abuse. :)
Take Care Everybody
-Dave
It's a bad idea to have both a global variable and a function variable with the same name. In this case, it's your TREE* cur. (It seems that if you move the global one to be local to main, everything is fine.)
Your style is inconsistent. You put the opening brace on the same line for your TREE struct, but then have it on the next line for your functions, ifs, and whiles. You also use "TREE *cur" in one place, and "TREE* cur" in another.
Please don't think I'm bashing you. I'm just trying to be helpful, and noticing little things like this is something I'm good at.