Review of basic data structures
-
Upload
deepa-rani -
Category
Education
-
view
123 -
download
1
description
Transcript of Review of basic data structures
![Page 1: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/1.jpg)
Review of Basic Data Structures
Data Structures through C++
![Page 2: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/2.jpg)
2
Syllabus
Review of Basic Data Structures The List ADT Stack ADT Queue ADT Array and Linked Implementations using
Template Classes in C++.
![Page 3: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/3.jpg)
3
Objective
To learn the implementation of list, stack, and queue ADTs in C++ using templates.
![Page 4: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/4.jpg)
4
LINEAR LISTArray Representation
![Page 5: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/5.jpg)
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)
![Page 6: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/6.jpg)
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)
![Page 7: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/7.jpg)
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.
![Page 8: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/8.jpg)
8
The Linear List Data Structure… Operations on a Linear List
![Page 9: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/9.jpg)
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.
![Page 10: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/10.jpg)
10
The Abstract Data Type linearList…
![Page 11: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/11.jpg)
11
The Abstract Class linearList
![Page 12: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/12.jpg)
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.
![Page 13: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/13.jpg)
13
Array Representation…
Different ways of mapping [5, 2, 4, 8, 1]
![Page 14: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/14.jpg)
14
Removing and Inserting an Element
![Page 15: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/15.jpg)
15
Class Definition for
arrayList
![Page 16: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/16.jpg)
16
Linear ListLinked Representation
![Page 17: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/17.jpg)
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.
![Page 18: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/18.jpg)
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.
![Page 19: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/19.jpg)
19
Removing an Element from a Singly Linked List…
![Page 20: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/20.jpg)
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.
![Page 21: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/21.jpg)
21
Inserting an Element into a Singly Linked List…
![Page 22: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/22.jpg)
22
The Struct chainNode
Defines a data type called chainNode for storing nodes.
![Page 23: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/23.jpg)
23
The Class chain
Implements a linear list as singly linked list of nodes.
![Page 24: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/24.jpg)
24
StacksArray and Linked List Representation
![Page 25: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/25.jpg)
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.
![Page 26: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/26.jpg)
26
Stack Example
Adding element E to the following stack.
![Page 27: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/27.jpg)
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?
![Page 28: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/28.jpg)
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.
![Page 29: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/29.jpg)
29
The Stack ADT
![Page 30: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/30.jpg)
30
The Abstract Class stack
![Page 31: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/31.jpg)
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.
![Page 32: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/32.jpg)
32
The Class derivedArrayStack
![Page 33: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/33.jpg)
33
The Class arrayStack
Better in performance than derivedArrayStack
![Page 34: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/34.jpg)
34
The Class arrayStack…
![Page 35: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/35.jpg)
35
Stack Run Times in Seconds
![Page 36: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/36.jpg)
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.
![Page 37: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/37.jpg)
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.
![Page 38: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/38.jpg)
38
The Class linkedStack
![Page 39: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/39.jpg)
39
QueuesArray and Linked Representations
![Page 40: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/40.jpg)
40
Queue Definition
A queue is a linear list in which insertions take place from rear end and deletions take place from front end.
![Page 41: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/41.jpg)
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?
![Page 42: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/42.jpg)
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.
![Page 43: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/43.jpg)
43
The Queue ADT
![Page 44: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/44.jpg)
44
The Abstract Class queue
![Page 45: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/45.jpg)
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.
![Page 46: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/46.jpg)
46
Queue: Array Representation…
![Page 47: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/47.jpg)
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.
![Page 48: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/48.jpg)
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.
![Page 49: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/49.jpg)
49
Improving Deletion in Queues…
![Page 50: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/50.jpg)
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.
![Page 51: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/51.jpg)
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.
![Page 52: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/52.jpg)
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.
![Page 53: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/53.jpg)
53
Circular Queues
![Page 54: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/54.jpg)
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)
![Page 55: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/55.jpg)
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.
![Page 56: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/56.jpg)
56
The Class arrayQueue
Same as arrayStack, except push operation.
![Page 57: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/57.jpg)
57
Queue: Linked Representation
May be implemented in the following two ways:
But, which is better?
![Page 58: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/58.jpg)
58
Queue: Linked Representation…
Initial values queueFront = queueBack = NULL.
Boundary value queueFront = NULL iff queue is empty.
All operations require O(1) time.
![Page 59: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/59.jpg)
59
Linked Queue: Pushing an Element
![Page 60: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/60.jpg)
60
Linked Queue: Popping an Element
![Page 61: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/61.jpg)
61
linkedQueue : push method
![Page 62: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/62.jpg)
62
linkedQueue : pop method
![Page 63: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/63.jpg)
63
Summary
Linear Lists Array lists Linked lists
Stacks Array implementation Linked implementation
Queues Array implementation Linked implementation
![Page 64: Review of basic data structures](https://reader033.fdocuments.net/reader033/viewer/2022061223/54c346844a795969698b457f/html5/thumbnails/64.jpg)
64
The EndReview of Basic Data Structures