well i have a problem sorting the linked list.. its changing the data instead of changing the pointers..
anything that can be optimized.. i will appreciate too ^^
well i have a problem sorting the linked list.. its changing the data instead of changing the pointers..
anything that can be optimized.. i will appreciate too ^^
post agenda.dat so i can do some tests
> struct student* temp = (struct student*)malloc(sizeof(struct student));
Casting malloc is poor style in a C program, see the FAQ
> fflush(stdin);
This is undefined behaviour - see the FAQ.
If you don't know what undefined behaviour means, then consider this.
"Your input buffer is flushed, my input buffer is untouched, his hard disk is being reformatted."
> gets((*temp).name);
Ugh, the world's most dangerous function. Never use gets()
Guess what, this is in the FAQ as well.
> char *str=(char*)malloc(50);
This is never freed, this is a memory leak.
As for sorting a linked list, one way is to create a new list from the old list, and have a 'sorted insert' function which adds a node at the correct place in the list.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
here u have the agenda.txt.. rename to agenda.dat..
about the sorting.. it cant be sorted that way..
the problem is here
wasnt better chaging the next pointer instead of chaning the data pointer?Code:*temp=curr->data; curr->data=menor->data; menor->data=*temp;
Code:typedef struct productNode * pNPtr; typedef struct productNode{ char brand[PRODUCT_BRAND_MAX + 1]; char name[PRODUCT_NAME_MAX + 1]; unsigned price; /* Stored in cents (not dollars). */ unsigned qty; pNPtr nextProduct; } ProductNodeType; typedef struct{ CashType coins[DISTINCT_CASH]; unsigned totalCoins; pNPtr headProduct; unsigned totalProducts; }Code:int addProduct(VendingMachineType* vm, char* brand, char* name, unsigned price, unsigned qty){ pNPtr cur, prev,head,newProduct; head = vm->headProduct; newProduct = malloc(sizeof(ProductNodeType)); if(newProduct==NULL){ return FAIL; }else{ cur = head; prev = NULL; /*comparison for brand where to put*/ while((cur!=NULL) && (strcmp(cur->brand, brand)<0)){ prev = cur; cur = cur->nextProduct; } /*comparison for name where to put when the brand is in order*/ while((cur!=NULL)&& (strcmp(cur->brand, brand)<=0) && (strcmp(cur->name, name)<0)){ prev = cur; cur=cur->nextProduct; } strcpy(newProduct->brand, brand); strcpy(newProduct->name, name); newProduct->price = price; newProduct->qty = qty; newProduct->nextProduct = cur; vm->totalProducts++; if(prev==NULL) vm->headProduct = newProduct; else prev->nextProduct = newProduct; } return SUCCESS; }
Then here's some really useful advice on that front:
Lookup stricmp. You will no longer need to convert string to lowercase using tolower.
Next thing. Why would your sorting algorithm ever need to malloc anything? All it has to do is swap around a few pointer values within those structs to reorder the list. 'search', 'find', 'input', and 'main' could also be done without it (perhaps statically allocate).
Dont use an "if .. else it .. else if .. else if" for checking the values of 'option'. Learn about the 'switch' statement.
Why do your functions return values if you never look at the return value?
Best not to use 'new' as a variable name. Sure that will compile okay as long as you use a .c file extension, but syntax hilighters still thinks it's a keyword, and it makes porting to C++ harder.
Your indenting is consistent, but annoying. Every curly brace should be one tab furthur to the left.
Just a style issue, but those #defines could more tidily be an enum.
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"