Presentation is loading. Please wait.

Presentation is loading. Please wait.

ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.

Similar presentations


Presentation on theme: "ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists."— Presentation transcript:

1 ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists

2 Elementary Data Structures zUsed as programming tools (stacks & queues) - software (i.e. compilers) - used in the operations of a certain task zImplemented in CPU instructions - hardware

3 Stacks zDefinition Õ container of objects Õ uses LIFO principle for object operations Õ operations are push and pop zSample uses : Õ Internet browsers Õ Undo / Redo feature

4 Other Applications zParsing - check for matching parenthesis or brackets - aid for algorithms applied to complex data structures (traverse nodes of a tree and searching vertices of a graph)

5 Basic Stack Operations zpush(o) zpop() zInserts object o onto top of stack Input: object Output: none zRemoves the top object of stack and returns it to calling method Input: none Output: object

6 Related Stack Operations zisEmpty() zisFull() zReturns a boolean indicating if stack is empty. Input: none Output: boolean zReturns a boolean indicating if stack is full Input: none Output: boolean

7 Other Stack Operations zsize() : ztop() zReturns the number of objects in stack. Input: none Output: integer zReturns the top object of the stack. Input: none Output: object

8 An Array-Based Stack zCreate an array A of size N zStore elements of stack S in array A zLet t (integer) - index of the top element of stack S S 0 1 2 3 t... N-1

9 Array Implementation zAlgorithm size() return t + 1 zAlgorithm isempty() return (t<0) zAlgorithm push(o) if size()=N then throw a STACKEMPTYEXCEPTION t=t+1 S[t]=o

10 Array Implementation zAlgorithm pop() if isempty() then throw a STACKEMPTYEXCEPTION e=S[t] S[t]=null t=t-1 return e zAlgorithm top() if isempty() then throw a STACKEMPTYEXCEPTION return S[t]

11 Class Problem zUsing the stack functions, make an algorithm that will determine that given a string x : a) has matching operands (i.e. (),{},[]) b) report an error if an operand is missing zExample : x = “(12xxs3e)”, correct x = “(sdfsdfs}”, error

12 Class Problem - Summary 1. Make an empty stack 2. Read characters until end of file 3. If the character is an open anything, push it in the stack 4. If it’s a close anything,then if stack is empty, report an error, otherwise, pop the stack 5. If popped symbol is not the corresponding opening symbol, then report an error 6. At end of file, if stack is not empty report an error

13 Stacks- other applications zRecursion - i.e. computing for N factorial zOperand parsing - evaluating an expression - i.e. ((4*5)+(6+2)/5)) - postfix notation A*B+C = AB*C+ zfunction calls - variables and routine position are saved

14 Stacks - Query zHow can the undo/redo function be implemented using stacks ?

15 QUEUES zA queue is a container of objects that are inserted and removed according to the FIFO principle. zOperations: Enqueue - insert an item at the rear of queue z Dequeue - remove an item from the front of the queue zSample uses : movie line printing queue

16 The Queue Abstract Data Type zenqueue(o) zdequeue() zInsert object o at the rear of the queue Input: object Output: none zRemove the object from the front of the queue and return it. Input: none Output: object

17 The Queue Abstract Data Type zsize() :Returns the number of objects in the queue. Input: noneOutput: object zisEmpty(): Return a boolean indicating if the queue is empty. Input: noneOutput:boolean zfront(): Return the front object of the queue. z Input: noneOutput: object

18 Array Implementation zCreate a queue using an array in a circular fashion zQueue consists of an N-element array Q and two integer variables: f : index of the front element r: index of the element after the rear one zConfigurations : “normal” “wrapped around”

19 Queue Implementation f r Array Q f is an index to a cell Q storing the first element, r is an index to the next available cell in Q

20 Queues- Example zPrint Jobs - All requests from workstations are enqueued to the print queue on a first come first served basis - The current (first) printjob is dequeued and sent to the printer for processing

21 Queue - Applications zI/O Request Handling File server - Workstation (print, data access,etc.) zTelephone Call Handling zHistory functions in applications

22 Limitations - Using Arrays zMaximum size needs to be predetermined zPotential wastage of allocated memory zNeed to handle overflow

23 Linked List ADT Head next next next null Each node stores a reference to an element and a reference, called next to another node

24 Singly Linked List Queue Implementation zNodes connected in a chain by links headtail zhead of the list - front of the queue tail of the list - rear of the queue

25 Removing at the Head headtail zadvance head reference headtail

26 Inserting at the Tail zcreate a new node headtail zchain it and move the tail reference headtail

27 Other Advantages zInsertions and deletions are easier compared to an array implementation when dealing with “lists”

28 Linked Lists - Query zIf a stack will be implemented using a linked list, is it better to assign the head or the tail as the top of the stack ? Why ?

29 Double-Ended Queues zData structure that allows insertion and deletion at the front and the rear end of the queue (pronounced as “deck”)

30 Doubly linked lists zA node in a doubly linked list is like a node in a singly linked list except that, in addition to the next link, it also has a prev link to the previous node in the list. zAdvantage over singly linked lists : deletions at the tail of the list can be done in constant time

31 Doubly-Linked Lists

32 Linked Lists - Query zWhat is the running time to search for an entry in a linked list ? Deleting from the tail ? Inserting from the head ?


Download ppt "ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists."

Similar presentations


Ads by Google