240 likes | 442 Views
Introduction to Algorithms & Data Structures. (Part II) By Suzila Yusof. Data Structures in Programming. Structure in C + + is represented by the struct keyword which shows the data structure to allow different types of data stored in a name.
E N D
Introduction to Algorithms & Data Structures (Part II) By Suzila Yusof
Data Structures in Programming • Structure in C + + is represented by the struct keyword which shows the data structure to allow different types of data stored in a name. • Although these different types of data, but it interconnected with each other.
Data Structures in Programming cont… • Array is a data structure but the data is stored is of the same type. • Examples of data structures: - array - linked list - tree - stack - queue
The terms used in the data structure • Data: a value or set specific values • Item Data: refers to a unit value • Item Group: Items that are broken down into sub-sub item • Elementary Item: Item data are not broken • Examples : Name (Item Group) can be broken down into FirstName and LastName but NOKP (Item Elementary) exists as a single item that does not be solved.
Structure Definition • The structure is a collection of data related to use of the same name. • Structural element known as a member of, and may consist of the same type • Each structure can be defined with a name similar structure known as the tag structure • Struct statement used to define the structure. It defines the new data types and have more than one member.
Structure Definitions cont ... • Syntax
Structure Definitions cont ... • Each definition is a definition of normal variables such as int or float sales i [20] • Or any legal interpretations, including pointer variables, if the structure requires pointer • At the end of the previously defined structure (;) Finally, you can specify one or morestructural variables. • This will require processors to provide space for variables and types of variables in the structure.
Structure Definitions cont ... RECORD Rekod Pelajar Rekod Pekerja Rekod Pelanggan Rekod Penumpang Rekod Inventori Each Record hasField / Location dataseparate • Rekod Pekerja • No Pekerja • Nama Penuh • Jawatan • Gaji Pokok Dalam Bahasa C/C++ Rekod = Struktur Medan/Lokasi = ahli data (p/u)
Examples Student Records Declaration of variables Medan : No KP Nama Penuh Kursus Semester HPNM char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; Structural definition Tag structure struct RekodPelajar { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; } pelajar_JTMK; Member data Variable Structure
Declaration of Variable Structure & Structure of The Separate struct RekodPelajar { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; }; struct RekodPelajar pelajar_JTMK;
Declaration of Variable Structure & Structure of The Coincident struct RekodPelajar { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; } pelajar_JTMK;
Declaration of Variable Structure pelajar_JTMK Anonymous struct { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; } Pelajar_JTMK;
Declaration of Variable Structure & Structure with Initial Value assigment. struct RekodPelajar { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; } pelajar_JTMK = {001122115533, “Ahmad Ali Abu”, “DIP”,5, 3.99};
Memory Representation Pelajar_JTMK
Reference Location • The location for the expert referred to the structure using the dot operator • Sintaks : • example pembolehubahStruktur.ahliStruktur Lokasi ini dirujuk sebagai : pelajar_JTMK.Kursus
Overview 2 RekodPelajar structure variables with the initial value of the lot HPNM Semester Kursus NamaPenuh NoKP Pelajar_JTMK Pelajar_JKE HPNM Semester Kursus NamaPenuh NoKP
Nested Structure • The situation in which there are elements of the samestructure or other structuralmembers. • Purpose is to structure the classification of records according to certain.
Declaration of structure with many elements Struct pelajar { char nama; int no_pend; char KodJab; int umur; char jantina; int marka_ kuiz1; int markah_kuiz2; int markah_kuiz3; int markah_tugasan1; int markah_tugasan2; int markah_amali1; int markah amali2; char gred; } rekodPelajar;
Declaration of structure with nested structures Struct peribadi_pelajar { char nama; int no_pend; char KodJab; int umur; char jantina; }; struct markah_pelajar { int markah_ kuiz1; int markah_kuiz2; int markah_kuiz3; int markah_tugasan1; int markah_tugasan2; int markah_amali1; int markah amali2; }; struct pelajar { struct peribadi_pelajar peribadi; Struct markah_pelajar markah; char gred; };
Other ways of the Declaration of structure with nested structures Struct pelajar { struct { char nama; int no_pend; char KodJab; int umur; char jantina; } peribadi; struct { int markah_ kuiz1; int markah_kuiz2; int markah_kuiz3; int markah_tugasan1; int markah_tugasan2; int markah_amali1; int markah amali2; } markah; char gred; };
Nested structures cont ... • Variable declaration and assignment of initial value for the nested structure is the same as the normal structure of variable declarations. Example : to streamline the assignment bg nested structure, the use of tan '{' and '}' to the same structure is highly encouraged. Example: struct pelajar pAli = {“Ali Abu”, 722, “JTMK”, 20, ‘L’, 5, 4, 4, 8, 8, 7, 6, ‘B+’}; struct pelajar pAli = {{“Ali Abu”, 722, “JTMK”, 20, ‘L’ }, {5, 4, 4, 8, 8, 7, 6}, ‘B+’};
Reference Example Elements for nested structures cout <<“NAMA:” <<pAli.peribadi.nama<<“\n; cout <<“MARKAH KUIZ 3:” <<pAli.markah.markah_kuiz3<<“\n; cout <<“GRED:” <<pAli.gred<<“\n;