Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.

Similar presentations


Presentation on theme: "Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science."— Presentation transcript:

1 Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science II Saturday, 5 October 2007 Lecture 7 of Queues, Linked Lists II

2 Asymptotic Notations Queues Linked Lists Outline

3 STACKS Exercises: -match Parentheses Algorithm ParenMatch(S,n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match

4 STACKS Solution -match Parentheses Algorithm ParenMatch(X,n): For i=0;i<n; i++{ If X[i]==‘[‘ or X[i]==‘(‘ or X[i]==‘{‘ then S.push(X[i]); else If X[i]==‘]‘ or X[i]==‘)‘ or X[i]==‘}‘ then Try{ Check(x[i], S.pop()) }catch(EmptyException){return false} } If(S.isEmpty){return false} Return true

5 QUEUES Definition A container of objects that are inserted and removed according to the first-in first-out FIFO principle.

6 QUEUES Applications Queues provide services in computer science, transport and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.

7 QUEUES Basics -f: front is an index to the cell of Q storing the first element of the queue. -r: is an index to the next available array cell in Q. -Initially f=r=0 -Enqueue: from the rear r -Dequeue: from the front f

8 QUEUES Queue ADT Fundamentals methods enqueue(object): inserts an element at the end of the queue object dequeue(): removes and returns the element at the front of the queue

9 QUEUES Queue ADT Supporting methods object front(): returns the element at the front without removing it integer size(): returns the number of elements stored boolean isEmpty(): indicates whether no elements are stored

10 QUEUES Examplefrom book

11 QUEUES Interface in Java public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; }

12 QUEUES Queue ADT Algorithms Algorithm size() return (N − f + r) mod N

13 QUEUES Queue ADT Algorithms Algorithm isEmpty() return (f = r)

14 QUEUES Queue ADT Algorithms Algorithm enqueue(o) if size() = N − 1 then throw FullQueueException else Q[r] ← o r ← (r + 1) mod N

15 QUEUES Queue ADT Algorithms Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o ← Q[f] f ← (f + 1) mod N return o

16 QUEUES Exercise Describe how to implement a Queue ADT using two stacks

17 QUEUES Exercise Describe how to implement a stack ADT using two stacks

18 LINKED LISTS Definition A sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. head

19 LINKED LISTS Definition A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time,[1] but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.

20 LINKED LISTS Applications Navigating pages in a novel. Presenting test questions. Moving through a list of user settings on an application. head tail

21 LINKED LISTS Vs. Arrays Space usage: Indexing Insertion Deletion head tail

22 SINGLY LINKED LISTS Definition A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node head tail

23 SINGLY LINKED LISTS Class Node class Node { // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ Node() { this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; }

24 SINGLY LINKED LISTS Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node

25 SINGLY LINKED LISTS Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node

26 SINGLY LINKED LISTS Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

27 SINGLY LINKED LISTS Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node

28 SINGLY LINKED LISTS Exercise Compare arrays with singly linked lists in terms of: -Space Usage. -Indexing -Insertion -Deletion

29 References [1] Textbook. [2] http://en.wikipedia.org/wiki/Linked_list http://en.wikipedia.org/wiki/Linked_list Analysis Tools


Download ppt "Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science."

Similar presentations


Ads by Google