1 / 23

Introduction to Algorithms & Data Structures

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.

pgrove
Download Presentation

Introduction to Algorithms & Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Algorithms & Data Structures (Part II) By Suzila Yusof

  2. 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.

  3. 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

  4. 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.

  5. 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.

  6. Structure Definitions cont ... • Syntax

  7. 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.

  8. 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)

  9. 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

  10. 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;

  11. 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;

  12. Declaration of Variable Structure pelajar_JTMK Anonymous struct { char NoKP[15]; char NamaPenuh[35]; char Kursus[15]; int Semester; float HPNM; } Pelajar_JTMK;

  13. 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};

  14. Memory Representation  Pelajar_JTMK

  15. 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

  16. 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

  17. Examples of Operations on Structural Elements.

  18. 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.

  19. 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;

  20. 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; };

  21. 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; };

  22. 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+’};

  23. 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;

More Related