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

Chapter 1 - Intro To DATA STRUCT

The document provides an introduction to data structures. It defines a data structure as a logical model for organizing and managing data. The study of data structures includes their logical description and implementation. Data structures can be primitive or non-primitive, linear or non-linear, static or dynamic. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. The document also discusses abstract data types, declaring structures in C++, initializing structure variables, and the difference between data structures and arrays.

Uploaded by

ameeramzar85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter 1 - Intro To DATA STRUCT

The document provides an introduction to data structures. It defines a data structure as a logical model for organizing and managing data. The study of data structures includes their logical description and implementation. Data structures can be primitive or non-primitive, linear or non-linear, static or dynamic. Common data structures include arrays, linked lists, stacks, queues, trees and graphs. The document also discusses abstract data types, declaring structures in C++, initializing structure variables, and the difference between data structures and arrays.

Uploaded by

ameeramzar85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter 1

INTRODUCTION TO
DATA STRUCTURE
Learning Outcome:

• Student should be able to:


– Understand data structure
– Understand array of data structure
Computer
Language

Find Solution:
Solve the
world
problem
Programmer

The best way to solve problem, is to draw flowchart


Understand data structure
Understand data structure
Now we explore some convenient techniques for organizing and
managing information.

Define data structure


A data structure is a logical model of a particular
organization of data.
The study of data structure includes:
– Logical description of the data structure.
– Implementation of the data structure
Type of data type
Describe with example the types of data
structures
a. Primitive and non-primitive
b. Linear and non-linear
Linear→The elements form a sequence.
Eg: arrays, linked lists, stack and queue.
Non-linear→The elements do not form a sequence.
Eg: tree and graph
c. Static and dynamic
A static data structure has a fixed size.
A dynamic data structure grows and shrinks at execution
time as required by its contents.
Describe the Abstract Data Type(ADT)
✓ Collections can be implemented in many different ways.

✓ An abstract data type (ADT) is an organized collection of


information and a set of operations used to manage that
information.

✓ The set of operations defines the interface to the ADT.

✓ As long as the ADT fulfills the promises of the interface, it


doesn't really matter how the ADT is implemented.

✓ Objects are a perfect programming mechanism to create ADTs


because their internal details are encapsulated.
Data Structure in Programming
Definition :

• A data structure is a group of data elements grouped together


under one name,

• These data elements, known as members, can have different


types and different lengths, and

• A data structure creates a new type.

• A data structure is a user-defined data type.


• Although the data type is different, it still relate to each other.
Analogy (Data Passing)

NoKP Nama Umur

NoKP Umur

Nama
Data type of C++
JENIS DATA
JENIS DATA MUDAH
BERSTRUKTUR
• int • Struktur storan (storage
• char structure)
• Float • Struktur keadaan
• double (Circumstance Structure)
• Struktur pautan (linking
structure)
• Struktur hubungan (relation
structure)
Declaring and Defining a Structure
• Data structure is mathematically way for storage of data in the
computer memory (storing and accessing)

• struct statements are used to define a structure.

• Its defines new data type and can have more than 1 members.
Struct Declaration
Data structures are declared in C++ using the following syntax:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;

• where structure_name is a name for the structure type


• object_name can be a set of valid identifiers for objects that have the type
of this structure.
• Within braces { } there is a list with the data members, each one is
specified with a type and a valid identifier as its name.
Struct Declaration

Declaration in C++ :
Keyword struct is in C++ library

struct structure_name {
member_type1 member_name1;
member_type2 member_name2; Members
member_type3 member_name3;
.
.
};
structure_name object_name;
Struct Declaration: Example

Example 1:
Keyword struct is in C++ library structure_name

struct product{
int weight;
Members
float price;
}; object_name
product apple;
structure_name
Structure Declaration
Like database records
• Structs are like database records - a row in a table, where the field names are like
the column names.
Rekod Pelajar
Rekod Pekerja Setiap REKOD mempunyai
Rekod Pelanggan MEDAN/LOKASI data yang
Rekod Penumpang tersendiri
Rekod Inventori

Rekod Pekerja
Dalam Bahasa C/C++ -No Pekerja
Rekod = Struktur -Nama Penuh
Medan/Lokasi = ahli data (p/u) -Jawatan
-Gaji Pokok
Example
Rekod Pelajar Variable Declaration

Medan :
No KP char NoKP[15];
Nama Penuh char NamaPenuh[35];
Kursus char Kursus[15];
Semester int Semester;
HPNM float HPNM;

Takrifan Struktur
struct RekodPelajar Structure Tag
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15]; Members
int Semester;
float HPNM;
} pelajar_JTMK; Structure Variable
Structure Declaration &
Pembolehubah Struktur Secara
Berasingan
struct RekodPelajar
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
};
struct RekodPelajar pelajar_JTMK;
Pengisytiharan Struktur &
Pembolehubah Struktur Secara Serentak
struct RekodPelajar
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
} pelajar_JTMK;
Pengisytiharan Pembolehubah
pelajar_JTMK Tanpa Nama Struktur

struct
{
char NoKP[15];
char NamaPenuh[35];
char Kursus[15];
int Semester;
float HPNM;
} Pelajar_JTMK;
Exercise
• Based on the exercise one, declare a name for another
object of the type student as student2.

• Add a code to receive an input from a user for each member


in struct student and used student2 to refer for each
member.

• Display the new results after modification of an exercise one


is done.
Declare multiple data structure.
Example code 3:
#include <iostream>
#include <string>
using namespace std; Structure name

struct Student{
string matric_num;
string gred; Members

double cpa;
}; Continue..
Declare multiple data structure.
Example code 3: Structure name

struct Subject{
string subject_code;
Members
string subject_name;
};
Student student1;
Object name
Subject subject1;

Structure name
Continue..
Initialize data structure.
Example code 5:
#include<iostream>
using namespace std;
struct Quiz{
double quiz1, quiz2, quiz3, quiz4, quiz5;
};
struct Test{
double test1,test2;
}; Continue..
Initialize data structure.
Example code 4:
Quiz quiz;
Object name
Test test;
void main(){
quiz.quiz1=2.30;
quiz.quiz2=3.30; Initialize data
structure
quiz.quiz3=2.50;
quiz.quiz4=3.40;
quiz.quiz5=3.20; Continue..
Initialize data structure.
Example code 4:
test.test1=15.00; Initialize data
structure
test.test2=13.00;
cout<<"Quiz 1\t:"<<quiz.quiz1<<"\n";
cout<<"Quiz 2\t:"<<quiz.quiz2<<"\n";
cout<<"Quiz 3\t:"<<quiz.quiz3<<"\n";
cout<<"Quiz 4\t:"<<quiz.quiz4<<"\n";
cout<<"Quiz 5\t:"<<quiz.quiz5<<"\n";
Continue..
Initialize data structure.
Example code 5:
void main(){

Structure
Quiz quiz={2.30,3.30,2.50,3.4,3.2};
Initialize data structure
name
Test test={15.00, 13.00};
Object name

Continue..
Exercise
The formula to calculate is as below:
TTest 1 + TTest 2 + A1+A2 + A3
* 100
50
In your program :
i. Declare TWO structure data and give the name for the
structure name as TheoryTest and Assignment
ii. Will receive the values for each marks from student
iii. Will display the values for each mark from student
iv. Will display the total mark for a student at the end of the
semester.
Understand Array Data
Structure
Difference Between
Data Structure and Array
Data Structure Array

• Data Stucture is a collection of • Array is a collection of


heterogeneous data. homogeneous data.
• Structure elements are referred • Array elements are referred by
by its unique name. subscript.
• Data Stucture elements are • Array elements are accessed by
accessed by its object as '.' it's position or subscript.
operator.
Difference Between
Data Structure and Array
Data Structure Array

• Syntax in data structure : • Syntax in array :


struct struct_name { <data_type> array_name[size];
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
};
structure_name object_name
[size];
Difference Between
Data Structure and Array

Data Structure Array

• Example : • Example :
struct product{ int matric_num[10];
int weight;
float price;
};
product apple;
Describe array as basic data
Example code 1:
structure
#include <iostream>
using namespace std;
struct STUDENT{
int matric_num;
float test;
float final;
};
STUDENT S1[5];

matric_num matric_num matric_num matric_num matric_num


test test test test test
final final final final final

S1 0 S1 1 S1 2 S1 3 S1 4
Continue..
Identify the disadvantages of arrays ???

You might also like