my display function print garbage value from begining On Thu, Aug 19, 2010 at 4:23 PM, ram das <ramnaraya...@gmail.com> wrote:
> make declaration of char str[20] to char > *str=(char*)malloc(20*sizeof(char)); > and it will work . > > On Wed, Aug 18, 2010 at 11:23 PM, Raj Jagvanshi > <raj.jagvan...@gmail.com>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 algoge...@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> >> . >> 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 algoge...@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com> > . > 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 algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.