Chapter One: Fundamentals of Data Structure
Chapter One: Fundamentals of Data Structure
Introduction
The Term Data structure
is used describe the way data is stored( arranged) in computer
memory or on disk
Can also be defined as the organization of raw data
Algorithms are the procedures a software uses to
manipulate the data in these structures in various
ways, such as
searching for particular data
sorting the entire data
When ever you think to choose a data structure for
particular problem you need to associate a certain
algorithm and vice- versa
So data structures and algorithms are interrelated.
Data Structures: Dynamic Objects / Slide 3
Contiguous structures
Arrays (Homogenous)
Fixed-Length
Variable-Length
Statically Allocated( Memory allocation
at compilation Time)
Dynamically Allocated( Memory
allocation at run time)
Structures
Non-Homogenous
Data Structures: Dynamic Objects / Slide 5
Example
Data Structures: Dynamic Objects / Slide 6
Non-Contiguous structures
non-contiguous
Linked lists
Singly linked
Doubly linked
Circularly linked
Trees
Binary trees
Binary search trees
Graphs
Data Structures: Dynamic Objects / Slide 7
Binary Graph
Tree
Data Structures: Dynamic Objects / Slide 10
Queue
Type of Structure to insert at the back of the collection
and remove at the front
Called FIFO (First in First Out )data Structure
Data Structures: Dynamic Objects / Slide 12
Dynamic variables
Data Structures: Dynamic Objects / Slide 15
Example
int* p = new int;
p
Object (variable) destruction:
Data Structures: Dynamic Objects / Slide 17
Delete
Syntax
delete p;
storage pointed to by p is returned to free
store and p is now undefined
Example
int* p = new int;
*p = 10;
delete p;
p 10
Data Structures: Dynamic Objects / Slide 18
Array of New:
dynamic arrays
Syntax
P = new SomeType[Expression];
Where
P is a pointer of type SomeType
Expression is the number of objects to be constructed
-- we are making an array
Because of the flexible pointer syntax, P can
be considered to be an array
Data Structures: Dynamic Objects / Slide 19
Example
Dynamic Memory Allocation
Request for “unnamed” memory from the
Operating System
p
p = new int[100];
new
p
new
p = new int[n]; p
Data Structures: Dynamic Objects / Slide 20
void main()
{
int n;
cout << “How many students? “;
cin >> n;
?
B
Data Structures: Dynamic Objects / Slide 23
A 0 1 2 3 4
— — — — —
Data Structures: Dynamic Objects / Slide 24
Lab
Write a program that:
Initialize list of an integer array
Delete the first element from the array
Insert an element at the end of the array
Print or display the content of the array
Data Structures: Dynamic Objects / Slide 25
Use
1. Dynamic memory allocation
2. Functions (prototypes are given below) to
accomplish the tasks above
void initialize(int[], int, int);
int* deleteFirst(int ,int& size);
int* addElement(int , int& , int);
void print(int [], int);
Data Structures: Dynamic Objects / Slide 26
Solution
The prototypes and the preprocessor directive
#include<iostream.h>