that's because you are creating char str[20]; on the stack so when the function createList completes all str [20] will be deleted from stack as a part of stack unwinding. and next you are calling display where all the references to name is removed and you are getting gabage value.to avoid this you need to allocate dinamic memory for str.
ouy should use char *str= (char*)malloc(20*sizeof(char))in function createList ; instead of str[20]; On Thu, Aug 19, 2010 at 10:31 PM, Raj Jagvanshi <[email protected]>wrote: > my display function print garbage value from begining > > On Thu, Aug 19, 2010 at 5:22 PM, vineeth mohan > <[email protected]>wrote: > >> void display(Node *head) >> { >> cout<<"\n"; >> for( ; head ; head=head->next) >> cout<<"\t"<<head->name; >> cout<<"\n"; >> } >> >> >> when head reaches last node >> condition head is true , then head will become head->next which is null , >> and it will try to print the name field from of a null value which is error >> >> On Wed, Aug 18, 2010 at 10:53 PM, Raj Jagvanshi <[email protected] >> > wrote: >> >>> wats d problem in my display() >>> >>> >>> >>> >>> #include<iostream> >>> #include<malloc.h> >>> #include <string.h> >>> >>> using namespace std; >>> struct node >>> { >>> char *name; >>> struct node *next; >>> }; >>> typedef struct node Node; >>> >>> void createList(Node **head ) >>> { >>> char str[20]; >>> char *p; >>> cout<<"Enter a String: " ; >>> gets (str) ; >>> p = str; >>> if((strlen(p))<2) >>> return; >>> Node *temp=*head; >>> Node *newnode=(Node*)malloc(sizeof(Node)); >>> newnode->name=p; >>> newnode->next=NULL; >>> if(!temp) >>> *head = newnode; >>> else >>> { >>> while(temp->next) >>> temp = temp->next; >>> temp->next = newnode; >>> } >>> createList(head); >>> } >>> void display(Node *head) >>> { >>> cout<<"\n"; >>> for( ; head ; head=head->next) >>> cout<<"\t"<<head->name; >>> cout<<"\n"; >>> } >>> int main() >>> { >>> Node *head=NULL; >>> while(1) >>> { >>> cout<<"\n\t\tMENU\n"; >>> cout<<"0 : To exit.\n"; >>> cout<<"1 : To create a linear link list.\n"; >>> cout<<"2 : To display the list.\n"; >>> char choice; >>> choice = getchar(); >>> getchar(); >>> if(choice=='0') >>> break; >>> switch(choice) >>> { >>> case '1': >>> createList(&head ); >>> break; >>> case '2': >>> display(head); >>> break; >>> default: >>> cout<<"Enter valid choice."; >>> } >>> } >>> system("pause"); >>> return 0; >>> } >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Algorithm Geeks" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]<algogeeks%[email protected]> >>> . >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]<algogeeks%[email protected]> >> . >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<algogeeks%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Thanks & Regards Ram Narayan Das mob: +91 9177711195 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
