450 likes | 559 Views
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.
E N D
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 Abstract Data Type Array ADT ADT • a design tool • concern on the important concept or model • access is restricted • Array is an ADT? List Stack Queue Case
Abstract Data Types Abstract Data Type Array ADT ADT • List • Stack • Queue List Stack Queue Case
Abstract Data Types List Array • Array List • Linked List • Doubly Linked List • Circular Linked list ADT List List Stack Queue Case
Abstract Data Types -- end -- Sir Joseph Lindo University of the Cordilleras
Abstract Data Types -- end na -- Sir Joseph Lindo University of the Cordilleras
Memory start Array (A review) 1D Array a b c d 1-dimensional array x = [a, b, c, d] map into contiguous memory locations location(x[i]) = start + i
Array (A review) 2D Array The elements of a 2-dimensional array a declared as: int [][]a = new int[3][4]; may be shown as a table a[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]
Array (A review) 2D Array 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l 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
x[] a b c d e f g h i j k l Array (A review) 2D Array to 1D x.length = 3 x[0].length = x[1].length = x[2].length = 4
0 c 2c 3c ic row 0 row 1 row 2 … row i Array (A review) Locating Element x[i][j] 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
ADT Array is an ADT? • In an array any item can be accessed, while in ADT access is restricted. • ADT are more abstract than arrays.
List Array List • The Array List ADT extends the notion of array by storing a sequence of arbitrary objects
List Array 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)
List Array List • Applications: • Direct applications • Sorted collection of objects (elementary database) • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures
List Array-Based Implementation Use an array A of size N A variable n keeps track of the size of the array list Operation get(i) is implemented in returning A[i] Operation set(i,o) is implemented performing t = A[i], A[i] = o, and returning t.
List Array-Based Implementation Insertion In 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]
List Array-Based Implementation Deletion In 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]
List Array-Based Implementation Main Methods: get(integer i): returns the element set(integer i, object o): replace the element at index i with o and return the old element add(integer i, object o): insert a new element o to have index i remove(integer i): removes and returns the element at index i
List Linked 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
List Linked 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.
List Linked List Picture of our list (2, 6, 7, 8, 1) stored as a linked list:
List Linked List Actual picture in memory:
List Doubly-Linked 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:
List Doubly-Linked List
List Circular - Linked 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.
List Circular - Linked List • Two views of a circular linked list:
List Josephus Problem • 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.
List Josephus Problem • The count starts with the fifth and the next person to go is the fourth in count. • Eventually, a single person remains.
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
List Josephus Problem
Comprehension Check Josephus Problem • 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
Comprehension Check Josephus Problem • Using the circular-linked list and the Josephus problem • Using 5 movements • Answer the questions in the next slide. • Show the complete solution/cycle
Comprehension Check Josephus Problem • Which of the persons will be selected as the leader? • Which of them will be the third to be eliminated? • Which of them will be the 10th to be eliminated? • Which of them will be the 7th to be eliminated? • Which of them will be the 13th to be eliminated?
Abstract Data Types -- end -- Sir Joseph Lindo University of the Cordilleras
Abstract Data Types -- end na -- Sir Joseph Lindo University of the Cordilleras