__.exe has stopped working

when i run the following code in codeblocks or devc++,a window is displayed saying __.exe has stopped working..plz tell me if there's a solution to it
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
46
47
48
49
50
51
#include <iostream>
using namespace std;
struct node
{
    int data;
    struct node *link;
};
typedef node *list;
void showList(list);

int main()
{
    list head,tail,temp;
    char ch;
    int dat;
    head=tail=NULL;
    cout<<"Enter Data?(y/n)"<<endl;
    cin>>ch;
    if(ch=='y' || ch=='Y')
    {
        tail=new node;
        cout<<"give data"<<endl;
        cin>>dat;
        tail->data=dat;
        tail->link=NULL;
        head=temp;
        cout<<"Enter more Data?(y/n)"<<endl;
        cin>>ch;
    }
     while(ch=='y' || ch=='Y')
    {
        tail=new node;
        cout<<"give data"<<endl;
        cin>>dat;
        tail->link=new node;
        tail->link->data=dat;
        tail->link->link=NULL;
        tail=tail->link;
        cout<<"Enter more Data?(y/n)"<<endl;
        cin>>ch;
    }
    showList(head);
}
void showList(list start)
    {
        while(start!=NULL)
        {
            cout<<start->data<<endl;
            start=start->link;
        }
    }



Last edited on
This head=temp; is the problem because temp is uninitialized.
I guess you want that: head=tail;

So better remove temp at all

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
thanks for the suggestion...it removed this error however my complete linked list is not getting printed..only the first element is being read..if any1 cud suggest me wat to do now
if you'd use code tags you'd get more feedback...

the problem is this:

tail=new node;

It overwrites tail while head still points to the old tail which is not changed at all. Just remove that line
Topic archived. No new replies allowed.