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

ch05 ADTarray en

data structure array

Uploaded by

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

ch05 ADTarray en

data structure array

Uploaded by

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

Chapter 3

Abstract Data Types: Arrays

Data Structures

1
Outline
Introduction
ADT Examples
Operations on Arrays
Implementation of Arrays
Sorted Array
Unsorted Array
Application: array of structures

2
INTRODUCTION AND ADT EXAMPLES

3
Introduction
 Data abstraction:
 is the description of the functional qualities independently
from the implementation
 hides details
 ADT (Abstract Data Type) : model real word objects
 e.g., int, float, etc are predefined number models
 new data types can be created
 ADT is constituted by
 the representation of the data
 the operations on the data
 Separate the user interface from the implementation

4
Introduction
 Three steps for a good ADT creation
 understand a problem
 define the interface: the data and functions on these data
• representations, caracteristics, properties, operations
 implement interfaces
• interior behavior of the functions, algorithmic and coding
 Motivation
 Facilitate the management of large software
 Flexibility and reutilization of ADT
 We can implement the interfaces
• with different languages
• with different manners (e.g., to accelerate the execution time)

5
ADT Example: Queue
 Example: wait its turn in a line
 Data type: a list
 store in a FIFO (First In , First Out)
 Operations:
 Enqueue: add to the queue (to the last position)
 Dequeue: remove from the head of the queue
 A user
 manipulates his data by doing an enqueue and dequeue
operations only
 A programmer
 implements and access data freely

6
ADT Example: Stack
Example, a stack of plates
store in a LIFO (Last-in, first-out )
Operations:
Push: add an element
Pop: remove an element
A user
manipulates a stack by using the operations push and
pop
• does not interest in the implementation of the operations
A programmer
implements and access data freely
7
ADT Example: Array
Data type: a general list
Operations
Insert
Remove
Search
Sort
…
This is the subject of this chapter

8
ADT Example: String
 String of characters in C++
 we don’t have a predefined data type for the string of
characters
 a new ADT has been defined: string class
 Facilitate the manipulation of strings. This is an example
#include<iostream>
#include <string>
using namespace std;
int main(){
string fname, lname, name;
cout<<"lire un nom\n";
cin>>fname>>lname;
cout<<"size fname: "<<fname.length()<<endl;
cout<<"size lname: "<<lname.length()<<endl;
name=fname+" "+lname;
cout<<"Full name "<<name<<endl;
return 0;
9
}
ADT Example: Time
Data type:
time (hours, minutes, seconds)
Domaine:
verify the time constraints (24h, 60min, 60sec)
Operations:
Insert the actual time
Increase the time, second by second
Display the time
Compare two times (before, after or equal)

10
OPERATIONS ON ARRAYS

11
Operations on the array ADT
 An array is also called a list
 We consider two types of arrays
1. Unsorted array
2. Sorted array
 The main operations (or functions) to do in an array are:

1- Initialization 4- Modification
2- Insertion 5- Removal
3- Searching 6- Display

 Other operations (or functions)


 Test if the array is empty or full
 Return the elements number
 Insert or remove element at a given position or having a given value
 In the following, we will see the implementation of the array ADT
12
IMPLEMENTATION: UNSORTED ARRAY

13
Declarations, initializations and basic
tests
#define MAX 100
struct ARRAY{  The structure ARRAY contains:
int a[MAX];  a[]: array containing the data
int nbele;  nbele: the current elements
}; number in the array
void Initialize(ARRAY *t){  Each array starts empty initially
t->nbele=0;  It is necessary to test:
}  if the array is empty when we try
bool IsEmpty(ARRAY *t){ to remove an element
return (t->nbele==0);  if the array is full when we try to
} add an element
bool IsFull(ARRAY *t){
return (t->nbele==MAX);
}
Initialization: nbele=0; array is empty
Remark: to create the array ADT in the main 0 1 2 3 4 … 98 99
function, it is sufficient to do: ARRAY A; or …
ARRAY *pA=new ARRAY; then call the function Initialize(pA) 14
Searching functions
bool IsInArray(ARRAY *t, int val){  Here, we have two
for(int i=0; i<t->nbele; i++) sequential
if(t->a[i]==val)
return true;
searching functions
return false;  One of them
} verifies the
int FindElement(ARRAY *t, int val){ existence of a value
for(int i=0; i<t->nbele; i++)
if(t->a[i]==val)
 The other returns
return i; the index of the 1st
return -1; value occurrence, if
} it is found
int GetElementNb(ARRAY *t){
return t->nbele;
}
0 1 2 3 4 … 98 99
GetElement returns the effective
elements number. Here, nbele=4 9 11 4 8 …
15
Displaying function
void PrintArray(ARRAY *t){
if(IsEmpty(t)){
cout<<"The array is empty"<<endl;
}
for(int i=0; i<t->nbele; i++)
cout<< t->a[i] << " / ";
cout<<endl;
}

Display the array elements if it is not empty.


Here, we display 9 / 11 / 4 / 8
0 1 2 3 4 … 98 99
9 11 4 8 …
16
Insertion functions
Insert the value 5 in the head
0 1 2 3 4
bool InsertAtHead(ARRAY *t, int val){
if(IsFull(t)) 9 11 4 8
return false;
for(int i=t->nbele; i>0; i--) 0 1 2 3 4
t->a[i]=t->a[i-1]; //shifting
9 11 4 8
t->a[0]=val; //insert at head
t->nbele++; //increment nbele 0 1 2 3 4
return true; 5 9 11 4 8
}
bool InsertAtQueue(ARRAY *t, int val){ Insert the value 5 in tail
if(IsFull(t)) 0 1 2 3 4
return false;
9 11 4 8
t->a[t->nbele]=val; //insert at queue
t->nbele++; 0 1 2 3 4
return true;
9 11 4 8 5
}

17
Insertion functions (cont.)
bool InsertAtPosition(ARRAY *t, int val, int pos){
if(IsFull(t))
return false;
if(pos<0 || pos>=t->nbele)
return false;
for(int i=t->nbele; i>pos; i--)
t->a[i]=t->a[i-1]; //shifting
t->a[pos]=val; //insert at position
t->nbele++;
return true;
}
Insert the value 5 at the position (with index) 2
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
9 11 4 8 9 11 4 8 9 11 5 4 8

18
Deleting Functions
bool DeleteElement(ARRAY *t, int val){
if(IsEmpty(t))
return false;
for(int i=0;i<t->nbele; i++)
if(t->a[i]==val){//Find the position of val
for(int j=i; j<t->nbele-1; j++)
t->a[j]=t->a[j+1]; //shifting+deleting
t->nbele--; //decrease nbele
return true;
}
return false;
}
Remove the value 11. Firstly, find the position 11 that is 1, then shift
until 1 by crushing 11
0 1 2 3 4 0 1 2 3 4
9 11 4 8 9 4 8
19
Deleting Functions (cont.)
bool DeleteAtPosition(ARRAY *t, int pos){
if(IsEmpty(t))
return false;
if(pos<0 || pos>=t->nbele)
return false;
for (int i=pos; i<t->nbele-1; i++)
t->a[i]=t->a[i+1];
t->nbele--;
return true;
}
bool DeleteAllElement(ARRAY *t){//sufficient to put nbele=0
t->nbele=0; //next insertion will start at pos=0,
} //this will overwrite the existing elements
DeleteAtPosition(t,1): the position is already given:
pos=1, then shift until 1 by crushing pos=1
0 1 2 3 4 0 1 2 3 4
9 11 4 8 9 4 8
20
Modification functions
bool ModifyElement(ARRAY *t, int oldval, int newval){
int i=0;
if(IsEmpty(t))
return false;
for(int i=0; i<t->nbele; i++)
if(t->a[i]==oldval){//Find old value
t->a[i]=newval; //replace it
return true;
}
return false;
}

Find the old value, then replace it by the new one, otherwise
returns false. Here, we modify 4 by 20.
0 1 2 3 4 0 1 2 3 4
9 11 4 8 9 11 20 8
21
Modification functions (cont.)

bool ModifyAtPosition(ARRAY *t, int newval, int pos){


if(IsEmpty(t))
return false;
if(pos<0 || pos>=t->nbele)
return false;
t->a[pos]=newval;
return true;
}
Modify directly the old value by the new value according to
the position. Here, we modify the value of position 2 by 20.

0 1 2 3 4 0 1 2 3 4
9 11 4 8 9 11 20 8

22
IMPLEMENTATION: SORTED ARRAY

23
Sorted array
• Consider that the array is sorted in ascending order.
• What are the changes to do to the previous functions?
 Structure declaration • The following function should
does not change be implemented differently
 Non modified functions: – ModifyElement
 Initialize • A new function should be added
 DeleteAllElement – InsertElement
 DeleteElement • The following functions should
not be included
 DeleteAtPosition
– ModifyAtPosition
 IsEmpty et IsFull
– InsertAtHead
 IsInArray et FindElement
– InsertAtQueue
 GetElementNb
– InsertAtPosition
 PrintArray

24
Insertion Function
bool InsertElement(ARRAY *t, int val){
int pos;
if(IsFull(t))
return false;
//Find position
for(pos=0; pos<t->nbele; pos++)
if(t->a[pos] > val)  Find the good
break;
//shifting
insertion position to
for(int i=t->nbele; i>pos; i--)
maintain the array
t->a[i]=t->a[i-1]; sorted
//insertion  Shift the elements
t->a[pos]=val; stating from this
t->nbele++; position
return true;
}  Insert element

25
Insertion Function (cont.)
bool ModifyElement(ARRAY *t, int oldval, int newval){
int i=0, pos;
if(IsEmpty(t))
return false;
pos=FindElement(t,oldval);
if(pos==-1)
return false;
DeleteAtPosition(t,pos);
return InsertElement(t,newval);
}

 Find the position of the old value


 Remove it
 Call the previous insertion function

26
APPLICATION: ARRAY OF
STRUCTURES

27
An array containing an array of
structures
The same operations seen previously can be
applied to this case
The difference is that the stored data are
somehow complex
all the instructions related to data are modified
But, the same form of the algorithms remains
unchanged

28
Declaration
#define stdMAX 80
#define MAX 100
The structure
struct STUDENT{ ARRAY contains
char fname[stdMAX];
char lname[stdMAX];
now an array of
int id; structures STUDENT
float; GPA;
} STUDENT contains
struct ARRAY{ the information of a
STUDENT a[MAX];
int nbele; student
};

Implement the necessary 1- Initialization 4- Modification


functions to manage the array. 2- Insertion 5- Removal
Take the two cases: a sorted 3- Searching 6- Display
and unsorted array 29
Repesentation
ARRAY Structure

0 1 2 3 … 98 99

SUTDENT Structure a[0] a[1] a[2] …


nbele=3

0 1 2 3 … 98 99

Jean Denise Dario

Kopp Barillon Manara

111 222 333 …

3.5 3.2 2.75

nbele=3
30
//We consider a sorted array according to id
bool InsertStudent(ARRAY *t, const STUDENT *std){
int pos;
if(IsFull(t)) Insertion function
return false;
//find position
for(pos=0; pos<t->nbele; pos++)
if(t->a[pos].id > std->id)
break;
//shifting
for(int i=t->nbele; i>pos; i--){
strcpy(t->a[i].fname,t->a[i-1].fname);
strcpy(t->a[i].lname,t->a[i-1].lname);
t->a[i].id=t->a[i-1].id;
t->a[i].GPA=t->a[i-1].GPA; Attention: copying
} string of characters
//Insert at position is done with strcpy
strcpy(t->a[pos].fname,std->fname);
strcpy(t->a[pos].lname,std->lname);
t->a[pos].id=std->id;
t->a[pos].GPA=std->GPA;
t->nbele++;
return true;
} 31

You might also like