25
Linear Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Linear Data Structures

Embed Size (px)

DESCRIPTION

Linear Data Structures. Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park. Linear Data Structures. Lists Linked list Doubly linked list Circular list Stack Queue Circular queue. Linked List. Properties Elements in linked list are ordered - PowerPoint PPT Presentation

Citation preview

Page 1: Linear Data Structures

Linear Data Structures

Fawzi EmadChau-Wen Tseng

Department of Computer ScienceUniversity of Maryland, College Park

Page 2: Linear Data Structures

Linear Data Structures

ListsLinked listDoubly linked listCircular list

Stack Queue

Circular queue

Page 3: Linear Data Structures

Linked List

PropertiesElements in linked list are orderedElement has successor

State of ListHeadTailCursor (current position)

HeadTail

Cursor

Page 4: Linear Data Structures

Reference-based Implementation

Nodes contain references to other nodesExample

IssuesEasy to find succeeding elementsStart from head of list for preceding elements

Page 5: Linear Data Structures

Array vs. Reference-based Linked List

Reference-based linked listInsertion / deletion = O(1)Indexing = O(n)Easy to dynamically increase size of list

ArrayInsertion / deletion = O(n)Indexing = O(1)Compact, uses less spaceEasy to copy, mergeBetter cache locality

Page 6: Linear Data Structures

Linked List – Insert (After Cursor)

1. Original list & new element temp

2. Modify temp.next cursor.next

Page 7: Linear Data Structures

Linked List – Insert (After Cursor)

3. Modify cursor.next temp

4. Modify cursor temp

Page 8: Linear Data Structures

Linked List – Delete (Cursor)

1. Find before such that before.next = cursor

2. Modify before.next cursor.next

Page 9: Linear Data Structures

Linked List – Delete (Cursor)

3. Delete cursor

4. Modify cursor before.next

Page 10: Linear Data Structures

Doubly Linked List

PropertiesElements in linked list are orderedElement has predecessor & successor

State of ListHeadTailCursor (current position)

HeadTail

Cursor

Page 11: Linear Data Structures

Doubly Linked List

Example

IssuesEasy to find preceding / succeeding elementsExtra work to maintain links (for insert / delete)More storage per node

Page 12: Linear Data Structures

Node Structures for Linked Lists

Linked listClass Node {

Object data;Node next;

}

Doubly linked listClass Node {

Object data;Node next;Node previous;

}

Page 13: Linear Data Structures

Doubly Linked List – Insertion

Example

Must update references in both predecessor and successor nodes

Page 14: Linear Data Structures

Circular Linked Lists

Last element links to first elementProperties

Can reach entire list from any nodeNeed special test for end of listRepresent

BuffersNaturally circular data

HeadTail

Page 15: Linear Data Structures

Circular Linked Lists – Examples

Circular linked list

Circular doubly linked list

Page 16: Linear Data Structures

StackProperties

Elements removed in opposite order of insertionLast-in, First-out (LIFO)Must track position of Top (last element added)

Stack operationsPush = add element (to top)Pop = remove element (from top)

Page 17: Linear Data Structures

Stack Implementations

Linked listAdd / remove from head of list

ArrayIncrement / decrement Top pointer after push / pop

X Y Z … Top

Page 18: Linear Data Structures

Stack Applications

Run-time procedure information

Arithmetic computationsPostfix notation

Simplified instruction setJava bytecode

Page 19: Linear Data Structures

Queue

PropertiesElements removed in order of insertionFirst-in, First-out (FIFO)Must track Front (first in) and Back (last in)

Queue operationsEnqueue = add element (to back)Dequeue = remove element (from front)

Page 20: Linear Data Structures

Queue Implementations

Linked listAdd to tail (Back) of listRemove from head (Front) of list

ArrayCircular array

Page 21: Linear Data Structures

Queue – Array

Store queue as elements in arrayProblem

Queue contents move (“inchworm effect”)

As result, can not add to back of queue, even though queue is not full

Page 22: Linear Data Structures

Queue – Circular Array

Circular array (ring)q[ 0 ] follows q[ MAX – 1 ]Index using q[ i % MAX ]

ProblemDetecting difference between empty and nonempty queue

Page 23: Linear Data Structures

Queue – Circular ArrayApproach 1

Keep Front at first inKeep Back at last in

ProblemEmpty queue identical to queue with 1 element

Page 24: Linear Data Structures

Queue – Circular ArrayApproach 2

Keep Front at first inKeep Back at last in – 1

ProblemEmpty queue identical to full queue

Page 25: Linear Data Structures

Queue – Circular ArrayInherent problem for queue of size N

Only N possible (Front – Back) pointer locationsN+1 possible queue configurations

Queue with 0, 1, … N elements

SolutionsMaintain additional state information

Use state to recognize empty / full queueExamples

Record SizeRecord QueueEmpty flag

Leave empty element in queueStore marker in queue