0% found this document useful (0 votes)
38 views

Scratch Week 3

The document defines a Student data structure with fields for name, ID, GPA, semester, and a pointer to the next student. It then defines a linked list of Student nodes and includes an Insert function to add a new Student node in the correct sorted position by GPA.

Uploaded by

Nadita Vani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Scratch Week 3

The document defines a Student data structure with fields for name, ID, GPA, semester, and a pointer to the next student. It then defines a linked list of Student nodes and includes an Insert function to add a new Student node in the correct sorted position by GPA.

Uploaded by

Nadita Vani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Struct Student{ // User-defined data type

String Name;
String ID;
Float GPA;
Int Smstr;
};
Student S[26]; S[2].GPA; S[1].ID;
Student *p; p = S; (p+2)🡪GPA; (p+1)🡪ID;
Name ID GPA Smstr

Fulan 00120190001 3.2 4


John 00120190017 3.4 4

Jane 00120190020 3.0 4


Wahyuni 00120190023 3.45 4

Rizaldi 00120190002 3.35 4


Zidane 3.25

Disadvantage? 🡪 Linked List

Struct Student{ // User-defined data type


String Name;
String ID; Name ID Ne
Float GPA; GPA Smstr
xt
Int Smstr;
Student *next;
};
Wahy
xx..23 Ne
uni
Student *head = 0; 3.45 4
xt

xxx.1 xx…
John Ne Fulan xx..01 Jane Ne
7 Ne 20
xt xt xt
3.4 4 3.2 4 3.0 4
W xx
xx. Ne
Jo
3. xx.
Fu
Ne
ah
3. x.1
4 Ne
3.
.2
4 hn
xt
4 la
.0
4
yu
xt
45 7 xt
2
3 n
1
ni

xx.
Ri
Ne
3.
zal
.2
4
xt
35
di
0

Wahy xxx.1 xx…


xx..23 John Ne Fulan xx..01 Jane
uni Ne 7 Ne 20
xt xt xt 3.0 4
3.45 4 3.4 4 3.2 4
Struct Student{ // User-defined data type
String Name;
String ID; Name ID Ne
Float GPA; GPA Smstr
xt
Int Smstr;
Student *next;
};

Student *Insert(string n,string d,float g,int s,Student *h)


{
Student *cur = h,*prev = 0;
While(cur && cur->GPA >= g){
prev = cur;
cur = cur->next;
}
Student *temp = new Student;
temp->Name = n;
...
If(prev){ // in the middle and at the end
temp->next = prev->next;
prev->next = temp;
}else{
temp->next = cur;
h = temp;
}
Return h;
}

You might also like