INFO0902 Data Structures and Algorithms

Size: px
Start display at page:

Download "INFO0902 Data Structures and Algorithms"

Transcription

1 INFO0902 Data Structures and Algorithms Basic ADTs: stacks, queues, array lists, node list, sequences, iterators Justus H. Piater

2 Stacks

3 The Stack ADT (stack, LIFO) void push(object e); Object pop(void); Integer size(void); Boolean isempty(void); Object top(void); Applications? Stacks 3 / 83

4 Java Interface /** * Interface for a stack: a collection of objects that are inserted * and removed according to the last in first out principle. * Roberto Tamassia Michael Goodrich EmptyStackException */ public interface Stack { /** * Return the number of elements in the stack. number of elements in the stack. */ public int size(); /** * Return whether the stack is empty. true if the stack is empty, false otherwise. */ Stacks 4 / 83

5 Java Interface (Continued) public boolean isempty(); /** * Inspect the element at the top of the stack. top element in the stack. EmptyStackException if the stack is empty. */ public Object top() throws EmptyStackException; /** * Insert an element at the top of the stack. element element to be inserted. */ public void push (Object element); /** * Remove the top element from the stack. element removed. EmptyStackException if the stack is empty. */ public Object pop() throws EmptyStackException; Stacks 5 / 83

6 Errors pop() and top() raise an error if the stack is empty: /** * Runtime exception thrown when one tries to perform operation top or * pop on an empty stack. */ public class EmptyStackException extends RuntimeException { public EmptyStackException(String err) { super(err); Stacks 6 / 83

7 Implementation: Array Algorithm size(): return Algorithm isempty(): return Algorithm top(): if isempty() then error return Algorithm push( ): if size() then error Algorithm pop(): if isempty() then Stacks 7 / 83

8 Implementation: Array (Continued) error return Stacks 8 / 83

9 Complexity Time: Space: Stacks 9 / 83

10 Java Implementation /** * Implementation of the Stack interface using a fixed length array. * An exception is thrown if a push operation is attempted when the * size of the stack is equal to the length of the array. * Natasha Gelfand Roberto Tamassia FullStackException */ public class ArrayStack implements Stack { /** * Default length of the array used to implement the stack. */ public static final int CAPACITY = 1000; /** * Length of the array used to implement the stack. */ protected int capacity; Stacks 10 / 83

11 Java Implementation (Continued) /** * Array used to implement the stack. */ protected Object stack[]; /** * Index of the top element of the stack in the array. */ protected int top = 1; /** * Initialize the stack to use an array of default length CAPACITY. */ public ArrayStack() { this(capacity); /** * Initialize the stack to use an array of given length. * cap length of the array. */ Stacks 11 / 83

12 Java Implementation (Continued) public ArrayStack(int cap) { capacity = cap; stack = new Object[capacity]; /** * O(1) time. */ public int size() { return (top + 1); /** * O(1) time. */ public boolean isempty() { return (top < 0); /** * O(1) time. FullStackException if the array is full. Stacks 12 / 83

13 Java Implementation (Continued) */ public void push(object obj) throws FullStackException { if (size() == capacity) throw new FullStackException("Stack overflow."); stack[++top] = obj; /** * O(1) time. */ public Object top() throws EmptyStackException { if (isempty()) throw new EmptyStackException("Stack is empty."); return stack[top]; /** * O(1) time. */ public Object pop() throws EmptyStackException { Object elem; Stacks 13 / 83

14 Java Implementation (Continued) if (isempty()) throw new EmptyStackException("Stack is Empty."); elem = stack[top]; stack[top ] = null; // dereference stack[top] for garbage collection. return elem; Stacks 14 / 83

15 Application: Reverse an Array public static Integer[] reverse(integer[] a) { Stack stack = new ArrayStack(a.length); Integer[] b = new Integer[a.length]; for (int i=0; i < a.length; i++) stack.push(a[i]); for (int i=0; i < a.length; i++) b[i] = (Integer) (stack.pop()); return b; Stacks 15 / 83

16 Application: Matching Parentheses Algorithm parenmatch(, ): Input: a string of parentheses of different types. Output: true if all the parentheses match, false otherwise. empty stack for to do if is a left parenthesis then.push( ) else if.isempty() then return false if.pop() does not match return false if.isempty() then return true else return false then Stacks 16 / 83

17 Queues

18 The Queue ADT (file, FIFO) void enqueue(object e); Object dequeue(void); Integer size(void); Boolean isempty(void); Object front(void); Applications? Queues 18 / 83

19 Java Interface public interface Queue { /** * Returns the number of elements in the queue. number of elements in the queue. */ public int size(); /** * Returns whether the queue is empty. true if the queue is empty, false otherwise. */ public boolean isempty(); /** * Inspects the element at the front of the queue. element at the front of the queue. EmptyQueueException if the queue is empty. */ public Object front() throws EmptyQueueException; /** * Inserts an element at the rear of the queue. Queues 19 / 83

20 Java Interface (Continued) element new element to be inserted. */ public void enqueue (Object element); /** * Removes the element at the front of the queue. element removed. EmptyQueueException if the queue is empty. */ public Object dequeue() throws EmptyQueueException; Queues 20 / 83

21 Implementation: Simple Array Queues 21 / 83

22 Implementation: Circular Array Algorithm size(): return mod Algorithm isempty(): return Algorithm front(): if isempty() then error return Algorithm dequeue(): if isempty() then error return mod Algorithm enqueue(o): Queues 22 / 83

23 if size() then error mod Implementation: Circular Array (Continued) Queues 23 / 83

24 Complexity Time: Space: Queues 24 / 83

25 Linked Lists

26 Linked List DS Useful to avoid arbitrary capacity limits. The list is identified by the head pointer. Each node contains the element and next pointers. A null next pointer indicates the end of the list. Linked Lists 26 / 83

27 A Node in Java public class Node { // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public 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; Linked Lists 27 / 83

28 A Node in Java (Continued) // Modifier methods: public void setelement(object newelem) { element = newelem; public void setnext(node newnext) { next = newnext; Linked Lists 28 / 83

29 Complexity Space: Insertion and removal at the head: Insertion and removal at the tail: Ditto, with additional tail pointer: Linked Lists 29 / 83

30 Implementing a Stack Complexity of the operations? public class NodeStack implements Stack { protected Node top; // reference to the head node protected int size; // number of elements in the stack public NodeStack() { // constructs an empty stack top = null; size = 0; public int size() { return size; public boolean isempty() { if (top == null) return true; return false; public void push(object elem) { Node v = new Node(elem, top); // create and link in a new node Linked Lists 30 / 83

31 Implementing a Stack (Continued) top = v; size++; public Object top() throws EmptyStackException { if (isempty()) throw new EmptyStackException("Stack is empty."); return top.getelement(); public Object pop() throws EmptyStackException { if (isempty()) throw new EmptyStackException("Stack is empty."); Object temp = top.getelement(); top = top.getnext(); // link out the former top node size ; return temp; Linked Lists 31 / 83

32 Implementing a Queue Complexity of the operations? Why not the other way around? public void enqueue(object obj) { Node node = new Node(); node.setelement(obj); node.setnext(null); // node will be new tail node if (size == 0) head = node; // special case of a previously empty queue else tail.setnext(node); // add node at the tail of the list tail = node; // update the reference to the tail node size++; public Object dequeue() throws EmptyQueueException { if (size == 0) throw new EmptyQueueException("Queue is empty."); Object obj = head.getelement(); Linked Lists 32 / 83

33 Implementing a Queue (Continued) head = head.getnext(); size ; if (size == 0) tail = null; // the queue is now empty return obj; Linked Lists 33 / 83

34 Deques

35 The Deque ADT (double-ended queue) void insertfirst(object e); void insertlast(object e); Object removefirst(void); Object removelast(void); Integer size(void); Boolean isempty(void); Object first(void); Object last(void); Generalization of both the stack and the queue. Deques 35 / 83

36 Applications? The Deque ADT (double-ended queue) (Continued) Deques 36 / 83

37 Doubly Linked List DS public class DLNode { private Object element; private DLNode next, prev; DLNode() { this(null, null, null); DLNode(Object e, DLNode p, DLNode n) { element = e; next = n; prev = p; public void setelement(object newelem) { element = newelem; public void setnext(dlnode newnext) { next = newnext; public void setprev(dlnode newprev) { prev = newprev; public Object getelement() { return element; public DLNode getnext() { return next; public DLNode getprev() { return prev; Deques 37 / 83

38 Sentinel Nodes All insertions and removals are done in the same way, including at the head and at the tail. Deques 38 / 83

39 Implementing a Deque using a Doubly Linked List public class NodeDeque implements Deque { protected DLNode header, trailer; // sentinels protected int size; // number of elements public NodeDeque() { // initialize an empty deque header = new DLNode(); trailer = new DLNode(); header.setnext(trailer); // make header point to trailer trailer.setprev(header); // make trailer point to header size = 0; public Object first() throws EmptyDequeException { if (isempty()) throw new EmptyDequeException("Deque is empty."); return header.getnext().getelement(); public void insertfirst(object o) { DLNode second = header.getnext(); DLNode first = new DLNode(o, header, second); second.setprev(first); Deques 39 / 83

40 Implementing a Deque using a Doubly Linked List (Continued) header.setnext(first); size++; public Object removelast() throws EmptyDequeException { if (isempty()) throw new EmptyDequeException("Deque is empty."); DLNode last = trailer.getprev(); Object o = last.getelement(); DLNode secondtolast = last.getprev(); trailer.setprev(secondtolast); secondtolast.setnext(trailer); size ; return o; Deques 40 / 83

41 Array Lists

42 The Array List ADT Abstraction of an array: access by rank,, etc., Object elematrank(integer r); Object replaceatrank(integer r, Object e); void insertatrank(integer r, Object e); Object removeatrank(integer r); Array Lists 42 / 83

43 Java Interface public interface ArrayList { /** Returns the number of elements in the vector. */ public int size(); /** Returns whether the vector is empty. */ public boolean isempty(); /** Returns the element stored at the given rank. */ public Object elematrank(int r) throws BoundaryViolationException; /** Replaces the element stored at the given rank. */ public Object replaceatrank(int r, Object e) throws BoundaryViolationException; /** Inserts an element at the given rank. */ public void insertatrank(int r, Object e) throws BoundaryViolationException; /** Removes the element stored at the given rank. */ public Object removeatrank(int r) throws BoundaryViolationException; Array Lists 43 / 83

44 Implementing a Deque using an Array List Array Lists 44 / 83

45 Implementation by a Fixed-Size Array Complexity of the four functions in terms of : Complexity of the four functions in terms of and : Insertions and removals at in? Array Lists 45 / 83

46 Java Implementation /** Realization of a vector by means of an array. The array has * initial length 16 and is doubled when the size of the vector * exceeds the capacity of the array. No shrinking of the array is * performed. */ public class ArrayVector implements Vector { private Object[] A; // array storing the elements of the vector private int capacity = 16; // initial length of array A private int size = 0; // number of elements stored in the vector /** Creates the vector with initial capacity 16. */ public ArrayVector() { A = new Object[capacity]; /** Inserts an element at the given rank. */ public void insertatrank(int r, Object e) throws BoundaryViolationException { checkrank(r, size() + 1); if (size == capacity) { // an overflow capacity *= 2; Object[] B = new Object[capacity]; for (int i=0; i&ltsize; i++) Array Lists 46 / 83

47 Java Implementation (Continued) B[i] = A[i]; A = B; for (int i=size 1; i>=r; i ) // shift elements up A[i+1] = A[i]; A[r] = e; size++; /** Removes the element stored at the given rank. */ public Object removeatrank(int r) throws BoundaryViolationException { checkrank(r, size()); Object temp = A[r]; for (int i=r; i&ltsize 1; i++) // shift elements down A[i] = A[i+1]; size ; return temp; Array Lists 47 / 83

48 Implementation using an Extensible Array Whenever an insertion would exceed the array capacity, it is replaced by another array of twice the size. Proposition: The total time to insert elements at the end of the array is. Proof (by amortization): Suppose that the th augmentation costs Overcharge each insertion by 2 Euros. These Euros will pay for the augmentation. Euros. Array Lists 48 / 83

49 Implementation using an Additive Extensible Array When an insertion would exceed the array capacity, it is replaced by another array larger by a constant increment. Proposition: The total time to insert elements at the end of the array is. Proof: The insertions give rise to augmentations. Thus, the time required to insert all elements is proportional to Array Lists 49 / 83

50 java.util.arraylist and java.util.vector ADT Array List Java ArrayList and Vector size(), isempty() size(), isempty() elematrank( ) get( ) replaceatrank(, ) set(, ) insertatrank(, ) add(, ) removeatrank( ) remove( ) Array Lists 50 / 83

51 Node Lists

52 The Node List ADT Abstraction of a list: access by position; notions of preceding and following elements (but no absolute ranks) Position: Object element(void); Node List: Position first(void); Position last(void); Position prev(position p); Position next(position p); Node Lists 52 / 83

53 The Node List ADT (Continued) Object replace(position p, Object e); Position insertfirst(object e); Position insertlast(object e); Position insertbefore(position p, Object e); Position insertafter(position p, Object e); Object remove(position p); Applications? Node Lists 53 / 83

54 Error Conditions null has been removed from the list belongs to a different list is the first position, and we call prev( ) is the last position, and we call next( ) Which error checks would you perform? Node Lists 54 / 83

55 Java Interface public interface Position { /** Returns the element stored at this position. */ Object element(); public interface PositionList { /** Returns the number of elements in this list. */ public int size(); /** Returns whether the list is empty. */ public boolean isempty(); /** Returns the first node in the list. */ public Position first(); /** Returns the last node in the list. */ public Position last(); /** Returns the node after a given node in the list. */ public Position next(position p) throws InvalidPositionException, BoundaryViolationException; /** Returns the node before a given node in the list. */ public Position prev(position p) throws InvalidPositionException, BoundaryViolationException; Node Lists 55 / 83

56 Java Interface (Continued) /** Inserts an element at the front of the list. */ public Position insertfirst(object e); /** Inserts and element at the back of the list. */ public Position insertlast(object e); /** Inserts an element after the given node in the list. */ public Position insertafter(position p, Object e) throws InvalidPositionException; /** Inserts an element before the given node in the list. */ public Position insertbefore(position p, Object e) throws InvalidPositionException; /** Removes a node from the list. */ public Object remove(position p) throws InvalidPositionException; /** Replaces the element stored at the given node. */ public Object replace(position p, Object e) throws InvalidPositionException; Node Lists 56 / 83

57 Doubly Linked List: insert and remove an element Algorithm insertafter(, ): create a new node.setelement( ).setprev( ).setnext(.getnext()).getnext().setprev( ).setnext( ) return Algorithm remove( ):.getprev().setnext(.getnext()).getnext().setprev(.getprev()) return.element Node Lists 57 / 83

58 Java Implementation: DNode public class DNode implements Position { private DNode prev, next; // References to the nodes before and after private Object element; // Element stored in this position // Constructor public DNode(DNode newprev, DNode newnext, Object elem) { prev = newprev; next = newnext; element = elem; // Method from interface Position public Object element() throws InvalidPositionException { if ((prev == null) && (next == null)) throw new InvalidPositionException("Position is not in a list!"); return element; // Accessor methods public DNode getnext() { return next; public DNode getprev() { return prev; // Update methods Node Lists 58 / 83

59 Java Implementation: DNode (Continued) public void setnext(dnode newnext) { next = newnext; public void setprev(dnode newprev) { prev = newprev; public void setelement(object newelement) { element = newelement; Node Lists 59 / 83

60 Java Implementation: Doubly-Linked List public class NodePositionList implements PositionList { protected int numelts; // Number of elements in the list protected DNode header, trailer; // Special sentinels /** Constructor that creates an empty list; O(1) time */ public NodeList() { numelts = 0; header = new DNode(null, null, null); // create header trailer = new DNode(header, null, null); // create trailer header.setnext(trailer); // make header and trailer point to each other /** Checks if position is valid for this list and converts it to * DNode if it is valid; O(1) time */ protected DNode checkposition(position p) throws InvalidPositionExceptio if (p == null) throw new InvalidPositionException ("Null position passed to NodeList"); if (p == header) throw new InvalidPositionException ("The header node is not a valid position"); Node Lists 60 / 83

61 Java Implementation: Doubly-Linked List (Continued) if (p == trailer) throw new InvalidPositionException ("The trailer node is not a valid position"); try { DNode temp = (DNode)p; if ((temp.getprev() == null) (temp.getnext() == null)) throw new InvalidPositionException ("Position does not belong to a valid NodeList"); return temp; catch (ClassCastException e) { throw new InvalidPositionException ("Position is of wrong type for this list"); /** Returns the number of elements in the list; O(1) time */ public int size() { return numelts; /** Returns whether the list is empty; O(1) time */ public boolean isempty() { return (numelts == 0); /** Returns the first position in the list; O(1) time */ Node Lists 61 / 83

62 Java Implementation: Doubly-Linked List (Continued) public Position first() throws EmptyListException { if (isempty()) throw new EmptyListException("List is empty"); return header.getnext(); /** Returns the position before the given one; O(1) time */ public Position prev(position p) throws InvalidPositionException, BoundaryViolationException { DNode v = checkposition(p); DNode prev = v.getprev(); if (prev == header) throw new BoundaryViolationException ("Cannot advance past the beginning of the list"); return prev; /** Insert the given element before the given position, returning * the new position; O(1) time */ public Position insertbefore(position p, Object element) Node Lists 62 / 83

63 Java Implementation: Doubly-Linked List (Continued) throws InvalidPositionException { // DNode v = checkposition(p); numelts++; DNode newnode = new DNode(v.getPrev(), v, element); v.getprev().setnext(newnode); v.setprev(newnode); return newnode; /** Insert the given element at the beginning of the list, returning * the new position; O(1) time */ public Position insertfirst(object element) { numelts++; DNode newnode = new DNode(header, header.getnext(), element); header.getnext().setprev(newnode); header.setnext(newnode); return newnode; /**Remove the given position from the list; O(1) time */ public Object remove(position p) Node Lists 63 / 83

64 Java Implementation: Doubly-Linked List (Continued) throws InvalidPositionException { DNode v = checkposition(p); numelts ; DNode vprev = v.getprev(); DNode vnext = v.getnext(); vprev.setnext(vnext); vnext.setprev(vprev); Object velem = v.element(); // unlink the position from the list and make it invalid v.setnext(null); v.setprev(null); return velem; /** Replace the element at the given position with the new element * and return the old element; O(1) time */ public Object replace(position p, Object element) throws InvalidPositionException { DNode v = checkposition(p); Node Lists 64 / 83

65 Java Implementation: Doubly-Linked List (Continued) Object oldelt = v.element(); v.setelement(element); return oldelt; Node Lists 65 / 83

66 Sequences

67 The Sequence ADT Union of array list and node list, plus the following bridge functions: Position atrank(integer r); Integer rankof(position p); Sequences 67 / 83

68 Java Interface /** * An interface for a sequence, a data structure supporting all * operations of a vector and a list. */ public interface Sequence extends List, Vector { /** Returns the position containing the element at the given rank. */ public Position atrank(int r) throws BoundaryViolationException; /** Returns the rank of the element stored at the given position. */ public int rankof(position p) throws InvalidPositionException; Sequences 68 / 83

69 Implementation using a Doubly-Linked List Complexity of the array list, node list and bridge functions? Sequences 69 / 83

70 Java Implementation /** Implementation of a sequence by means of a doubly linked list. */ public class NodeSequence extends NodeList implements Sequence { /** Checks whether the given rank is in the range [0, n 1] */ protected void checkrank(int r, int n) throws BoundaryViolationException { if (r < 0 r >= n) throw new BoundaryViolationException("Illegal rank: " + r); /** Returns the position containing the element at the given rank; * O(n) time. */ public Position atrank (int rank) { DNode node; checkrank(rank, size()); if (rank <= size()/2) { // scan forward from the head node = header.getnext(); for (int i=0; i < rank; i++) node = node.getnext(); else { // scan backward from the tail node = trailer.getprev(); Sequences 70 / 83

71 Java Implementation (Continued) for (int i=1; i < size() rank; i++) node = node.getprev(); return node; /** Inserts an element at the given rank; O(n) time. */ public void insertatrank (int rank, Object element) throws BoundaryViolationException { checkrank(rank, size() + 1); if (rank == size()) insertlast(element); else insertbefore(atrank(rank), element); /** Removes the element stored at the given rank; O(n) time. */ public Object removeatrank (int rank) throws BoundaryViolationException { checkrank(rank, size()); return remove(atrank(rank)); Sequences 71 / 83

72 Implementation using an Array Note We need to implement the Position ADT. Complexity of the array list, node list and bridge functions? Sequences 72 / 83

73 Example: The Favorite List ADT Store in a list the elements accessed and their access counts void access(object e); Object remove(object e); List top(integer k); Sequences 73 / 83

74 Two Implementations Sorted list Move-to-front heuristic Complexity of the three functions in the following scenario: 1. accesses of element 1 2. accesses of element accesses of element Sequences 74 / 83

75 Iterators

76 The Iterator ADT To traverse all elements of a collection, independently of its implementation Boolean hasnext(void); Object next(void); Such a collection must provide the following function: Iterator elements(void); Iterators 76 / 83

77 Usage in Java public static void printcontainer(iterable container) { for (java.util.iterator iter = contaner.iterator(); iter.hasnext(); System.out.println(iter.next()); public static void printcontainer(iterable container) { for (Object obj : container) System.out.println(obj); Iterators 77 / 83

78 Position Iterator For node lists and sequences, it can be useful to iterate over positions. next() returns a Position instead of an Object. To obtain a position iterator, use positions() instead of elements(). Iterators 78 / 83

79 Implementing a Position Iterator for the NodePositionList class public class PositionIterator implements Iterator { protected List list; // the underlying list protected Position cur; // the current (next) position public PositionIterator() { // default constructor public PositionIterator(List L) { // preferred constructor list = L; if (list.isempty()) cur = null; // list is empty else cur = list.first(); // start with the first position public boolean hasnext() { return (cur!= null); public Object next() throws NoSuchElementException { if (!hasnext()) throw new NoSuchElementException("No next position"); Position toreturn = cur; if (cur == list.last()) cur = null; // no positions left else cur = list.next(cur); // move cursor to the next position return toreturn; Iterators 79 / 83

80 And Finally To the NodePositionList class we need to add: /** Returns an iterator of all the nodes in the list. */ public Iterator positions() { return new PositionIterator(this); /** Returns an iterator of all the elements in the list. */ public Iterator elements() { return new ElementIterator(this); Iterators 80 / 83

81 Iterators in Java See java.util.iterator. (There is also an older java.util.enumeration, whose use is discouraged.) The Java collections do not expose positions, but the java.util.listiterator provides equivalent functionality. Iterators 81 / 83

82 Summary

83 Summary We recalled some fundamental algorithmic concepts: ADT: stack + queue = deque array list + node list = sequence DS: simple and circular arrays extensible arrays singly and doubly linked lists Design Patterns: adapter iterator Summary 83 / 83

Lists. Outline. COMP9024: Data Structures and Algorithms. Lists. Array List ADT. Array-Based Implementation. Array lists Node lists Iterators

Lists. Outline. COMP9024: Data Structures and Algorithms. Lists. Array List ADT. Array-Based Implementation. Array lists Node lists Iterators COMP9024: Data Structures and lgorithms Week Four: Lists and Iterators Hui Wu Outline rray lists Node lists Iterators Session 2, 2016 http://www.cse.unsw.edu.au/~cs9024 1 2 Lists Lists list is a collection

More information

Linked Lists. Singly Linked Lists (3.2) CSE 2011 Winter /01/2011

Linked Lists. Singly Linked Lists (3.2) CSE 2011 Winter /01/2011 Linked Lists CSE 2011 Winter 2011 24 January 2011 1 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next

More information

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract

More information

Sequences Outline and Required Reading: Sequences ( 5.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Sequences Outline and Required Reading: Sequences ( 5.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic 1 Sequences Outline and Required Reading: Sequences ( 5.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Sequence ADT 2 Sequence ADT union of Vector ADT and List ADT provides both rank- and position-

More information

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,

More information

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures Lecture 3 Linear Data Structures Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Outline Our goal in this lecture is to Review the basic linear data structures Demonstrate how each can be defined as an Abstract Data

More information

CS2210 Data Structures and Algorithms

CS2210 Data Structures and Algorithms CS2210 Data Structures and Algorithms Lecture 3: ADT and Java interfaces Instructor: Olga Veksler 2004 Goodrich, Tamassia Outline Review Data Structures Abstract Data Types 2 Principles of OO Programming

More information

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal Queues COL 106 Slides by Amit Kumar, Shweta Agrawal The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out (FIFO) scheme Insertions are at the rear

More information

Queues. Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid. Java: Queues / 1

Queues. Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid. Java: Queues / 1 Queues Carlos Delgado Kloos Dep. Ingeniería Telemática Univ. Carlos III de Madrid cdk@it.uc3m.es Java: Queues / 1 Example The queue at the bus stop The printer queue cdk@it.uc3m.es Java: Queues / 2 Characteristics

More information

COMP9024: Data Structures and Algorithms

COMP9024: Data Structures and Algorithms COMP9024: Data Structures and Algorithms Week Four: Stacks and Queues Hui Wu Session 2, 2015 http://www.cse.unsw.edu.au/~cs9024 1 Outline Stacks Queues 2 Stacks 3 Abstract Data Types (ADTs) An abstract

More information

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify

More information

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011 Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error

More information

Vectors. 9/14/2007 2:55 PM Vectors 1

Vectors. 9/14/2007 2:55 PM Vectors 1 Vectors 9/4/2007 2:55 PM Vectors Outline and Reading The Vector ADT ( 5..) Array-based implementation ( 5..2) 9/4/2007 2:55 PM Vectors 2 The Vector ADT The Vector ADT stores objects to which it provides

More information

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Data Structure: Lists and Iterators 2017 Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Contents Data structures to be covered in this lecture Array lists (aka Vectors) Node lists

More information

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS SYLLABUS ABSTRACT DATA TYPES (ADTS) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations

More information

Lecture 3 Linear Data Structures

Lecture 3 Linear Data Structures Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify

More information

Lists and Sequences. 1/22/ :32 PM Sequences 1

Lists and Sequences. 1/22/ :32 PM Sequences 1 Lists and Sequences 1/22/2018 10:32 PM Sequences 1 Outline Singly linked list Position ADT and List ADT Doubly linked list Sequence ADT Implementations of the sequence ADT Iterators 1/22/2018 10:32 PM

More information

csci 210: Data Structures Stacks and Queues

csci 210: Data Structures Stacks and Queues csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks

More information

csci 210: Data Structures Linked lists

csci 210: Data Structures Linked lists csci 210: Data Structures Linked lists Summary Today linked lists single-linked lists double-linked lists circular lists READING: GT textbook chapter 3.2. 3.3. 3.4 Arrays vs. Linked Lists We ve seen arrays:

More information

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. Stacks 1 Stacks The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle. 2 Main stack operations Insertion and removal are defined by: push(e): inserts

More information

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004) COMP250: Stacks Jérôme Waldispühl School of Computer Science McGill University Based on slides from (Goodrich & Tamassia, 2004) 2004 Goodrich, Tamassia The Stack ADT A Stack ADT is a list that allows only

More information

Lists and Sequences. 1/18/2005 4:12 AM Sequences 1

Lists and Sequences. 1/18/2005 4:12 AM Sequences 1 Lists and Sequences /8/2005 4:2 AM Sequences Outline and Reading Singly linked list Position ADT and List ADT ( 2.2.2) Doubly linked list ( 2.2.2) Sequence ADT ( 2.2.3) Implementations of the sequence

More information

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.

Outline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1. Stacks Outline and Reading The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.5) Stacks 2 Abstract Data Types (ADTs) An abstract data

More information

Computer Science 210: Data Structures. Linked lists

Computer Science 210: Data Structures. Linked lists Computer Science 210: Data Structures Linked lists Arrays vs. Linked Lists Weʼve seen arrays: int[] a = new int[10]; a is a chunk of memory of size 10 x sizeof(int) a has a fixed size a[0] a[1] a[2]...

More information

Arrays. Array Definition

Arrays. Array Definition Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Arrays Arrays 1 Array Definition An array

More information

Solutions to a few of the review problems:

Solutions to a few of the review problems: Solutions to a few of the review problems: Problem 1 The interface Deque can have array based and linked implementations. Partial versions of implementation classes follow. Supply implementations for the

More information

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice: CMP-338 106 Points Total Midterm Spring 2017 Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use

More information

Data Structures and Algorithms " Arrays and Linked Lists!

Data Structures and Algorithms  Arrays and Linked Lists! Data Structures and Algorithms " Arrays and Linked Lists! Outline" Arrays! Singly Linked Lists! Doubly Linked Lists! Phạm Bảo Sơn - DSA 2 Arrays" Most commonly used data structure.! Built-in in most programming

More information

CPSC 35 Midterm Exam

CPSC 35 Midterm Exam CPSC 35 Midterm Exam Fall 2007 10:30-11:20am, Wednesday 7 November Closed book exam NAME: Problem Max Obtained 1 15 2 20 3 20 4 20 5 25 Total 100 1 15 points Problem 1: For each of the following three

More information

Linked Lists. Outline. COMP9024: Data Structures and Algorithms. Algorithms versus Heuristics. Data Structures and Algorithms. Abstract Data Types

Linked Lists. Outline. COMP9024: Data Structures and Algorithms. Algorithms versus Heuristics. Data Structures and Algorithms. Abstract Data Types COMP9024: Data Structures and Algorithms Week Three: Linked Lists and Recursion Outline Singly Linked Lists Doubly Linked Lists Recursions Hui Wu Session 1, 2014 http://www.cse.unsw.edu.au/~cs9024 1 2

More information

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1 CSE 2100 Data Structures and Algorithms... In Java Stacks 1 What is Stack A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle. Internet

More information

csci 210: Data Structures Linked lists Summary Arrays vs. Lists Arrays vs. Linked Lists a[0] a[1] a[2]... null Today READING: Arrays

csci 210: Data Structures Linked lists Summary Arrays vs. Lists Arrays vs. Linked Lists a[0] a[1] a[2]... null Today READING: Arrays Summary Today linked lists csci 210: Data Structures Linked lists single-linked lists double-linked lists circular lists READING: LC chapter 4.1, 4.2, 4.3 Arrays vs. Linked Lists Arrays vs. Lists We!ve

More information

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz 1 COE 312 Data Structures Welcome to Exam II Monday November 23, 2016 Instructor: Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your name

More information

CMSC 132: Object-Oriented Programming II. Stack and Queue

CMSC 132: Object-Oriented Programming II. Stack and Queue CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This

More information

CH 6 : VECTORS, LISTS AND SEQUENCES

CH 6 : VECTORS, LISTS AND SEQUENCES CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. Lists CSE 2011 Winter 2007 January 24, 2007 1 Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. An ADT specifies: data stored operations on the data error conditions associated with

More information

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow.

Assignment 1. Check your mailbox on Thursday! Grade and feedback published by tomorrow. Assignment 1 Check your mailbox on Thursday! Grade and feedback published by tomorrow. COMP250: Queues, deques, and doubly-linked lists Lecture 20 Jérôme Waldispühl School of Computer Science McGill University

More information

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures 1.00 Lecture 26 Data Structures: Introduction Stacks Reading for next time: Big Java: 19.1-19.3 Data Structures Set of primitives used in algorithms, simulations, operating systems, applications to: Store

More information

DataStruct 5. Stacks, Queues, and Deques

DataStruct 5. Stacks, Queues, and Deques 2013-2 DataStruct 5. Stacks, Queues, and Deques Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. October 30, 2013 Advanced Networking Technology

More information

Iterators and Sequences

Iterators and Sequences Iterators and Sequences Iterators and Sequences 1 Iterators An iterator abstracts the process of scanning through a collection of elements It maintains a cursor that sits between elements in the list,

More information

Stacks Goodrich, Tamassia, Goldwasser Stacks

Stacks Goodrich, Tamassia, Goldwasser Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Stacks Stacks 1 The Stack ADT The Stack ADT

More information

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Stacks and Queues. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Stacks and Queues EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG What is a Stack? A stack is a collection of objects. Objects in a stack are inserted and removed according to the

More information

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.

Outline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack. Stacks Outline and Reading The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack Stacks 2 Abstract Data Types (ADTs) An abstract data type

More information

CH 6. VECTORS, LISTS, AND SEQUENCES

CH 6. VECTORS, LISTS, AND SEQUENCES CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees The Stack ADT ( 2.1.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in first-out scheme Think

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

More information

The java.util.list ADT

The java.util.list ADT Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Lists and Iterators 2014 Goodrich, Tamassia,

More information

ADTs, Arrays, Linked Lists

ADTs, Arrays, Linked Lists 1 ADTs, Arrays, Linked Lists Outline and Required Reading: ADTs ( 2.1) Using Arrays ( 3.1) Linked Lists ( 3.2, 3.3, 3.4) CSE 2011, Winter 2017 Instructor: N. Vlajic Data Type 2 A data type is a classification

More information

Unit 4 Basic Collections

Unit 4 Basic Collections Unit 4 Basic Collections General Concepts Templates Exceptions Iterators Collection (or Container) Classes Vectors (or Arrays) Sets Lists Maps or Tables C++ Standard Template Library (STL Overview A program

More information

Linked Lists. Chapter 4

Linked Lists. Chapter 4 Linked Lists Chapter 4 1 Linked List : Definition Linked List: A collection of data items of the same type that are stored in separate objects referred to as "nodes". Each node contains, in addition to

More information

Computer Science 62. Midterm Examination

Computer Science 62. Midterm Examination Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 Your name (Please print) 1. Suppose you are given a singly-linked list

More information

CH 6 : VECTORS, LISTS AND SEQUENCES

CH 6 : VECTORS, LISTS AND SEQUENCES CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM

More information

Data Structures Lecture 5

Data Structures Lecture 5 Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 5 Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy

More information

Basic Data Structures

Basic Data Structures Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified. The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The

More information

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

More information

Basic Data Structures 1 / 24

Basic Data Structures 1 / 24 Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is

More information

ECE Fall st Midterm Examination (120 Minutes, closed book)

ECE Fall st Midterm Examination (120 Minutes, closed book) ECE Fall 2009 1 st Midterm Examination (120 Minutes, closed book) Name: Question Score Student ID: 1 (15) 2 (20) 3 (20) 4 (15) 5 (20) 6 (10) NOTE: Any questions on writing code must be answered in Java

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList! Implementations I 1 Agenda Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList! Stack and queues Array-based implementations

More information

Foundations of Data Structures

Foundations of Data Structures Foundations of Data Structures Lecture 4 Elementary Abstract Data Types (ADT): Stack Queue 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies:

More information

CMP-338 Solutions Midterm Spring Version 1

CMP-338 Solutions Midterm Spring Version 1 Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use only one side of the yellow paper. 1. (16

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice: Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use only one side of the yellow paper. 1. (16

More information

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

Data structure is an organization of information, usually in memory, for better algorithm efficiency. Lecture 01 Introduction Wednesday, 29 July 2015 12:59 pm Data structure is an organization of information, usually in memory, for better algorithm efficiency. Abstract data types (ADTs) are a model for

More information

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Linked Structures Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Linked Structures Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Linked Structures: Object references as links Linked vs. array-based structures Managing

More information

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty

The Stack ADT. Stacks and Queues. Stack: Illustration. What is a Stack? Accessors top size isempty The Stack ADT Stacks and Queues ADT Data Structure Interface add() remove() find() request result EECS200: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG Accessors top Mutators push pop of

More information

Programming. Lists, Stacks, Queues

Programming. Lists, Stacks, Queues Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation

More information

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Linked Structures - Review Chapter 13. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Linked Structures: Object references as links Linked vs. array-based structures

More information

Lists ADT and Iterators

Lists ADT and Iterators Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Lists ADT and Iterators 2014 Goodrich, Tamassia,

More information

Linear Data Structures

Linear Data Structures Linear Data Structures Arrays Arrays are stored in contiguous memory locations and contain similar data An element can be accessed, inserted or removed by specifying its position (number of elements preceding

More information

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on Lecture 16 Jérôme Waldispühl School of Computer Science McGill University Slides by Mathieu BlancheIe Recap from last lecture

More information

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack. STACK Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is

More information

Introduction to Linked Data Structures

Introduction to Linked Data Structures Introduction to Linked Data Structures A linked data structure consists of capsules of data known as nodes that are connected via links Links can be viewed as arrows and thought of as one way passages

More information

Stacks Fall 2018 Margaret Reid-Miller

Stacks Fall 2018 Margaret Reid-Miller Stacks 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack

More information

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList Implementations I 1 Agenda Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList Stack and queues Array-based implementations

More information

Apply to be a Meiklejohn! tinyurl.com/meikapply

Apply to be a Meiklejohn! tinyurl.com/meikapply Apply to be a Meiklejohn! tinyurl.com/meikapply Seeking a community to discuss the intersections of CS and positive social change? Interested in facilitating collaborations between students and non-profit

More information

CH7. LIST AND ITERATOR ADTS

CH7. LIST AND ITERATOR ADTS CH7. LIST AND ITERATOR ADTS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) LISTS LIST ADT A List

More information

Linked Lists. Chapter 12.3 in Savitch

Linked Lists. Chapter 12.3 in Savitch Linked Lists Chapter 12.3 in Savitch Preliminaries n Arrays are not always the optimal data structure: q An array has fixed size needs to be copied to expand its capacity q Adding in the middle of an array

More information

Stacks and Queues. David Greenstein Monta Vista

Stacks and Queues. David Greenstein Monta Vista Stacks and Queues David Greenstein Monta Vista Stack vs Queue Stacks and queues are used for temporary storage, but in different situations Stacks are Used for handling nested structures: processing directories

More information

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014 Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014 Review List = Organization of Data in a Linear Fashion, where Order is Important Set of actions that can be carried out efficiently on the data. Typical Actions

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

8. Fundamental Data Structures

8. Fundamental Data Structures 172 8. Fundamental Data Structures Abstract data types stack, queue, implementation variants for linked lists, [Ottman/Widmayer, Kap. 1.5.1-1.5.2, Cormen et al, Kap. 10.1.-10.2] Abstract Data Types 173

More information

Interfaces & Generics

Interfaces & Generics Interfaces & Generics CSC207 Winter 2018 The Programming Interface The "user" for almost all code is a programmer. That user wants to know:... what kinds of object your class represents... what actions

More information

Data structure and algorithm in Python

Data structure and algorithm in Python Data structure and algorithm in Python Linked Lists Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Singly Linked Lists 2. Circularly Linked Lists 3. Doubly Linked

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff

Queues. Virtuelle Fachhochschule. Prof. Dr. Debora Weber-Wulff Queues Virtuelle Fachhochschule Prof. Dr. Debora Weber-Wulff!1 Queues First In, First Out Well-known in socialist society Operations enqueue join the back of the line dequeue remove from the front of the

More information

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 CMPSCI 187: Programming With Data Structures Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 Implementing Stacks With Linked Lists Overview: The LinkedStack Class from L&C The Fields and

More information

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java Stacks (01F) Lecture6: Stacks What is stack? An abstract data type storing arbitrary objects. An ordered list in which all insertions and deletions are made at one end, called top Push Pop Top Bohyung

More information

DNHI Homework 3 Solutions List, Stacs and Queues

DNHI Homework 3 Solutions List, Stacs and Queues Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations.

More information