0% found this document useful (0 votes)
62 views30 pages

Chapter One: Fundamentals of Data Structure

This document provides an introduction to data structures. It discusses that data structures describe how data is organized in memory and algorithms manipulate this data. There are two main types of data structures: contiguous and non-contiguous. Contiguous structures like arrays store data together in memory, while non-contiguous structures like linked lists store data in scattered locations linked together. Common non-contiguous structures include linked lists, trees, and graphs. The document provides examples and characteristics of various data structures like arrays, stacks, queues, and discusses static versus dynamic memory allocation using new and delete operators in C++.

Uploaded by

medilvyboy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views30 pages

Chapter One: Fundamentals of Data Structure

This document provides an introduction to data structures. It discusses that data structures describe how data is organized in memory and algorithms manipulate this data. There are two main types of data structures: contiguous and non-contiguous. Contiguous structures like arrays store data together in memory, while non-contiguous structures like linked lists store data in scattered locations linked together. Common non-contiguous structures include linked lists, trees, and graphs. The document provides examples and characteristics of various data structures like arrays, stacks, queues, and discusses static versus dynamic memory allocation using new and delete operators in C++.

Uploaded by

medilvyboy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter One

Fundamentals of Data structure


Data Structures: Dynamic Objects / Slide 2

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

Fundamental Type Of Data


Structures
Two types:-
 Contiguous
 Non-Contiguous
 In a contiguous structure, items of data are kept
together in memory (either in RAM or in a disk file).
 Items in a non-contiguous structure are scattered in
memory, But are linked to each other in some way.
Data Structures: Dynamic Objects / Slide 4

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

Contiguous structures example


 An array is an example of a contiguous
structure, since each element in the array is
located next to one or two other elements

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

Non-Contiguous structures Example

 A linked list is an example of a non-contiguous


data structure.
 Here, the nodes of the list are linked together
using pointers stored in each node.
 Non-contiguous are of two Types:
 Linear
 Non-Linear
Data Structures: Dynamic Objects / Slide 8

Linear structures Example

Singly Linked list Doubly Linked List


Data Structures: Dynamic Objects / Slide 9

Non-Linear structures Example

Binary Graph
Tree
Data Structures: Dynamic Objects / Slide 10

Implementation of Non-Contiguous Data Structure

 Non-contiguous structures are implemented


as a collection of data items, called nodes
 A node can point to one or more other nodes
in the collection.
 The simplest kind of non-contiguous structure
is a linked list
Data Structures: Dynamic Objects / Slide 11

Another Data Structure types


 Stacks
 Type of Structure to insert and remove an item at the top
of the collection
 Called LIFO (Last In First Out )data Structure

 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

Characteristics of Data Structures

Data Structure Advantage Disadvantage


Array Very fast access if Slow search and
index is known Deletion, fixed size
Ordered array Quicker search than Slow insertion and
unsorted array deletion, Fixed size
Stack Provides last-in first- Slow access to the
out access other items
Queue Provides First-in first- Slow access to the
out access other items
Data Structures: Dynamic Objects / Slide 13

Characteristics of Data Structures


(Cont’d)
Data Structure Advantage Disadvantage

Linked List Quick Insertion and deletion, Slow Search


variable size

Binary Tree Quick search, Insertion and Deletion algorithm is


deletion for balanced tree complex

Heap Fast insertion, deletion, access Slow access to the other


to the largest item item

Graphs Models real-world situations Some algorithms are


slow and complex
Data Structures: Dynamic Objects / Slide 14

Dynamic variables
Data Structures: Dynamic Objects / Slide 15

Static vs. Dynamic variables


 Static object  Dynamic object
 Memory is acquired
 Memory is acquired by program
automatically with an allocation request
 Memory is returned
new operation
automatically when object  Dynamic objects can exist beyond
goes out of scope the function in which they were
allocated
{  Object memory is returned by a
int a[200]; deallocation request
int b; delete operation
… int* ptr;
} ptr = new int[200];
ptr = new int;

delete [] ptr;
Data Structures: Dynamic Objects / Slide 16

Object (variable) creation: New


Syntax
ptr = new SomeType;

where ptr is a pointer of type SomeType

Example
int* p = new int;

Uninitialized int variable

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

 int *p, n=10;


p = new int; new

p
p = new int[100];

new

p
new

p = new int[n]; p
Data Structures: Dynamic Objects / Slide 20

Memory Allocation Example


Need an array of unknown size

void main()
{
int n;
cout << “How many students? “;
cin >> n;

int *grades = new int[n];

for(int i=0; i < n; i++)


{
cout << “Input Grade for Student” << (i+1) << “ ? :”;
cin >> grades[i];
}
. . .
printMean( grades, n ); // call a function with dynamic
array
. . .
Data Structures: Dynamic Objects / Slide 21

Freeing (or deleting) Memory


Data Structures: Dynamic Objects / Slide 22

Dangling Pointer Problem


int *A = new int[5];
for(int i=0; i<5; i++)
A[i] = i;
int *B = A;
A
0 1 2 3 4
B

delete [] A; Locations do not belong to program


B[0] = 1; // illegal!
A —

?
B
Data Structures: Dynamic Objects / Slide 23

Memory Leak Problem


int *A = new int [5];
for(int i=0; i<5; i++)
A[i] = i;
A 0 1 2 2 3 4

These locations cannot be


A = new int [5]; accessed by program

A 0 1 2 3 4

— — — — —
Data Structures: Dynamic Objects / Slide 24

Exercise on dynamic memory

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

Exercise on dynamic memory

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>

void initialize(int[ ], int, int);


int* deleteFirst(int ,int& size);
int* addElement(int , int& , int);
void print(int [ ], int);
Data Structures: Dynamic Objects / Slide 27

The main Function


int main() initialize(A, n, 3);
{ print(A, n);
int n; A = addElement(A,n,5);
cout << "Enter list size: "; print(A, n);
cin >> n; A = deleteFirst(A,n);
int *A = new int[n]; print(A, n);
if(n<=0) delete [] A;
{ return 0;
cout << "bad size" << endl; }
return 0;
}
Data Structures: Dynamic Objects / Slide 28

initialize and print


void initialize(int list[], int size, int value) void print(int list[], int size)
{ {
for(int i=0; i<size; i++) cout << "[ ";
list[i] = value;
for(int i=0; i<size; i++)
}
cout << list[i] << " ";
cout << "]" << endl;
}
Data Structures: Dynamic Objects / Slide 29

Deleting the First Element


int* deleteFirst(int list[], int& size)
{
if(size <= 1) // copy and delete old array
{
size = 0;
return NULL;
for(int i=0; i<size-1; i++)
}
int* newList = new int [size-1]; newList[i] = list[i+1];
// make new array
if(newList==0)
delete [ ] list;
{ size--;
cout << "Memory allocation return newList;
error for deleteFirst!";
exit(0); }
}
Data Structures: Dynamic Objects / Slide 30

Adding Element at the end


int* addElement(int list[], int& size, int value) for(int i=0; i<size; i++)
{ newList[i] = list[i];
int* newList = new int [size+1]; if(size) delete [] list;
// make new array newList[size] = value;
if(newList==0) size++;
{ return newList;
cout << "Memory allocation error for }
addElement!" << endl;
exit(0);
}

You might also like