Summary

This document is a lecture on data structures in C++, covering fields, records, files, arrays, stacks, and queues. It includes examples and explains the concepts. The lecture was delivered by a university department of electrical and electronic engineering.

Full Transcript

Lecture (2)  Fields, Records, Files  Arrays  Stacks  Queues 2  Data structure means the way by which we structurally organize the data in memory to determine the efficiency with which different operations on the information are carried ou...

Lecture (2)  Fields, Records, Files  Arrays  Stacks  Queues 2  Data structure means the way by which we structurally organize the data in memory to determine the efficiency with which different operations on the information are carried out.  We must choose the data structures carefully on the basis of which operation the application will use most frequently. 3  A field is an item of stored data, and could be a name, an address, a date, a description, a quantity etc.  A field is defined with given a name (identifier) and a type.  The data type defines the data that will be stored in the field. 4  Examples of fields: String firstName; int quantity; Boolean isInteger;  A record is a collection of fields that related to a single entity.  A student record may include student id, name, birth date, address and GPA fields. 5 General syntax in C++: sruct StructName; { data type identifier; … }  Struct is a collection of fields of a different data types. The fields of struct are referred to as members.  The individual member of struct be accessed by the name of the struct followed by the name of the member. thePerson.firstName = “Ahmed”; cout > employee.empName; cin >> employee.empPayRec.salary; 8  A file is a collection of related records.  For example, a student file might include all of the records of students enrolled at school and a police department might keep a file of criminal records.  Files are stored on a secondary storage devices such as disks, cloud, etc. 9  Key is a field in a data file containing information capable of uniquely identifying a record in relation to all other records in the same data file.  No two records within a file can have the same key value.  Data records stored on files can be accessed and processed later.  The file can be of a fixed length record or a variable length record. 10  An array is a fixed number of elements of the same type stored sequentially in memory.  An integer array holds some number of integers and a character array holds some number of characters and so on.  The size of array is referred to as its dimension.  To declare an array in C++: type arrayName [ dimension ] 11  To declare an integer array named arr of four elements: int arr  To initialize its elements, we use: arr = 15; arr = 21; arr = 9; arr = 13; OR arr = { 15, 21, 9,13 }  Sometimes it is convenient to let the compiler determine the size of the array: int arr { 9, 0, 6, 7, 12, 10, 14 }  When accessing an array the index given must be a positive integer from 0 to n-1, where n is the dimension of the array. 12 #include using namespace std; int main() { int arr; cout arr[i]; // inputting array values } cout

Use Quizgecko on...
Browser
Browser