Radix sort implementation-Segmantation fault

I'm trying to implement Radix sort(http://en.wikipedia.org/wiki/Radix_sort). I'm getting segmentation fault at line 74 in the code although i'm not trying to dereference any null pointer.


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<iostream.h>
#include<conio.h>
#include<cmath>

using namespace std;

int *p;    //pointer to array of elements
int n;     //length of array

int cnt=1;//to track the maximum length of entered numbers

class node
{
    public:
        int data;
        node *link;
}*a[10];   //array of pointers to node
 
void initialize()  //initializes each node to NULL
{
     for(int i=0;i<10;i++)
     {
        a[i]=new node;
        a[i]=NULL;
     }
}

void calculate(int i) //calculates the maximum length among the entered numbers
{
    int cnt2=1;
        i=i/10;
        while(i!=0)
        {
                i=i/10;
                cnt2++;
                if(cnt<cnt2)
                        cnt++;
        }    
}
        
void insert(int n,node *st)   //inserts element to the end of linked list
{
        node *temp=new node;
        temp->data=n;
        if(st==NULL)
        {
                temp->link=NULL;
                st=temp;
        }
        else
        {
                while(st->link!=NULL)
                {
                        st=st->link;
                }
                st->link=temp;
                temp->link=NULL;
        }
}
 
void array_insert()   //relocates the array with the new numbers according to 
                      //the elements in the linked list 
{
        int i=0,j=0;
        
        beg:
        node *temp=new node;
        temp=a[i];
        while(j<n)
        {
                if(temp==NULL)
                        {
                        i++;
                        goto beg;
                        }
                else if(temp->link==NULL)
                        {
                        p[j]=temp->data;
                        j++;
                        i++;
                        goto beg;
                        }
                else
                        {
                        p[j]=temp->data;
                        j++;
                        temp=temp->link;
                        }
        }
}
 
void radix_sort()   //sort the array according to radix sort method
{
        initialize();
        int i=0,div,mod;
        for(i=0;i<cnt;i++)
        {
                int j;
                for(j=0;j<n;j++)
                {
                        div=p[j]/pow(10.0,i);
                        mod=div%10;
                        switch(mod)
                        {
                                case 0:
                                        insert(p[j],a[0]);
                                        break;
                                case 1:
                                        insert(p[j],a[1]);
                                        break;
                                case 2:
                                        insert(p[j],a[2]);
                                        break;
                                case 3:
                                        insert(p[j],a[3]);
                                        break;
                                case 4:
                                        insert(p[j],a[4]);
                                        break;
                                case 5:
                                        insert(p[j],a[5]);
                                        break;
                                case 6:
                                        insert(p[j],a[6]);
                                        break;
                                case 7:
                                        insert(p[j],a[7]);
                                        break;
                                case 8:
                                        insert(p[j],a[8]);
                                        break;
                                case 9:
                                        insert(p[j],a[9]);
                                        break;  
                        }       
                }
                array_insert();
        }
                
}
 
int main()
{
        cout<<"Enter the number of elements"<<endl;
        cin>>n;
 
        p=new int[n];
 
        cout<<"Enter the elements"<<endl;
 
        int i;
        for(i=0;i<n;i++)
        {
        cin>>p[i];
                calculate(p[i]);
    }
        
    radix_sort();
        
        for(i=0;i<n;i++)
        {
        cout<<p[i]<<"\t";
    }
    getch();
    return 0;
}



1
2
        a[i]=new node;
        a[i]=NULL;

┬┐what do you think you are doing?

1
2
3
4
5
6
7
8
void insert(int n,node *st){
//...
        if(st==NULL)
        {
                temp->link=NULL;
                st=temp;
        }
//...  
`st' is an input parameter. Its modification would not be seeing outside the function.

Get rid of the globals and the goto.
thank you ne555 the node *st was causing the problem as it's modification was invisible outside function.I modified it as follow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void insert(int n,int i)
{
	node *temp=new node;
	temp->data=n;
	
	if(a[i]==NULL)
	{
		temp->link=NULL;
		a[i]=temp;
	}
	else
	{
		node *temp2=new node;
		temp2=a[i];
		while(temp2->link!=NULL)
		{
			temp2=temp2->link;
		}
		temp2->link=temp;
		temp->link=NULL;
	}
}
Topic archived. No new replies allowed.