bubble sort using linked list

kindly guide me thru the errors in my code..why am i not getting a sorted list?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
  #include <iostream>
using namespace std;
struct node
{
    int data;
    struct node *link;
};
typedef struct node *list;
int main()
{
    int dat,countt=0,swapp;
    char ch;
    list head,temp;
    head=NULL;
    cout<<"Enter the list of numbers you want to sort?(y/n)"<<endl;
    cin>>ch;
    while(ch=='y'||ch=='Y')
    {
        cout<<"Data?"<<endl;
        cin>>dat;
        temp=new node;
        temp->data=dat;
        temp->link=head;
        head=temp;
        cout<<"Enter another number?(y/n)?"<<endl;
        cin>>ch;
    }
    temp=head;
    cout<<"The numbers are\n";
    while(temp!=NULL)
    {
        cout<<temp->data<<"\t";
        temp=temp->link;
        countt++;
    }
    temp=head;
    cout<<"\n";
    cout<<countt;
    for(int i=0;i<countt;i++)
    {
        for(int j=0;j<countt;j++)
        {
            if((temp->data)>(temp->link->data))
            {
               swapp=temp->data;
               temp->data=temp->link->data;
               temp->link->data=swapp;
               temp=temp->link;
            }
        }
    }
    temp=head;
    cout<<"The numbers are\n";
    while(temp!=NULL)
    {
        cout<<temp->data<<"\t";
        temp=temp->link;
        countt++;
    }


}

Topic archived. No new replies allowed.