Joseph Lindo
AbstractDataTypesSir 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
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
Joseph Lindo
ADT
Abstract Data TypesAbstract DataType• List• Stack• Queue
List
Stack
Queue
Case
ArrayADT
Joseph Lindo
Abstract Data TypesList
• Array List• Linked List• Doubly Linked List• Circular Linked list
List
Stack
Queue
Case
ArrayADT
List
Joseph Lindo
AbstractDataTypes
-- end --Sir Joseph Lindo
University of the Cordilleras
Joseph Lindo
AbstractDataTypes-- end na --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
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]
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
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[]
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
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.
Joseph Lindo
Array List
List
• The Array List ADT extends the notion of array by storing a sequence of arbitrary objects
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)
Joseph Lindo
Array List
List
Applications:• Direct applications
Sorted collection of objects (elementary database)
• Indirect applicationsAuxiliary data
structure for algorithmsComponent of other
data structures
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.
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]
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]
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
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
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.
Joseph Lindo
Linked List
List
Picture of our list (2, 6, 7, 8, 1) stored as a linked list:
Joseph Lindo
Linked List
List
Actual picture in memory:
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:
Joseph Lindo
Doubly-Linked List
List
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.
Joseph Lindo
Circular - Linked List
List
• Two views of a circular linked list:
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.
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.
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
Joseph Lindo
Josephus Problem
List
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
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
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?
Joseph Lindo
AbstractDataTypes
-- end --Sir Joseph Lindo
University of the Cordilleras
Joseph Lindo
AbstractDataTypes-- end na --Sir Joseph Lindo
University of the Cordilleras