1 / 45

Abstract Data Types Sir Joseph Lindo University of the Cordilleras

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.

Download Presentation

Abstract Data Types Sir Joseph Lindo University of the Cordilleras

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract Data Types Sir Joseph Lindo University of the Cordilleras

  2. 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

  3. 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

  4. Abstract Data Types Abstract Data Type Array ADT ADT • List • Stack • Queue List Stack Queue Case

  5. Abstract Data Types List Array • Array List • Linked List • Doubly Linked List • Circular Linked list ADT List List Stack Queue Case

  6. Abstract Data Types -- end -- Sir Joseph Lindo University of the Cordilleras

  7. Abstract Data Types -- end na -- Sir Joseph Lindo University of the Cordilleras

  8. 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

  9. 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]

  10. 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

  11. 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

  12. 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

  13. 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.

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

  15. 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)

  16. List Array List • Applications: • Direct applications • Sorted collection of objects (elementary database) • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures

  17. 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.

  18. 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]

  19. 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]

  20. 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

  21. 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

  22. 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.

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

  24. List Linked List Actual picture in memory:

  25. 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:

  26. List Doubly-Linked List

  27. 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.

  28. List Circular - Linked List • Two views of a circular linked list:

  29. 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.

  30. 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.

  31. List Josephus Problem

  32. List Josephus Problem

  33. List Josephus Problem

  34. List Josephus Problem

  35. List Josephus Problem

  36. List Josephus Problem

  37. List Josephus Problem

  38. List Josephus Problem

  39. List Josephus Problem

  40. List Josephus Problem

  41. 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

  42. 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

  43. 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?

  44. Abstract Data Types -- end -- Sir Joseph Lindo University of the Cordilleras

  45. Abstract Data Types -- end na -- Sir Joseph Lindo University of the Cordilleras

More Related