problem with pointer

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 #include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *next;
};
typedef node *list;
list head, temp;
void traverse(list*);
int main()
{
    head=NULL;
    int dat;
    char ch;
    cout<<"Do you wish to enter data?"<<endl;
    cin>>ch;
    while(ch=='y'||ch=='Y')
    {
        temp=new node;
        cout<<"Enter data"<<endl;
        cin>>dat;
        temp->data=dat;
        temp->next=head;
        temp=head;
        cout<<"Do you wish to enter more data?"<<endl;
        cin>>ch;
    }
    cout<<"Traversing the linked list"<<endl;
    traverse(&head);
}

void traverse(list *temp1)
{
    temp=head;
    for(int i=1; temp!=NULL; i++)
    {
        cout<<"Data at node "<<i<<" is "<<temp->data<<endl;
        cout<<"At address "<<temp->next<<endl;
        cout<<endl;
        temp=temp->next;
    }
}
which pointer?
What problem?
Just look at all of the interactions your program has with 'head'. It is first assigned a value of NULL then it is never assigned anything again. So when you call "traverse()" 'temp' copies 'head' (which was never assigned anything but NULL) and your for loop evaluates but never executes.
Line 26.

PS. Global variables are naughty, although not a major culprit here.
Topic archived. No new replies allowed.