1 / 19

Coen 352 – summer 2019

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

ludlow
Download Presentation

Coen 352 – summer 2019

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. Coen 352 – summer 2019 Tutorial Session 2

  2. Outline • Queue and stacks: • List implementation • Array implementation • Exercise on stack and queue: implementation • Binary search

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

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

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

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

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

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

  9. Worst case time complexity linear data structure

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

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

  12. BINARY SEARCH: WITH EXAMPLE • Conditions: the array should be sorted • Output: the position of the element or -1 if the element does not exist

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

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

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

More Related