Sorting linked list

Our case study was to create a 2d array that uses sorting searching and linked list and apply classes, functions, structure and pointers so I am sorry I made the code complicated. The problem is on sorting, if you will run the code, there will be an excess number (I think it is addresS), how should I fix it? and the second problem is how should I put the name beside the student number so that it will look like this:

SORT > What block do you want to sort? [1] >
201310246 Jenny Cruz
201536975 Hailey Dream

wherein the number is snum (student number)
and the then fname and lname (first & last name) thanks!

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  #include<iostream>
#include<conio.h>
#include<string>

using namespace std;
int blk=0,rc=0,temp=0;//record number
int ch,mainmenu();//,ADD(),DISPLAY(struct StudentRecord sr,int );
void ADD(),DISPLAY(),SORT(),SEARCH();
const int size=20;

struct StudentRecord{
string fname,lname;
int snum;
float GWA;
}sr[5][size];

struct node
{
    int data;
    struct node *next;
};

main()
{

do{ 
ch=mainmenu();
}while (ch!=20);
				
cout<<"END";
getch();
return 0;
}

int mainmenu()
{
cout<<endl<<"<<@@ STUDENT RECORDS @@>>"<<endl<<endl;
cout<<"[1] ADD"<<endl;
cout<<"[2] SORT"<<endl;
cout<<"[3] SEARCH"<<endl;
cout<<"[4] DISPLAY"<<endl;
cout<<"[5] EXIT"<<endl;
cout<<endl<<"Enter Choice: ";
cin>>ch; 
//cout<<"RC: "<<rc;
switch(ch)
	{	
	case 1: ADD();break;
	case 2: SORT();break;
	case 3: SEARCH();break;
	case 4: DISPLAY();break;
	case 5: return 20;break;
	}
}


void ADD()
{
system("cls");
if (rc==20)
	{
		cout<<"Cant add more record";
		getch();
	}
else
	{
	cout<<"Enter Block [1/2]: ";
	cin>>blk;
//	if (blk == temp)
	rc++;
//	else
//	rc--;
	temp = blk;
	cout<<"\nFirst Name: ";
	cin>>sr[blk][rc].fname;
	cout<<"Last Name: ";
	cin>>sr[blk][rc].lname;
	cout<<"Student Number: ";
	cin>>sr[blk][rc].snum;
	cout<<"GWA: ";
	cin>>sr[blk][rc].GWA;
	}
system("cls");
}

void DISPLAY()
{
system("cls");

if (rc!=0)
	{
	cout<<endl<<"<<@@List of records@@>>"<<endl<<endl;
	cout<<"BLOCK 1\n\n";
	cout<<"#: NAME:          STUDENT NO:  GWA:"<<endl;
	for (int i=1;i<=rc;i++)
		{
		cout<<i<<": "<<sr[1][i].fname<<" "<<sr[1][i].lname<<" : "<<sr[1][i].snum<<" : "<<sr[1][i].GWA<<endl;
		}
	cout<<"\nBLOCK 2\n\n";
	for (int i=1;i<=rc;i++)
		{
		cout<<i<<": "<<sr[2][i].fname<<" "<<sr[2][i].lname<<" : "<<sr[2][i].snum<<" : "<<sr[2][i].GWA<<endl;
		}
	}
else 
	cout<<"No record to display"<<endl;
}

void SEARCH()
{
system("cls");
int sn,s ;
cout<<"\nEnter Student number you want to Search = ";
cin>>sn;


for(int i=0;i<=size;i++)
{
        for(int j=0;j<=size;j++)
        {
                if  (sr[i][j].snum==sn)
                  {
                   cout<<"\nStudent is Found at Block: "<<i;
                    s=1;
                    cout<<"\n\n: "<<sr[i][j].fname<<" "<<sr[i][j].lname<<" : "<<sr[i][j].snum<<" : "<<sr[i][j].GWA<<endl;
                    break;
                     }
                     
         }

}
if(s==0)
                      cout<<"Student is Not Found";
}

class Sortfunc { 
 public:
        
        
/* Function to insert a node at the begining of a linked lsit */
void insertAtTheBegin(struct node **start_ref, int data)
{
    struct node *ptr1 = (struct node*)malloc(sizeof(struct node));
    ptr1->data = data;
    ptr1->next = *start_ref;
    *start_ref = ptr1;
}
 
/* Function to print nodes in a given linked list */
void printList(struct node *start)
{
    struct node *temp = start;
    printf("\n");
    while (temp!=NULL)
    {
        printf("%d \n", temp->data);
        temp = temp->next;
    }
}
 
/* Bubble sort the given linked lsit */
void bubbleSort(struct node *start)
{
    int swapped, i;
    struct node *ptr1;
    struct node *lptr = NULL;
 
    /* Checking for empty list */
    if (ptr1 == NULL)
        return;
 
    do
    {
        swapped = 0;
        ptr1 = start;
 
        while (ptr1->next != lptr)
        {
            if (ptr1->data > ptr1->next->data)
            { 
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}
 
/* function to swap data of two nodes a and b*/
void swap(struct node *a, struct node *b)
{
    int temp = a->data;
    a->data = b->data;
    b->data = temp;
}

};


void SORT(){

    Sortfunc s;
    int arr[20]; 
    int list_size, i,ans;
    cout << "What block do you want to sort? [1/2]: ";
    cin >>ans;
    
    if(ans==1)
      for(int i=1;i<=size;i++)
      arr[i] = sr[1][i].snum;
    else
       for(int i=1;i<=size;i++)
      arr[i] = sr[2][i].snum;    
     
     
    /* start with empty linked list */
    struct node *start = NULL;
 
    /* Create linked list from the array arr[].
      Created linked list will be 1->11->2->56->12 */
    for (i = 0; i<=size; i++)
        s.insertAtTheBegin(&start, arr[i]);
 
    /* print list before sorting */
    printf("\n Linked list before sorting ");
    s.printList(start);
 
    /* sort the linked list */
    s.bubbleSort(start);
 
    /* print list after sorting */
    printf("\n Linked list after sorting ");
    s.printList(start);
 

}
 
 
Topic archived. No new replies allowed.