abstract data types sir joseph lindo university of the cordilleras

45
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras

Upload: melinda-oliver

Post on 30-Dec-2015

23 views

Category:

Documents


0 download

DESCRIPTION

Abstract Data Types Sir Joseph Lindo University of the Cordilleras. Abstract Data Types. Array (A review). Array. ADT. ADT. a contiguous block of memory, divided into a number of slots stores multiple data items of the same data type. List. Stack. Queue. Case. Abstract Data Types. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

AbstractDataTypesSir Joseph Lindo

University of the Cordilleras

Page 2: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Abstract Data TypesArray (A review)

• a contiguous block of memory, divided into a number of slots

• stores multiple data items of the same data type

List

Stack

Queue

Case

ADTArray

ADT

Page 3: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

ADT

Abstract Data TypesAbstract DataType• a design tool• concern on the important

concept or model• access is restricted

• Array is an ADT?

List

Stack

Queue

Case

ArrayADT

Page 4: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

ADT

Abstract Data TypesAbstract DataType• List• Stack• Queue

List

Stack

Queue

Case

ArrayADT

Page 5: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Abstract Data TypesList

• Array List• Linked List• Doubly Linked List• Circular Linked list

List

Stack

Queue

Case

ArrayADT

List

Page 6: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

AbstractDataTypes

-- end --Sir Joseph Lindo

University of the Cordilleras

Page 7: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

AbstractDataTypes-- end na --Sir Joseph Lindo

University of the Cordilleras

Page 8: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

1D Array

Array (A review)

1-dimensional array x = [a, b, c, d]map into contiguous memory locations

Memorya b c d

start

location(x[i]) = start + i

Page 9: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

2D Array

Array (A review)

The elements of a 2-dimensional array a declared as:

int [][]a = new int[3][4];

may be shown as a tablea[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3]

Page 10: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

2D Array

Array (A review)

view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l]and store as 4 1D arrays

2-dimensional array x a, b, c, d

e, f, g, hi, j, k, l

Page 11: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

2D Array to 1D

Array (A review)

x.length = 3x[0].length = x[1].length = x[2].length = 4

a b c d

e f g h

i j k l

x[]

Page 12: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Locating Element x[i][j]

Array (A review)

• assume x has r rows and c columns• each row has c elements• i rows to the left of row i• so ic elements to the left of x[i][0]• so x[i][j] is mapped to position ic + j of the 1D array

row 0 row 1 row 2 … row i

0 c 2c 3c ic

Page 13: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array is an ADT?

ADT

• In an array any item can be accessed, while in ADT access is restricted.

• ADT are more abstract than arrays.

Page 14: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array List

List

• The Array List ADT extends the notion of array by storing a sequence of arbitrary objects

Page 15: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array List

List

• An element can be accessed, inserted or removed by specifying its index (number of elements preceding it)

• An exception is thrown if an incorrect index is given (e.g., a negative index)

Page 16: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array List

List

Applications:• Direct applications

Sorted collection of objects (elementary database)

• Indirect applicationsAuxiliary data

structure for algorithmsComponent of other

data structures

Page 17: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array-Based Implementation

List

Use an array A of size NA variable n keeps track of the size of the

array listOperation get(i) is implemented in

returning A[i]Operation set(i,o) is implemented

performing t = A[i], A[i] = o, and returning t.

Page 18: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array-Based Implementation

List

InsertionIn operation add(i, o), we need to make

room for the new element by shifting forward the n – I elements A[i], …, A[n - 1]

Page 19: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array-Based Implementation

List

DeletionIn operation remove(i), we need to fill the

hole left by the removed element by shifting backward the n - i – 1 elements A[i + 1], …, A[n - 1]

Page 20: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Array-Based Implementation

List

Main Methods:get(integer i): returns the elementset(integer i, object o): replace the element

at index i with o and return the old elementadd(integer i, object o): insert a new

element o to have index iremove(integer i): removes and returns the

element at index i

Page 21: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Linked List

List

• Various cells of memory are not allocated consecutively in memory.

• Not enough to store the elements of the list.• With arrays, the second element was right next

to the first element.• Now the first element must explicitly tell us

where to look for the second element.• Do this by holding the memory address of the

second element

Page 22: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Linked List

List

Create a structure called a Node.

• The objectfield will hold the actual list element.• The nextfield in the structure will hold the

starting location of the next node.• Chain the nodes together to form a linkedlist.

Page 23: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Linked List

List

Picture of our list (2, 6, 7, 8, 1) stored as a linked list:

Page 24: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Linked List

List

Actual picture in memory:

Page 25: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Doubly-Linked List

List

• Moving forward in a singly-linked list is easy; moving backwards is not so easy.

• To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node:

Page 26: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Doubly-Linked List

List

Page 27: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Circular - Linked List

List

• The next field in the last node in a singly-linked list is set to NULL.

• Moving along a singly-linked list has to be done in a watchful manner.

• Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.

• A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.

Page 28: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Circular - Linked List

List

• Two views of a circular linked list:

Page 29: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

• A case where circularly linked list comes in handy is the solution of the Josephus Problem.

• Consider there are 10 persons. They would like to choose a leader.

• The way they decide is that all 10 sit in a circle.• They start a count with person 1 and go in

clockwise direction and skip 3. Person 4 reached is eliminated.

Page 30: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

• The count starts with the fifth and the next person to go is the fourth in count.

• Eventually, a single person remains.

Page 31: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 32: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 33: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 34: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 35: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 36: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 37: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 38: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 39: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 40: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

List

Page 41: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

Comprehension Check

• Given fifteen person named: Akang, Bebeng, Chito, Dingdong, Eman, Feng, Gina, Henry, Ikong, Joseph, Kekang, Lina, Neneng, Mario, Omar

• They are arranged in the same manner as mentioned above

Page 42: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

Comprehension Check

• Using the circular-linked list and the Josephus problem

• Using 5 movements

• Answer the questions in the next slide.

• Show the complete solution/cycle

Page 43: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

Josephus Problem

Comprehension Check

1. Which of the persons will be selected as the leader?

2. Which of them will be the third to be eliminated?3. Which of them will be the 10th to be eliminated?4. Which of them will be the 7th to be eliminated?5. Which of them will be the 13th to be eliminated?

Page 44: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

AbstractDataTypes

-- end --Sir Joseph Lindo

University of the Cordilleras

Page 45: Abstract Data Types Sir Joseph  Lindo University of the Cordilleras

Joseph Lindo

AbstractDataTypes-- end na --Sir Joseph Lindo

University of the Cordilleras