introduction data structure

44
KPD8013 PROGRAMMING III

Upload: muhammad-ismail

Post on 21-Jan-2018

56 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Introduction data structure

KPD8013

PROGRAMMING III

Page 2: Introduction data structure

INTRODUCTION to course

• Credit Hour: 3.0

• Contact Hour: 5 hours

Tuesday (8.00 – 1.30 pm)

Page 3: Introduction data structure

1.0 Data Structures 1.1 Introduction to Data

Structures

Page 4: Introduction data structure

1.1.1 Define Data Structures

• A data structure is a scheme for organizing data in the memory of a computer.

• A way of storing and organizing data in a computer so that it can be used efficiently

Page 5: Introduction data structure

1.1.1 Define Data Structures

• Some of the more commonly used data structures include lists, arrays, stacks, queues, heaps, trees, and graphs.

• The way in which the data is organized affects the performance of a program for different tasks.

Page 6: Introduction data structure

1.1.1 Define Data Structures

• Computer programmers decide which data structures to use based on the nature of the data and the processes that need to be performed on that data.

Page 7: Introduction data structure

Array

Linked List

Tree Queue Stack

1.1.1 Define Data Structures

Types of data structures

Page 8: Introduction data structure

1.1.1 Define Data Structures

The Need for Data Structures • Goal: to organize data

• Criteria: to facilitate efficient storage of data retrieval of data manipulation of data

Page 9: Introduction data structure

1.1.1 Define Data Structures

The Need for Data Structures

• Design Issue:

select and design appropriate data types (This is the main motivation to learn and understand data structures)

Page 10: Introduction data structure

1.1.1 Define Data Structures

Data Structure Operations

• Traversing Accessing each data element exactly once so that certain items in

the data may be processed

• Searching Finding the location of the data element (key) in the structure

• Insertion Adding a new data element to the structure

Page 11: Introduction data structure

1.1.1 Define Data Structures

Data Structure Operations • Deletion

Removing a data element from the structure

• Sorting Arrange the data elements in a logical order (ascending/

descending)

• Merging Combining data elements from two or more data structures into

one

Page 12: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack

• Based on the principle of Last In First Out (LIFO)

• Used extensively at every level of a modern computer system (compiler etc.)

Page 13: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack

• It is named stack as it behaves like a real-world stack. For example – a stack of books or a pile of plates, etc.

Page 14: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Representation

• The following diagram depicts a stack and its operations.

Page 15: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Operations

• Peek: If the stack is not empty, return its top element.

• Pop: If the stack is not empty, delete and return its top element.

Page 16: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Operations

• Push: Add a given element to the top of the stack.

• Size: Return the number of elements in the stack.

Page 17: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

UML Diagram

Translate into a Java interface Stack.java

<<interface>> Stack

+peek(): Object +pop(): Object +push(Object) +size(): int

Page 18: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Example: … Stack crates = new ArrayStack(4); crates.push(“CARROT”); crates.push(“ORANGES”); crates.push(“RAISIN”); crates.pop(); crates.pop(); crates.push(“PEACH”); crates.push(“BANANAS”); crates.pop();

Page 19: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Example:

CARROT ORANGE

CARROT ORANGE

CARROT

RAISIN ORANGE

CARROT

CARROT PEACH

CARROT PEACH

CARROT

BANANAS

Page 20: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Exercise:

Stack stack= new ArrayStack(); stack.push(“Monday”); stack.push(“Tuesday”); stack.push(“Wednesday”); stack.pop(); stack.pop(); stack.push(“Thursday”);

stack.push(“Friday”);

stack.pop();

stack.push(“Saturday”);

stack.push(“Sunday”);

stack.pop();

stack.pop();

Page 21: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Answer:

Thursday

Monday

Page 22: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation

• Several way to implement stack interface.

• Simplest way is to use an ordinary array.

Page 23: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation

<<interface>> Stack

+peek() :Object +pop() :Object +push(Object) +size() :int

ArrayStack

-a :Object[] -size :int

+ArrayStack(int) +isEmpty():boolean +peek() :Object +pop() :Object +push(Object) +size() :int -resize()

Page 24: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation

Stack.java

ArrayStack.java

TestArrayStack.java

Page 25: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• ArrayStack implementation uses a backing array a[] to store the stack’s elements.

• Its other field is the integer size, which keeps a count of the number of elements in the stack.

Page 26: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• In addition to the four methods required by the interface, the class also includes a public isEmpty() method and a private resize() method.

Page 27: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• The latter is called by push() when the array is full.

• Its rebuild the array, doubling its size.

• This is done by declaring a temporary reference aa[] to the array in line 35.

Page 28: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• Redefining a[] as a new array with twice the length in line 36.

• Using the arraycopy() method of the system class to copy all the stack elements from aa[] back to a[] in line 37.

Page 29: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• The peek() and pop() methods required that the stack be not empty.

• This constrain enforce at line 14 and 19.

Page 30: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• Note at line 21 the pop() method resets to null the array element that references the object being remove from the stack.

Page 31: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Array Implementation (refer to ArrayStack.java)

• This prevents an unreachable reference from being maintain.

• This class is tested in TestArrayStack.java

Page 32: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue

• is a data structure that implements the first-in-first-out (FIFO) protocol.

• The only access point in the structure are at its ends where the elements are inserted and removed.

Page 33: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue

• Insertions are always made at the back of the queue.

• Removals are always made at the front.

Page 34: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue

• The queue data structure abstracts the notion of an ordinary waiting line such as a line of people waiting to purchase admission tickets.

Page 35: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue Operations

• Add: Insert a given element at the back of the queue.

• First: If the queue is not empty, return the element that is at the front of the queue.

Page 36: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue Operations

• Remove: If the queue is not empty, delete and return the element that is at the front of the queue.

• Size: Return the number of elements in the queue.

Page 37: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue Operations

Translate into a Java interface Queue.java

<<interface>> Queue

+add(object) +first(): object +remove(): object +size(): int

Page 38: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue Example: … Queue queue = new ArrayQueue(); queue.add(“CARROT”); queue.add(“ORANGES”); queue.add(“RAISIN”); queue.remove(); queue.remove();

queue.add(“PEACH”);

queue.add(“BANANAS”);

queue.remove();

queue.add(“DURIAN”);

queue.add(“GRAPES”);

queue.remove();

queue.remove();

Page 39: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Example:

CARROT ORANGE CARROT RAISIN ORANGE CARROT

RAISIN ORANGE RAISIN PEACH RAISIN

BANANAS PEACH RAISIN BANANAS PEACH

DURIAN BANANAS PEACH

DURIAN BANANAS PEACH GRAPES

Page 40: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Stack Example:

DURIAN BANANAS GRAPES

DURIAN GRAPES

Page 41: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming Queue Exercise: … Queue queue = new ArrayQueue(); queue.add(“MONDAY”); queue.add(“TUESDAY”); queue.add(“SUNDAY”); queue.remove(); queue.remove();

queue.add(“WEDNESDAY”); queue.add(“SATURDAY”); queue.remove(); queue.remove(); queue.add(“THURSDAY”); queue.add(“FRIDAY”); queue.remove(); queue.remove();

Page 42: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

Queue Answer:

Friday

Page 43: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Queue Implementation

• Several way to implement queue interface.

• Simplest way is to use an array as we did with the ArrayStack implementation of the Stack interface.

Page 44: Introduction data structure

1.1.2 Apply Stacks, Queues, Linked Lists

in programming

An Queue Implementation

• The simplest alternative way to an array implementation is a linked implementation.