review
DESCRIPTION
Review. Pseudo Code Basic elements of Pseudo code Basic operations of Pseudo code Flow Chart Symbols used in flow charts Examples. List. List Data Structure List operations List Implementation Array Linked List. The LIST Data Structure. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/1.jpg)
Review
1
Pseudo CodeBasic elements of Pseudo codeBasic operations of Pseudo codeFlow ChartSymbols used in flow chartsExamples
![Page 2: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/2.jpg)
List
2
List Data StructureList operationsList ImplementationArrayLinked List
![Page 3: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/3.jpg)
The LIST Data Structure The List is among the most generic of data
structures.
Real life:
a. shopping list, b. groceries list, c. list of people to invite to dinnerd. List of presents to get
3
![Page 4: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/4.jpg)
Lists A list is collection of items that are all of the
same type (grocery items, integers, names)
The items, or elements of the list, are stored in some particular order
It is possible to insert new elements into various positions in the list and remove any element of the list
4
![Page 5: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/5.jpg)
Lists List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be arranged in a list:
(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the second element, and so on
The order is important here; this is not just a random collection of elements, it is an ordered collection
5
![Page 6: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/6.jpg)
Lists List is a set of elements in a linear order.
For example, data values a1, a2, a3, a4 can be arranged in a list:
(a3, a1, a2, a4)
In this list, a3, is the first element, a1 is the second element, and so on
The order is important here; this is not just a random collection of elements, it is an ordered collection
6
![Page 7: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/7.jpg)
List OperationsUseful operations createList(): create a new list (presumably empty) copy(): set one list to be a copy of another clear(); clear a list (remove all elments) insert(X, ?): Insert element X at a particular position
in the list remove(?): Remove element at some position in
the list get(?): Get element at a given position update(X, ?): replace the element at a given position
with X find(X): determine if the element X is in the list length(): return the length of the list.
7
![Page 8: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/8.jpg)
List Operations We need to decide what is meant by “particular
position”; we have used “?” for this.
There are two possibilities:
1. Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays
2. Use a “current” marker or pointer to refer to a particular position in the list.
8
![Page 9: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/9.jpg)
List Operations We need to decide what is meant by “particular
position”; we have used “?” for this.
There are two possibilities:
1. Use the actual index of element: insert after element 3, get element number 6. This approach is taken by arrays
2. Use a “current” marker or pointer to refer to a particular position in the list.
9
![Page 10: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/10.jpg)
List Operations If we use the “current” marker, the following
four methods would be useful:
start(): moves the “current” pointer to the first element.
tail(): moves the “current” pointer to the last element.
next(): move the current position forward one element
back(): move the current position backward one element
10
![Page 11: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/11.jpg)
Implementing Lists
We have designed the interface for the List; we now must consider how to implement that interface.
11
![Page 12: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/12.jpg)
Implementing Lists We have designed the interface for the List; we
now must consider how to implement that interface.
Implementing Lists using an array: for example, the list of integers (2, 6, 8, 7, 1) could be represented as:
A 6 8 7 11 2 3 4 5
2current
3size
5
12
![Page 13: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/13.jpg)
List Implementation add(9); current position is 3. The new list would thus
be: (2, 6, 8, 9, 7, 1) We will need to shift everything to the right of 8 one
place to the right to make place for the new element ‘9’.
current3
size5
step 1: A 6 8 7 11 2 3 4 5
26
current4
size6
step 2: A 6 8 7 11 2 3 4 5
26
9
notice: current pointsto new element
13
![Page 14: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/14.jpg)
Implementing Lists next():
current4
size6
A 6 8 7 11 2 3 4 5
26
95
14
![Page 15: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/15.jpg)
Implementing Lists
There are special cases for positioning the current pointer:
a. past the last array cell b. before the first cell
We will have to worry about these when we write the actual code.
15
![Page 16: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/16.jpg)
Implementing Lists remove(): removes the element at the current
index
current5
size6
A 6 8 11 2 3 4 5
26
9
5
Step 1:
current5
size5
A 6 8 11 2 3 4 5
2 9Step 2:
16
![Page 17: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/17.jpg)
Implementing Lists remove(): removes the element at the current
index
We fill the blank spot left by the removal of 7 by
shifting the values to the right of position 5 over to the left one space.
current5
size5
A 6 8 11 2 3 4 5
2 9Step 2:
current5
size6
A 6 8 11 2 3 4 5
26
9
5
Step 1:
17
![Page 18: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/18.jpg)
Implementing Listsfind(X): traverse the array until X is located.
int find(int X){int j;for(j=1; j < size+1; j++ ) if( A[j] == X ) break; if( j < size+1 ) { // found X current = j; // current points to where X found return 1; // 1 for true}return 0; // 0 (false) indicates not found}
18
![Page 19: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/19.jpg)
Implementing Lists
Other operations:
get() return A[current];update(X) A[current] = X;length() return size;back() current--;start() current = 1;end() current = size;
19
![Page 20: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/20.jpg)
Analysis of Array Lists
add we have to move every element to the right of
current to make space for the new element. Worst-case is when we insert at the beginning; we
have to move every element right one place. Average-case: on average we may have to move
half of the elements
20
![Page 21: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/21.jpg)
Analysis of Array Lists remove
Worst-case: remove at the beginning, must shift all remaining elements to the left.
Average-case: expect to move half of the elements.
find Worst-case: may have to search the entire array Average-case: search at most half the array.
Other operations are one-step.
21
![Page 22: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/22.jpg)
List Using Linked MemoryVarious cells of memory are not allocated
consecutively in memory.
22
![Page 23: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/23.jpg)
List Using Linked Memory Various cells of memory are not allocated
consecutively in memory. With arrays, the second element was right next to
the first element.
23
![Page 24: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/24.jpg)
List Using Linked Memory Various cells of memory are not allocated
consecutively in memory. With arrays, the second element was right next to
the first element. Now the first element must explicitly tell us where to
look for the second element
24
![Page 25: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/25.jpg)
List Using Linked Memory Various cells of memory are not allocated
consecutively in memory. With arrays, the second element was right next to
the first element. Now the first element must explicitly tell us where to
look for the second element. Do this by holding the memory address of the
second element
25
![Page 26: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/26.jpg)
List Using Linked Memory Various cells of memory are not allocated
consecutively in memory. With arrays, the second element was right
next to the first element. Now the first element must explicitly tell us
where to look for the second element. Do this by holding the memory address of
the second element This is what we call Linked List
26
![Page 27: Review](https://reader036.vdocument.in/reader036/viewer/2022070420/56815f0b550346895dcdcacc/html5/thumbnails/27.jpg)
Summary
27
List Data StructureList operationsList ImplementationArrayLinked List