Introduction to Programming (Java) 9/11 · Introduction to Programming (Java) 2008/2009....

Post on 25-Jun-2020

46 views 0 download

Transcript of Introduction to Programming (Java) 9/11 · Introduction to Programming (Java) 2008/2009....

Introduction to Programming (Java) 9/11 1/39

Introduction to Programming (Java)9/11

Michal Krátký

Department of Computer ScienceTechnical University of Ostrava

Introduction to Programming (Java)2008/2009

Introduction to Programming (Java) 9/11 2/39

Linear Data Structures

Dynamic Array

Dynamic Memory Allocation

Variables of primitive data types are stored on a stackOperation system provides a memory, heap, for a program.This memory is allocated by the new operatorWe do not delete the allocated memory (like in the case ofC++), since Garbage collector frees the unused memoryA detection: There is no pointer to an unused memory

The positive: programmer does not care about thisThe negative: overhead is higher

Introduction to Programming (Java) 9/11 3/39

Linear Data Structures

Dynamic Array

Dynamic Array

In the case of a dynamic array, we must resize theallocated memory if the array overflows

This reallocation has high overhead. ⇒ It is not efficient toresize the array after the insertion of each item

Therefore, we handle these variables:capacity – the size of the allocated memorysize – the number of inserted items

Issues

Arrays for various data types

Introduction to Programming (Java) 9/11 4/39

Linear Data Structures

Dynamic Array

DynamicArray 1/5, createArray()

public class DynamicArray{

s t a t i c i n t INDEX_SIZE = 0 ;s t a t i c i n t INDEX_FIRST_ITEM = 1 ;

s t a t i c i n t [ ] c rea teAr ray ( i n t capac i t y ){

i n t ar ray [ ] = new i n t [ capac i t y ] ;/ / the f i r s t i tem inc ludes the ar ray s ize/ / ( the number o f i tems i n the ar ray )ar ray [ INDEX_SIZE ] = 0 ;return ar ray ;

}

Introduction to Programming (Java) 9/11 5/39

Linear Data Structures

Dynamic Array

Dynamic Array, Implementation

INDEX_SIZE

INDEX_FIRST_ITEM

array.length-1It means

capacity=array.length-1

Introduction to Programming (Java) 9/11 6/39

Linear Data Structures

Dynamic Array

Dynamic Array, Implementation

1

INDEX_SIZE

0insert(0):

2 0 1insert(1):

INDEX_SIZE

Introduction to Programming (Java) 9/11 7/39

Linear Data Structures

Dynamic Array

DynamicArray 2/5, insert()

public s t a t i c i n t [ ] i n s e r t ( i n t [ ] array , i n t i tem ){

i n t [ ] newArray = ar ray ;

i f ( a r ray . leng th <= ar ray [ INDEX_SIZE ]+1){

/ / a r ray over f lows = > res i ze i tnewArray = res i ze ( ar ray ) ;

}newArray [ INDEX_FIRST_ITEM + newArray [ INDEX_SIZE ] ] = i tem ;newArray [ INDEX_SIZE ]++ ;

/ / p o i n t e r to the ar ray would by changed , r e t u r n the p o i n t e rreturn newArray ;

}

Introduction to Programming (Java) 9/11 8/39

Linear Data Structures

Dynamic Array

DynamicArray 3/5, resize() and copy()

s t a t i c i n t [ ] r es i ze ( i n t [ ] a r ray ){

i n t [ ] newArray = new i n t [ 2 ∗ ar ray . leng th ] ;copy ( array , newArray ) ;return newArray ;

}

/ / copy a l l i tem from srcAr ray to destArrays t a t i c void copy ( i n t [ ] srcArray , i n t [ ] destArray ){

for ( i n t i = 0 ; i < srcAr ray . leng th ; i ++){

destArray [ i ] = srcAr ray [ i ] ;}

}

Introduction to Programming (Java) 9/11 9/39

Linear Data Structures

Dynamic Array

Dynamic Array, Resize

6

INDEX_SIZE

0 1 2 3 4 5

insert(6):

insert(5):

6 0 1 2 3 4 5

newArray

After copying

7 0 1 2 3 4 5 6After insertion

INDEX_SIZE

Notice: The old small array is destroyed by Garbage Collector

Introduction to Programming (Java) 9/11 10/39

Linear Data Structures

Dynamic Array

DynamicArray 4/5, print()

/ / p r i n t on ly i tems of t h i s ar rays t a t i c void p r i n t ( i n t [ ] a r ray ){

for ( i n t i = 0 ; i < ar ray [ INDEX_SIZE ] ; i ++){

System . out . p r i n t ( a r ray [ INDEX_FIRST_ITEM + i ] + " , " ) ;}System . out . p r i n t ( " \ n " ) ;

}

Introduction to Programming (Java) 9/11 11/39

Linear Data Structures

Dynamic Array

DynamicArray 5/5, main()

public s t a t i c void main ( S t r i n g [ ] args ){

f i n a l i n t capac i t y = 1 0 ;

i n t [ ] a r ray = createAr ray ( capac i t y ) ;

for ( i n t i = 0 ; i < 5 0 ; i ++){

ar ray = i n s e r t ( array , i ) ;}p r i n t ( a r ray ) ; / / r e s u l t : 0 , 1 , . . . 4 9

}}

Introduction to Programming (Java) 9/11 12/39

Linear Data Structures

Dynamic Sorted Array

Sorted Array

We can sort each item that is inserted into an array

In this case, we must search the appropriate item locationin the array

Sequence or binary searching may be applied

Introduction to Programming (Java) 9/11 13/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 1/6, createArray()

public class DynamicSortedArray{

s t a t i c i n t INDEX_SIZE = 0 ;s t a t i c i n t INDEX_FIRST_ITEM = 1 ;

s t a t i c i n t [ ] c rea teAr ray ( i n t capac i t y ){

i n t ar ray [ ] = new i n t [ capac i t y ] ;a r ray [ INDEX_SIZE ] = 0 ;return ar ray ;

}

Introduction to Programming (Java) 9/11 14/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 2/6, insert()

public s t a t i c i n t [ ] i n s e r t ( i n t [ ] array , i n t i tem ){

i n t [ ] newArray = ar ray ;

i f ( a r ray . leng th <= ar ray [ INDEX_SIZE ]+1){

/ / a r ray i s f u l l ? res i ze t h i s ar ray . . .newArray = res i ze ( ar ray ) ;

}

/ / s o r t t h i s i tem i n t o the ar rayi nse r tAndSor t ( newArray , i tem ) ;

/ / increment the number o f i tems i n the ar raynewArray [ INDEX_SIZE ]++ ;

return newArray ;}

Introduction to Programming (Java) 9/11 15/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 3/6, insertAndSort() 1/2

s t a t i c void i nse r tAndSor t ( i n t [ ] array , i n t i tem ){

/ / search the appropr ia te l o c a t i o ni n t index ;

i f ( a r ray [ INDEX_SIZE ] ! = 0 ){

index = ar ray [ INDEX_SIZE ] ;for ( i n t i = 0 ; i < ar ray [ INDEX_SIZE ] ; i ++){

i f ( i tem < ar ray [ INDEX_FIRST_ITEM + i ] ){

index = i ;break ;

}}

}else{

index = 0 ;}

Introduction to Programming (Java) 9/11 16/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 4/6, insertAndSort() 2/2

/ / s h i f t a l l g rea te r i temsfor ( i n t i = ar ray [ INDEX_SIZE ] ; i > index ; i−−){

a r ray [ INDEX_FIRST_ITEM + i ] = ar ray [ INDEX_FIRST_ITEM + i − 1 ] ;}

a r ray [ INDEX_FIRST_ITEM + index ] = i tem ;}

Introduction to Programming (Java) 9/11 17/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 5/6, resize() and print()

s t a t i c i n t [ ] r es i ze ( i n t [ ] a r ray ){

i n t [ ] newArray = new i n t [ 2 ∗ ar ray . leng th ] ;

/ / we can use t h i s method ins tead of ’ i tem by i tem ’ copyingSystem . arraycopy ( array , 0 , newArray , 0 , a r ray . leng th ) ;

return newArray ;}

s t a t i c void p r i n t ( i n t [ ] a r ray ){

for ( i n t i = 0 ; i < ar ray [ INDEX_SIZE ] ; i ++){

System . out . p r i n t ( a r ray [ INDEX_FIRST_ITEM + i ] + " , " ) ;}System . out . p r i n t ( " \ n " ) ;

}

Introduction to Programming (Java) 9/11 18/39

Linear Data Structures

Dynamic Sorted Array

DynamicSortedArray 6/6, main()

public s t a t i c void main ( S t r i n g [ ] args ){

f i n a l i n t CAPACITY = 1 0 ;f i n a l i n t COUNT = 5 0 ;

i n t [ ] a r ray = createAr ray (CAPACITY ) ;

for ( i n t i = COUNT−1 ; i > = 0 ; i −−){

a r ray = i n s e r t ( array , i ) ;}p r i n t ( a r ray ) ; / / Resul t : 0 , 1 , . . . , 4 9

}}

Introduction to Programming (Java) 9/11 19/39

Linear Data Structures

Queue

Queue

Queue is a FIFO (first in, first out) data structureIt means, the first removed item is the first added item ofthe queue

Operations

Put – item insertionGet – item removingEmpty – test of the empty queue

If there is no memory for the next added item, overflowappears. On the other hand, if we want to get item from theempty queue, underflow appears.

Introduction to Programming (Java) 9/11 20/39

Linear Data Structures

Queue

Queue, Implementation

We need two pointersThe first pointer, head, references the queue’s first item.This item is retrieved when the Get operation is invoked.The second pointer, tail, references the queue’s last item.

head tail

Introduction to Programming (Java) 9/11 21/39

Linear Data Structures

Queue

Queue 1/5

public class Queue{

public s t a t i c i n t INDEX_TAIL = 0 ;public s t a t i c i n t INDEX_FIRST_ITEM = 1 ;

public s t a t i c i n t [ ] createQueue ( i n t capac i t y ){

i n t [ ] a r ray = new i n t [ capac i t y ] ;a r ray [ INDEX_TAIL ] = 0 ;return ar ray ;

}

Introduction to Programming (Java) 9/11 22/39

Linear Data Structures

Queue

Queue 2/5, put()

public s t a t i c boolean put ( i n t [ ] arrayQueue , i n t i tem ){

boolean r e t = fa lse ;i f ( arrayQueue [ INDEX_TAIL ] < arrayQueue . length −1){

arrayQueue [ INDEX_TAIL ]++ ;arrayQueue [ arrayQueue [ INDEX_TAIL ] ] = i tem ;r e t = true ;

}return r e t ;

}

Introduction to Programming (Java) 9/11 23/39

Linear Data Structures

Queue

Queue, Implementation

1

INDEX_TAIL

10put(10)

2

INDEX_TAIL

10 20put(20)

1

INDEX_TAIL

20get()

In our implementation head = 1

Introduction to Programming (Java) 9/11 24/39

Linear Data Structures

Queue

Queue 3/5, get()

public s t a t i c i n t get ( i n t [ ] arrayQueue ){

i n t value ;

i f ( ! empty ( arrayQueue ) ){

value = arrayQueue [ INDEX_FIRST_ITEM ] ;for ( i n t i =INDEX_FIRST_ITEM ; i <arrayQueue [ INDEX_TAIL ] ; i ++){

arrayQueue [ i ] = arrayQueue [ i + 1 ] ;}arrayQueue [ INDEX_TAIL]−−;

}else{

value = −1;}return value ;

}

Introduction to Programming (Java) 9/11 25/39

Linear Data Structures

Queue

Queue 4/5, empty()

public s t a t i c boolean empty ( i n t [ ] arrayQueue ){

boolean r e t = fa lse ;i f ( arrayQueue [ INDEX_TAIL ] < = INDEX_TAIL ){

r e t = true ;}return r e t ;

}

Introduction to Programming (Java) 9/11 26/39

Linear Data Structures

Queue

Queue 5/5, main()

public s t a t i c void main ( S t r i n g [ ] args ){

f i n a l i n t CAPACITY = 1 0 ;i n t [ ] arrayQueue = createQueue (CAPACITY ) ;put ( arrayQueue , 1 0 ) ;put ( arrayQueue , 2 0 ) ;System . out . p r i n t l n ( get ( arrayQueue ) ) ; / / 10System . out . p r i n t l n ( empty ( arrayQueue ) ) ; / / f a l s e

put ( arrayQueue , 3 0 ) ;System . out . p r i n t l n ( get ( arrayQueue ) ) ; / / 20System . out . p r i n t l n ( get ( arrayQueue ) ) ; / / 30

System . out . p r i n t l n ( get ( arrayQueue ) ) ; / / −1System . out . p r i n t l n ( empty ( arrayQueue ) ) ; / / t r ue

}}

Introduction to Programming (Java) 9/11 27/39

Linear Data Structures

Queue

Queue

Issues

Queues for various data types

Problems

Complexity of the Get operation: O(n).We can solve this by cyclic queue

head tail headtail

Application

A buffer for data exchange

Introduction to Programming (Java) 9/11 28/39

Linear Data Structures

Stack

Stack

Stack is a LIFO (last-in, first out) data structure

It means, the first removed item is the last inserted item

Top: a pointer to the last inserted item

Bottom: a pointer to the last item in a stack

Introduction to Programming (Java) 9/11 29/39

Linear Data Structures

Stack

Stack, Operations

Push – insertion of the item in topPop – removing item in topEmpty – test of empty stackTop – return of the item in top

If a stack is empty and the Pop method is invocated, underflowappears. If it is not able to insert the next item, overflowappears.

Obviously, operations have a constant complexity.

Introduction to Programming (Java) 9/11 30/39

Linear Data Structures

Stack

Stack, Operations

a) Push(15); Push(6); Push(2); Push(9);

b) Push(17); Push(3);

c) Pop();

Introduction to Programming (Java) 9/11 31/39

Linear Data Structures

Stack

Stack 1/7, createStack()

public class Stack {s t a t i c f i n a l i n t INDEX_TOP = 0 ;s t a t i c f i n a l i n t INDEX_FIRST_ITEM = 1 ;

s t a t i c i n t [ ] c reateStack ( i n t capac i t y ){

i n t arrayStack [ ] = new i n t [ capac i t y ] ;ar rayStack [ INDEX_TOP ] = 0 ;return arrayStack ;

}

Introduction to Programming (Java) 9/11 32/39

Linear Data Structures

Stack

Stack 2/7, push()

s t a t i c boolean push ( i n t [ ] arrayStack , i n t value ){

boolean r e t = fa lse ;i f ( ar rayStack [ INDEX_TOP] < arrayStack . length −1){

ar rayStack [ ar rayStack [ INDEX_TOP ] + INDEX_FIRST_ITEM ] = value ;ar rayStack [ INDEX_TOP]++ ;r e t = true ;

}return r e t ;

}

Introduction to Programming (Java) 9/11 33/39

Linear Data Structures

Stack

Stack, Implementation

1

INDEX_TOP

0push(0)

2 0 1push(1)

INDEX_TOP

1 0pop()

INDEX_TOP

Introduction to Programming (Java) 9/11 34/39

Linear Data Structures

Stack

Stack 3/7, empty()

s t a t i c boolean empty ( i n t [ ] ar rayStack ){

boolean r e t = true ;i f ( ar rayStack [ INDEX_TOP] > 0 ){

r e t = fa lse ;}return r e t ;

}

Introduction to Programming (Java) 9/11 35/39

Linear Data Structures

Stack

Stack 4/7, top()

s t a t i c i n t top ( i n t [ ] ar rayStack ){

i n t value = −1;i f ( ! empty ( ar rayStack ) ){

value = arrayStack [ ar rayStack [ INDEX_TOP ] ] ;}return value ;

}

Introduction to Programming (Java) 9/11 36/39

Linear Data Structures

Stack

Stack 5/7, pop()

s t a t i c i n t pop ( i n t [ ] ar rayStack ){

i n t value = −1;i f ( ! empty ( ar rayStack ) ){

value = arrayStack [ ar rayStack [ INDEX_TOP ] ] ;ar rayStack [ INDEX_TOP]−−;

}return value ;

}

Introduction to Programming (Java) 9/11 37/39

Linear Data Structures

Stack

Stack 6/7, main() 1/2

public s t a t i c void main ( S t r i n g args [ ] ){

f i n a l i n t CAPACITY = 1 0 ;

i n t arrayStack [ ] = createStack (CAPACITY ) ;

for ( i n t i = 0 ; i < 1 2 ; i ++){

System . out . p r i n t ( push ( arrayStack , i ) + " , " ) ;}

System . out . p r i n t l n ( ) ;

/ / Resul t :/ / t rue , t rue , t rue , t rue , t rue , t rue , t rue , t rue , t rue ,/ / fa l se , fa l se , f a l s e

Introduction to Programming (Java) 9/11 38/39

Linear Data Structures

Stack

Stack 7/7, main() 2/2

for ( i n t i = 0 ; i < 1 2 ; i ++){

System . out . p r i n t ( pop ( arrayStack ) + " , " ) ;}

System . out . p r i n t l n ( ) ;/ / Resul t :/ / 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , −1 , −1 , −1

}}

Introduction to Programming (Java) 9/11 39/39

Linear Data Structures

Stack

Stack

Issues

Dynamic stackStacks for various data types

Application

Replacement of recursion (QuickSort, tree searching,compilers and so on)Storage of invoked method parameters