KPD8013 PROGRAMMING III
KPD8013
PROGRAMMING III
INTRODUCTION to course
• Credit Hour: 3.0
• Contact Hour: 5 hours
Tuesday (8.00 – 1.30 pm)
1.0 Data Structures 1.1 Introduction to Data
Structures
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
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.
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.
Array
Linked List
Tree Queue Stack
1.1.1 Define Data Structures
Types of data structures
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
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)
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
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
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.)
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.
1.1.2 Apply Stacks, Queues, Linked Lists
in programming
Stack Representation
• The following diagram depicts a stack and its operations.
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.
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.
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
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();
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
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();
1.1.2 Apply Stacks, Queues, Linked Lists
in programming
Stack Answer:
Thursday
Monday
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.
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()
1.1.2 Apply Stacks, Queues, Linked Lists
in programming
An Array Implementation
Stack.java
ArrayStack.java
TestArrayStack.java
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
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
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();
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
1.1.2 Apply Stacks, Queues, Linked Lists
in programming
Stack Example:
DURIAN BANANAS GRAPES
DURIAN GRAPES
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();
1.1.2 Apply Stacks, Queues, Linked Lists
in programming
Queue Answer:
Friday
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.
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.