Professional Documents
Culture Documents
Introduction
Goals of the Course
§Become familiar with some of the fundamental
data structures in computer science
• Type of Modules:
• A single, stand-alone function
• A method of a class
• A class
• Several functions or classes working closely together
• Other blocks of code
Problem Solving
A good solution consists of :
• Modules that
– organize data collection to facilitate operations
– must store, move, and alter data
– use algorithms to communicate with one another
Algorithm
• Module implements algorithms
State
Structure •Stack
•Queue
Data Types
Storage Structured Data Types
– Array – can contain multiple data with the same
types
– Struct – can contain multiple data with different
type
typedef struct { int age; char
*name;
enum {male, female} gender;
} Person;
Data Types
Linked Data Structure
– Linear Data Structure with restriction
(state Structure)
• Queue & Stack
– Linear Data Structure with no restriction
• Unsorted linked list
• Sorted linked list
– Non-linear Data Structure
• Binary Tree
• Graph
Linear Data Structure with
restriction
• Queue
– First-In-First-Out (FIFO) data structure
– the first element added to the queue will be the
first one to be removed (post office, bank etc)
Out
In
Back Front
Queue Application
Linear Data Structure with
restriction
•Stack
– Based on the principle of Last In First Out (LIFO)
– Stacks are used extensively at every level of a
modern computer system (compiler etc.)
§In Out
Top
Stack Application
Linear Data Structure with no
restriction
• Linked list consists of:
– a sequence of nodes,
– data fields
– one or two links or references pointing to the next
and/or previous nodes
3 12 11
Linear Data Structure with no
restriction
• Sorted linked list
– Data stored in ascending or descending order with
no duplicates
– Insertion at front, middle or rear of the list
– Deletion will not affect the ascending /
descending order of the list
• Unsorted linked list
– A linked list with no ordering
Non-linear Data Structure
• Tree
– A data structure based on a tree structure
– A tree structure is a way of representing the
hierarchical nature of a structure in a graphical
form
– a binary tree is a tree data structure in which each
node has at most two children
– Used for searching big amount of data
Tree
Root
Children of node 20
vertex
leaf
Sibling
Graph
• A graph consists of a set of vertices, and a set
of edges, such that each edge is a connection
between a pair of vertices.
Functional abstraction
• Separates the purpose of a module from its implementation
30
Abstraction Example
book
title
abstract to year attributes
author
publisher
price
getData()
print()
checkPrice() behavior
checkPublisher()
Abstraction of a book
31
Encapsulation
• The process of combining data and functions
into a single unit called class.
36
Constructors
– Used to create and initialize new instances of a class
– Is invoked when an instance of a class is declared
book(int = 2000);
Default Constructor Implementation
• Sets data members to initial values
book::book(){
price = 10.00;
strcpy (author,”Dayang Norhayati”);
strcpy (title, “Learn Data Structure”);
year = 2012;
}
// end default constructor
Instance declaration:
book myBook;
Instance myBook is created with the price set to 10.0, author
39 set to
Dayang Norhayati, title set to Learn Data Structure and year set to
2012
Constructor with Argument
Implementation
Instance declaration:
book myBook(“NorBahiah”,25.00);
• Example:
▫ Array of class students registered in Data Structure class
▫ Array of class lecturer teaching at FCI.
▫ Array of class subjects offered in Semester II.
public:
void read_data()
{ cin >> name >> age >> salary; }
void print_data()
{ cout << name << age << salary; }
} ;
46
Array of class Example
There are 2 methods to call member function for manager
array.
1. By using array subscript in order to access manager in
certain location of the array.
cin >> n ;
manager[n].read_data() ;
cout << manager[n].print_data() ;