review of basic data structures

Post on 24-Jan-2015

122 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Review of Basic Data Structures

Data Structures through C++

2

Syllabus

Review of Basic Data Structures The List ADT Stack ADT Queue ADT Array and Linked Implementations using

Template Classes in C++.

3

Objective

To learn the implementation of list, stack, and queue ADTs in C++ using templates.

4

LINEAR LISTArray Representation

5

Data Objects and Structures A data object is a set of instances or

values. Examples:

An instance may be primitive or may be composed of other instances. (Eg: 123, total)

6

Data Objects and Structures… A data structure is a data object

together with the relationships that exist among the instances and among the individual elements that compose an instance. These relationships are provided by specifying

the operations of interest. Most frequently used data objects and their

operations are already implemented in C++ as primitive data types.▪ Boolean (bool)▪ Integer (int)

7

The Linear List Data Structure Each instance of linear list (or ordered list) is

an ordered collection of elements. e0, e1, e2, e3, e4, …, en-1

Index of ei is i. n is the list length or size. When n = 0, the list is empty. When n > 0, e0 is the zeroth element, en-1 is the last.

e0 comes before e1, e1 comes before e2, and so on. Examples:

List of students in a class in alphabetic order. List of percentages of students in decreasing order.

8

The Linear List Data Structure… Operations on a Linear List

9

The Abstract Data Type linearList

ADT provides a specification of Instances, and Operations that are to be performed.

It is independent of any programming language.

All representations of ADT must satisfy the specification.

10

The Abstract Data Type linearList…

11

The Abstract Class linearList

12

Array Representation

An array is used to store the list of elements.

If we use one dimensional array, the elements of the array are accessed as: A[0] accesses zeroth element. A[1] accesses first element. A[n] accesses nth element.

Elements are mapped to positions in the array. location (i) = i is the most natural mapping. other ways of mapping are also possible.

13

Array Representation…

Different ways of mapping [5, 2, 4, 8, 1]

14

Removing and Inserting an Element

15

Class Definition for

arrayList

16

Linear ListLinked Representation

17

Singly Linked Lists

Each element of an instance of a data object is represented in a cell or a node.

Each node keeps the location of next node, called a link or a pointer.

As each node links to only one other node, the structure is called as a singly linked list or a chain.

18

Removing an Element from a Singly Linked List

Removing the third element from the list involves: Locating the second node. Linking the second node to the fourth

node. Similar steps are followed for

removing any element.

19

Removing an Element from a Singly Linked List…

20

Inserting an Element into a Singly Linked List

To insert an element as ith element, Find the location on i-1th element. Insert the new node after that element.

21

Inserting an Element into a Singly Linked List…

22

The Struct chainNode

Defines a data type called chainNode for storing nodes.

23

The Class chain

Implements a linear list as singly linked list of nodes.

24

StacksArray and Linked List Representation

25

Stack Definition

A stack is a linear list in which insertions and removals take place at the same end, called top. The other end is called bottom. Insertions are also called as pushes. Removals are also called as pops.

A stack is a LIFO (Last In First Out) list.

26

Stack Example

Adding element E to the following stack.

27

Stack Applications in Real World

Stack of books in a library. Stack of CDs. Stack of papers in a printer.

Identify more examples of stacks from real world?

28

Stack Applications in Computers

For implementing function calls. For implementing recursive

functions. For converting infix expression to

postfix. For evaluating postfix expressions. For construction of compilers. For depth first search of a graph.

29

The Stack ADT

30

The Abstract Class stack

31

Stack: Array Representation

Can be implemented in two ways: derivedArrayStack▪ Derived from arrayList and stack classes.▪ Uses the methods of arrayList for performing

push and pop operations.▪ Efficiency of this class is low due to the usage

of methods of arrayList for push and pop. arrayStack▪ Derived from stack class.▪ Improves the run-time performance.

32

The Class derivedArrayStack

33

The Class arrayStack

Better in performance than derivedArrayStack

34

The Class arrayStack…

35

Stack Run Times in Seconds

36

Stack: Linked Representation

Left end of the chain or the right end of the chain can be used as a stack top.

Using right end as top takes more time.

So, left end of the stack is used as top.

Push and pop operations are done at the left end.

37

Stack: Linked Representation

Can be implemented in two ways: derivedLinkedStack▪ Derived from chain and stack classes.▪ Can be obtained by changing

derivedArrayStack. linkedStack▪ Derived from stack.▪ Improves the run-time performance.

38

The Class linkedStack

39

QueuesArray and Linked Representations

40

Queue Definition

A queue is a linear list in which insertions take place from rear end and deletions take place from front end.

41

Queue Applications in Real World

Railway reservation counters. Soda vending machines. Normally at all service centers.

What other example queues can you think of?

42

Queues in Computers

For job processing in operating systems.

For printing documents in printers. For breadth-first search of a graph. For file handling in distributed file

systems.

43

The Queue ADT

44

The Abstract Class queue

45

Queue: Array Representation

ith element may be stored in ith location. location(i) = i

queueFront and queueBack are used to denote front and rear of the queue. queueFront = 0. Queue size = queueBack+1. queueBack = -1, for empty queue.

46

Queue: Array Representation…

47

Insertion and Deletion in a Queue

Insertion Increments queueBack by 1. Places new element in

queue[queueBack]. Takes O(1) time.

Deletion Shifts all the elements one position to

the left. Takes O(n) time, where n is number of

elements. Very time consuming.

48

Improving Deletion in Queues

We can improve the deletion operation by the following method: Use the equation▪ location(i) = location(front element) + i.

Does not require shifting of elements by one position to the left.

Increment queueFront by 1 for every deletion.

Queue is empty, if queueBack < queueFront.

Takes O(1) time.

49

Improving Deletion in Queues…

50

Efficient Utilization of Space in Queues

When the second equation is used, every deletion increments queueFront by 1.

This may result in a situation shown below, where no new elements can be inserted even when space is available.

51

Efficient Utilization of Space in Queues…

One method is To shift all the elements to the left end,

which leaves space at the right end allowing insertions.

Deletion time is O(1). Insertion time is O(arrayLength) in worst

case.

52

Efficient Utilization of Space in Queues…

Another method is To insert the elements from the

queueFront when no space is available at the queueBack.

Both insertion and deletion take O(1) time.

In this case, the array is viewed as a circle.

The queue is called as a circular queue.

53

Circular Queues

54

Circular Queues…

Position 0 is preceded by arrayLength-1.

When queueBack = arrayLength-1, then the new element is inserted into position 0.

Uses the following equation: location(i) = (location(front element) + i)

% arrayLength

Queue is empty iff queueFront = queueBack = 0 (initially) queueFront = queueBack (otherwise)

55

Circular Queues…

Even when Queue is full, the condition queueFront = queueBack becomes true.

To avoid this confusion , a queue is never made full, and the size is doubled whenever such a situation arises.

56

The Class arrayQueue

Same as arrayStack, except push operation.

57

Queue: Linked Representation

May be implemented in the following two ways:

But, which is better?

58

Queue: Linked Representation…

Initial values queueFront = queueBack = NULL.

Boundary value queueFront = NULL iff queue is empty.

All operations require O(1) time.

59

Linked Queue: Pushing an Element

60

Linked Queue: Popping an Element

61

linkedQueue : push method

62

linkedQueue : pop method

63

Summary

Linear Lists Array lists Linked lists

Stacks Array implementation Linked implementation

Queues Array implementation Linked implementation

64

The EndReview of Basic Data Structures

top related