Linear Structures Every non-empty linear structure has A unique - - PDF document

linear structures
SMART_READER_LITE
LIVE PREVIEW

Linear Structures Every non-empty linear structure has A unique - - PDF document

2/4/2011 Linear Structure Linear Structures Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Chapter 3 Every element except first


slide-1
SLIDE 1

2/4/2011 1

1

Linear Structures

Chapter 3

CPTR 318

2 CPTR 314

Linear Structure

 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 element except first has a unique

predecessor

Implementations

 Array

 Contiguous memory  Space needed only for data  Insertions and deletions may require data movement

 Linked list

 Non-contiguous memory  Data and links both require space  Insertions and deletions require no data movement

Array details

 For an array A of size n, where n > 0:  First element: A[0]  Last element: A[n – 1]  Predecessor of A[i], for all i > 0: A[i – 1]  Successor of A[i], for all i < n – 1: A[i + 1] n − 1 i − 1 i + 1 i n A template <typename T> struct Node { T data; Node *prev, *next; }; template <typename T> class List { Node<T> *head, *tail; // Methods omitted … };

List details

 For a list L, where L.head is not null, and p is a

Node<T> pointer to an element in the list:

 First element: L.head  Last element: L.tail  Predecessor of p, for all p ≠ L.head: p->prev  Successor of p, for all p ≠ L.tail: p->next

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

slide-2
SLIDE 2

2/4/2011 2 Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

Operations

Operation Array Linked List Searching O(n) O(n) Ordered Search O(log n) O(n) Insertion O(n) O(1) Deletion O(n) O(1) Sorting O(n log n) O(n log n) Random Access O(1) O(n)

slide-3
SLIDE 3

2/4/2011 3 Specialized Linear Structures

 Stack

 LIFO  Operations: push, pop, top

 Queue

 FIFO  Operations: enqueue, dequeue, first

 Deque

 Access at both ends  Operations: insert_front, insert_back, delete_front,

delete_back, front, back

Stack

 LIFO  Operations

 push  pop  top

 Applications

 Depth-first search  Recursive evaluation

Queue

 FIFO  Operations

 enqueue  dequeue  front

 Applications

 Breadth-first search  Scheduling  Simulations

16

Encapsulation—Good!

 Data structure encapsulation is also known as

data hiding

 Client need not worry about the internal details

 Client is concerned with what, not how

 The author of the encapsulating class is free to

change the internals of the encapsulated class without breaking client code

 Wonderful for software maintenance

issues: fixes, optimizations, etc.

17

Encapsulation—Bad!

 The client is insulated from accessing

the individual elements of the data structure

 The client is at the mercy of the

access operations provided by the encapsulating class

 What if I want to print only the items in a

list of integers that are even?

18

Encapsulation Dilemma

 How can we provide access to individual

elements of an encapsulated data set without exposing the implementation of the data structure?

 This problem has been encountered many

times in the past

 Fortunately a standard solution has been

devised

slide-4
SLIDE 4

2/4/2011 4

19

Iterator

 An iterator is an object that provides access

to individual elements of a collection in a standard way without exposing the implementation details of that collection

 The collection can be real or virtual

 Traversing a list vs. generating a random number

21

Design Patterns

 Iterator is one example of a design pattern  A design pattern in software is a design that solves

a programming problem

 The design has application beyond the

particular problem at hand

 Concept originally proposed by

Christopher Alexander for architectural design

 GoF book  The iterator concept is one example of

a design pattern

22

Structure of a Design Pattern

 Name  Context for its use  Problem description  Solution

23

Iterator Design Pattern

 Name: Iterator  Context for its use: An encapsulated collection of data

elements

 Problem description: Need to provide client-controlled

access to individual elements of the collection without exposing the underlying data structure of the collection holding those elements

 Solution: Provide a separate object that

 has data structure-specific knowledge of the collection, but that  presents to the client generic operations that access elements in

an implementation-independent manner

The Standard Template Library.

 The STL provides a collection of data

structures and provides some generic algorithms, such as sorting.

 As its name suggests, the STL makes heavy

use of templates.

 All compiler that implement the standard has

the library available.

Basic Data Structures

 Data Structure is a representation of data and

the operations allowed on that data.

 Basic Data Structures

 List  Stack  Queue  Sets  Maps  Priority Queues

slide-5
SLIDE 5

2/4/2011 5 STL basic components

 Containers  Iterators  Algorithms

STL Containers

 A collection of objects (elements)  Must support:

 bool empty()  iterator begin()  iterator end()  int size()

STL Iterators

Iterators maintain a notion of a current position in the container and provides basic operations such as the ability to advance to the next position and access the item in the current position

Must support:

 itr++  itr-- (optional)  *itr  ==  != 

Each container could define several iterators

 const_iterator must be used to traverse const containers