190 likes | 293 Views
Coen 352 – summer 2019. Tutorial Session 2. Outline. Queue and stacks: List implementation Array implementation Exercise on stack and queue: implementation Binary search. Stack and queue. Stack: examine the item most recently add LIFO= Last In First Out
E N D
Coen 352 – summer 2019 Tutorial Session 2
Outline • Queue and stacks: • List implementation • Array implementation • Exercise on stack and queue: implementation • Binary search
Stack and queue • Stack: examine the item most recently add LIFO= Last In First Out • Queue: examine the item least recently add FIFO= First In First Out
Stack Array implementation • simple way to implement stack • Add element from left to right • Keep track of the index of the top element • Problem. Requiring client to provide capacity does not implement (a good) API! Solution with Resizing array: If array is full, create a new array of twice the size, and copy items. halve size of array when array is one-quarter full
Stack singly linked list implementation • Need a class to represent a node in the list Class Node { Type data; Node next; } • The list keep track of the head
Stack implementation (con’t) Class List{ Node first; // all operations } • Add element from right to left. • Nb: can use doubly linked list to implement stack
Queue: array implementation • Simple implementation • Need to keep track of the index of the front and the rear With a simple array when we dequeue we need to shift all the elements in the front Solution: Make the array circular or use a list !!!!!!!
Queue: list implementation • Use doubly linked list. Class Node { Type data; Node next; Node previous;} • Need to keep track of the front and rear of the list: Class List{ Node first; Node rear; // all operations}
Exercise 1 Create a class called MyStack to implement the stack using array representation. You need an internal array, choose any size to your stack, the constructor should initialize the array representing your stack . Implement pop and push. Populate your stack with values from the command line arguments Print the sum of the elements in your stack and the maximum in your stack What is the complexity of finding the maximum?
Exercise 2 Same as exercise 1, but in this case we will implement a queue with a doubly linked list. Implement the enqueue and dequeue Print the sum of the elements in your queue and the minimum in your queue What is the complexity of finding the maximum?
BINARY SEARCH: WITH EXAMPLE • Conditions: the array should be sorted • Output: the position of the element or -1 if the element does not exist
Exercise Implement the binary search using recursion using : • Array as data structure • LinkedList as data structure (use the linkedlist from java library ) • You should use the command line argument to populate your array
ANALYSIS OF THE COMPLEXITY • Test your algorithm with array/ list of different sizes (5 10 100 1000 ) and print the times its take: LinkedList vs array
Some links Queue with circular array • http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/8-List/array-queue2.html • https://www.javainuse.com/java/circular_java Learn java: • https://www.w3schools.com/java/