no it head to head->next at the end of for loop block not at the beginning.
try out this
for (int t=0;t<5;t++)
cout<<"\n"<<t;
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.
>
--
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.