Algorithms and Data Structures - WordPress.com · Algorithms and Data Structures 1
Data Structures
description
Transcript of Data Structures
![Page 1: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/1.jpg)
1
Data Structures
![Page 2: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/2.jpg)
2
The logical or mathematical model of a particular organization of data is called a data structure
DATA STRUCTURES
![Page 3: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/3.jpg)
3
A primitive data type holds a single piece of data–e.g. in Java: int, long, char, boolean etc.–Legal operations on integers: + - * / ...
A data structure structures data!–Usually more than one piece of data–Should provide legal operations on the data–The data might be joined together (e.g. in an array): a collection
DATA STRUCTURES
![Page 4: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/4.jpg)
4
Static vs. Dynamic StructuresStatic vs. Dynamic Structures
A static data structure has a fixed size
This meaning is different than those associated with the static modifier
Arrays are static; once you define the number of elements it can hold, it doesn’t change
A dynamic data structure grows and shrinks as required by the information it contains
![Page 5: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/5.jpg)
5
An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation.
Abstract Data Type
![Page 6: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/6.jpg)
6
Abstract Data Type
In computing, we view data from three perspectives:
Application level
View of the data within a particular problem
Logical level
An abstract view of the data values (the domain) and the set of operations to manipulate them
Implementation level
A specific representation of the structure to hold the data items and the coding of the operations in a programming language
![Page 7: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/7.jpg)
7
Problem Solving: Main Steps
1. Problem definition
2. Algorithm design / Algorithm specification
3. Algorithm analysis
4. Implementation
5. Testing
6. [Maintenance]
![Page 8: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/8.jpg)
8
Problem Definition
What is the task to be accomplished?
Calculate the average of the grades for a given student
What are the time / space / speed / performance requirements?
![Page 9: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/9.jpg)
9
. Algorithm Design / Specifications
Algorithm: Finite set of instructions that, if followed, accomplishes a particular task.Describe: in natural language / pseudo-code / diagrams / etc. Criteria to follow:
Input: Zero or more quantities (externally produced)Output: One or more quantities Definiteness: Clarity, precision of each instructionFiniteness: The algorithm has to stop after a finite (may be very large) number of stepsEffectiveness: Each instruction has to be basic enough and feasible
![Page 10: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/10.jpg)
10
Implementation, Testing, Maintenances
Implementation
Decide on the programming language to use
C, C++, Lisp, Java, Perl, Prolog, assembly, etc. , etc.
Write clean, well documented code
Test, test, test
Integrate feedback from users, fix bugs, ensure compatibility across different versions Maintenance
![Page 11: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/11.jpg)
11
Algorithm Analysis
Space complexity
How much space is required
Time complexity
How much time does it take to run the algorithm
Often, we deal with estimates!
![Page 12: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/12.jpg)
12
Space Complexity
Space complexity = The amount of memory required by an algorithm to run to completion
[Core dumps = the most often encountered cause is “memory leaks” – the amount of memory required larger than the memory available on a given system]
Some algorithms may be more efficient if data completely loaded into memory
Need to look also at system limitations
E.g. Classify 2GB of text in various categories [politics, tourism, sport, natural disasters, etc.] – can I afford to load the entire collection?
![Page 13: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/13.jpg)
13
Space Complexity (cont’d)
1. Fixed part: The size required to store certain data/variables, that is independent of the size of the problem:- e.g. name of the data collection- same size for classifying 2GB or 1MB of texts
2. Variable part: Space needed by variables, whose size is dependent on the size of the problem:- e.g. actual text - load 2GB of text VS. load 1MB of text
![Page 14: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/14.jpg)
14
Space Complexity (cont’d)
S(P) = c + S(instance characteristics)c = constant
Example:float sum (float* a, int n) {
float s = 0; for(int i = 0; i<n; i++) { s+ = a[i]; } return s;}Space? one word for n, one for a [passed by reference!], one for i constant space!
![Page 15: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/15.jpg)
15
Time Complexity
Often more important than space complexityspace available (for computer programs!) tends to be larger and largertime is still a problem for all of us
3-4GHz processors on the market researchers estimate that the computation of various transformations for 1 single DNA chain for one single protein on 1 TerraHZ computer would take about 1 year to run to completion
Algorithms running time is an important issue
![Page 16: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/16.jpg)
16
Running Time
Problem: prefix averagesGiven an array XCompute the array A such that A[i] is the average of elements X[0] … X[i], for i=0..n-1
Sol 1At each step i, compute the element X[i] by traversing the array A and determining the sum of its elements, respectively the average
Sol 2 At each step i update a sum of the elements in the array ACompute the element X[i] as sum/I
![Page 17: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/17.jpg)
17
Running time
Input
1 ms
2 ms
3 ms
4 ms
5 ms
A B C D E F G
worst-case
best-case}average-case?
Suppose the program includes an if-then statement that may execute or not: variable running timeTypically algorithms are measured by their worst case
![Page 18: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/18.jpg)
18
Experimental Approach
Write a program that implements the algorithmRun the program with data sets of varying size.Determine the actual running time using a system call to measure time (e.g. system (date) );
Problems?
![Page 19: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/19.jpg)
19
Experimental Approach
It is necessary to implement and test the algorithm in order to determine its running time.
Experiments can be done only on a limited set of inputs, and may not be indicative of the running time for other inputs.
The same hardware and software should be used in order to compare two algorithms. – condition very hard to achieve!
![Page 20: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/20.jpg)
20
Use a Theoretical Approach
Based on high-level description of the algorithms, rather than language dependent implementations
Makes possible an evaluation of the algorithms that is independent of the hardware and software environments
![Page 21: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/21.jpg)
21
Algorithm Description
How to describe algorithms independent of a programming language Pseudo-Code = a description of an algorithm that is
more structured than usual prose but less formal than a programming language
(Or diagrams)Example: find the maximum element of an array.
Algorithm arrayMax(A, n):Input: An array A storing n integers.Output: The maximum element in A.currentMax A[0]for i 1 to n -1 do
if currentMax < A[i] then currentMax A[i]return currentMax
![Page 22: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/22.jpg)
22
Properties of Big-Oh
Expressions: use standard mathematical symbols use for assignment ( ? in C/C++)use = for the equality relationship (? in C/C++)
Method Declarations: -Algorithm name(param1, param2) Programming Constructs:
decision structures: if ... then ... [else ..]while-loops while ... do repeat-loops: repeat ... until ... for-loop: for ... do array indexing: A[i]
Methodscalls: object method(args)returns: return value
Use commentsInstructions have to be basic enough and feasible!
![Page 23: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/23.jpg)
23
Asymptotic analysis - terminology
Special classes of algorithms:
logarithmic: O(log n)
linear: O(n)
quadratic: O(n2)
polynomial: O(nk), k ≥ 1
exponential: O(an), n > 1
Polynomial vs. exponential ?
Logarithmic vs. polynomial ?
![Page 24: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/24.jpg)
24
Some Numbers
log n n n log n n2 n3 2n
0 1 0 1 1 21 2 2 4 8 42 4 8 16 64 163 8 24 64 512 2564 16 64 256 4096 655365 32 160 1024 32768 4294967296
![Page 25: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/25.jpg)
25
Relatives of Big-Oh
“Relatives” of the Big-Oh
(f(n)): Big Omega – asymptotic lower bound
(f(n)): Big Theta – asymptotic tight bound
Big-Omega – think of it as the inverse of O(n)
g(n) is (f(n)) if f(n) is O(g(n))
Big-Theta – combine both Big-Oh and Big-Omega
f(n) is (g(n)) if f(n) is O(g(n)) and g(n) is (f(n))
Make the difference:
3n+3 is O(n) and is (n)
3n+3 is O(n2) but is not (n2)
![Page 26: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/26.jpg)
26
More “relatives”
Little-oh – f(n) is o(g(n)) if for any c>0 there is n0 such that f(n) < c(g(n)) for n > n0.Little-omegaLittle-theta
2n+3 is o(n2) 2n + 3 is o(n) ?
![Page 27: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/27.jpg)
27
Example
Remember the algorithm for computing prefix averages compute an array A starting with an array X every element A[i] is the average of all elements X[j] with j < iRemember some pseudo-code … Solution 1Algorithm prefixAverages1(X):Input: An n-element array X of numbers.Output: An n -element array A of numbers such that A[i] is the average of elements X[0], ... , X[i].
Let A be an array of n numbers.for i 0 to n - 1 do
a 0for j 0 to i do
a a + X[j] A[i] a/(i+ 1)
return array A
![Page 28: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/28.jpg)
28
Example (cont’d)
Algorithm prefixAverages2(X):
Input: An n-element array X of numbers.
Output: An n -element array A of numbers such that A[i] is the average of elements X[0], ... , X[i].
Let A be an array of n numbers.
s 0
for i 0 to n do
s s + X[i]
A[i] s/(i+ 1)
return array A
![Page 29: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/29.jpg)
29
Back to the original question
Which solution would you choose?O(n2) vs. O(n)
Some math …properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logbxa = alogbx
logba= logxa/logxb–properties of exponentials:a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a loga
b
bc = a c*loga
b
![Page 30: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/30.jpg)
30
Important Series
Sum of squares:
Sum of exponents:
Geometric series:
Special case when A = 2
20 + 21 + 22 + … + 2N = 2N+1 - 1
N largefor 36
)12)(1( 3
1
2 NNNNi
N
i
-1k and N largefor |1|
1
1
k
Ni
kN
i
k
1
11
0
A
AA
NN
i
i
N
i
NNiNNS1
2/)1(21)(
![Page 31: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/31.jpg)
31
Analyzing recursive algorithms
function foo (param A, param B) {
statement 1;
statement 2;
if (termination condition) {
return;
foo(A’, B’);
}
![Page 32: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/32.jpg)
32
Solving recursive equations by repeated substitution
T(n) = T(n/2) + c substitute for T(n/2)= T(n/4) + c + c substitute for T(n/4)= T(n/8) + c + c + c= T(n/23) + 3c in more compact form= …= T(n/2k) + kc “inductive leap”
T(n) = T(n/2logn) + clogn “choose k = logn”= T(n/n) + clogn= T(1) + clogn = b + clogn = θ(logn)
![Page 33: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/33.jpg)
33
Solving recursive equations by telescoping
T(n) = T(n/2) + c initial equation
T(n/2) = T(n/4) + c so this holds
T(n/4) = T(n/8) + c and this …
T(n/8) = T(n/16) + c and this …
…
T(4) = T(2) + c eventually …
T(2) = T(1) + c and this …
T(n) = T(1) + clogn sum equations, canceling the terms appearing on both sides
T(n) = θ(logn)
![Page 34: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/34.jpg)
34
RECURSION
Suppose P is a procedure containing either a CALL statement to itself or a CALL statement back to original procedure P .Then P is called a recursive procedure
Properties:
1. There must be certain criteria called basic criteria, for which the procedure does not call itself.
2. Each time the procedure does call itself (directly or indirectly), it must be closer to the base criteria.
![Page 35: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/35.jpg)
35
FACTORIAL WITHOUT RECURSION
FACTORIAL(FACT,N)
This procedure calculates N! and return the vale in the variable FACT .
1. If N ==0,then :Set FACT:=1, and Return.
2. Set FACT:=1[Initialize FACT for loop]
3. Repeat for K:=1 to N
Set FACT:=K*FACT
[END of loop]
4. Return.
![Page 36: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/36.jpg)
36
FACTORIAL WITH RECURSION
FACTORIAL(FACT,N)
This procedure calculates N! and return the vale in the variable FACT .
1. If N ==0,then :Set FACT:=1, and Return.
2. Call FACTORIAL(FACT,N-1).
3. Set FACT:=N*FACT.
4. Return.
![Page 37: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/37.jpg)
37
FACTORIAL EXAMPLE USING RECURSION
![Page 38: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/38.jpg)
38
FACTORIAL EXAMPLE USING RECURSION
![Page 39: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/39.jpg)
39
FACTORIAL EXAMPLE USING RECURSION
![Page 40: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/40.jpg)
40
FACTORIAL EXAMPLE USING RECURSION
![Page 41: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/41.jpg)
41
FACTORIAL EXAMPLE USING RECURSION
![Page 42: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/42.jpg)
42
FACTORIAL EXAMPLE USING RECURSION
![Page 43: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/43.jpg)
43
FACTORIAL EXAMPLE USING RECURSION
![Page 44: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/44.jpg)
44
FACTORIAL EXAMPLE USING RECURSION
![Page 45: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/45.jpg)
45
FACTORIAL EXAMPLE USING RECURSION
![Page 46: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/46.jpg)
46
FACTORIAL EXAMPLE USING RECURSION
![Page 47: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/47.jpg)
47
FACTORIAL EXAMPLE USING RECURSION
![Page 48: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/48.jpg)
48
Stack
A stack is a list that has addition and deletion of items only from one end.
It is like a stack of plates:
Plates can be added to the top of the stack.
Plates can be removed from the top of the stack.
This is an example of “Last in, First out”, (LIFO).
Adding an item is called “pushing” onto the stack.
Deleting an item is called “popping” off from the stack.
![Page 49: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/49.jpg)
49
STACK OPERATION (PUSH)
PUSH(STACK,TOP,MAXSTK,ITEM)
This procedure pushes an ITEM onto a stack.
1.[Stack already filled]
If TOP== MAXSTK, then: Print:OVERFLOW, and Return.
2. Set TOP:=TOP+1.[ Increases TOP by 1]
3. Set STACK[TOP]:=ITEM. [Inserting ITEM in new TOP position]
4. Return.
![Page 50: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/50.jpg)
50
STACK OPERATION (POP)
POP(STACK,TOP,ITEM)
This procedure deletes the top element of STACK and assigns it to the variable ITEM .
1.[Stack has an item to be to removed]
If TOP== 0, then: Print:UNDERFLOW, and Return.
2. Set ITEM:=STACK[top].[ Assigns TOP element to ITEM ]
3. Set TOP:=TOP-1. [Decreases TOP by 1]
4. Return.
![Page 51: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/51.jpg)
51
STACK EXAMPLES
Implementing a stack :
MAXSTK=7.
Data
push 43
push 23
push 53
pop
pop
push 100
![Page 52: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/52.jpg)
52
STACK EXAMPLES
7
6
5
4
2
1
TOP=0
MAXSTK=7
![Page 53: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/53.jpg)
53
STACK EXAMPLES
7
6
5
4
2
1
TOP=1
MAXSTK=7
PUSH(43)
43
![Page 54: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/54.jpg)
54
STACK EXAMPLES
7
6
5
4
2
1
TOP=2
MAXSTK=7
PUSH(23)
43
23
![Page 55: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/55.jpg)
55
STACK EXAMPLES
7
6
5
4
2
1
TOP=3
MAXSTK=7
PUSH(53)
43
23
53
![Page 56: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/56.jpg)
56
STACK EXAMPLES
7
6
5
4
2
1
TOP=2
MAXSTK=7
POP( )
43
23
![Page 57: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/57.jpg)
57
STACK EXAMPLES
7
6
5
4
2
1
TOP=0
MAXSTK=7
POP( )
43
![Page 58: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/58.jpg)
58
STACK EXAMPLES
7
6
5
4
2
1
TOP=0
MAXSTK=7
PUSH(100)
43
100
![Page 59: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/59.jpg)
59
STACK EXAMPLES
1. INFIX to POSTFIX
2. POSTFIX expression solving
3. N-QUEEN’S problem
![Page 60: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/60.jpg)
60
INFIX to POSTFIX
Properties while transforming infix to postfix expression
1. besides operands and operators, arithmetic expression contains left and right parentheses
2. We assume that the operators in q consist only of
1. Exponent
2. Multiplication
3. Division
4. Addition
5. Subtraction
![Page 61: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/61.jpg)
61
INFIX to POSTFIX
3. We have three levels of precedence
precedence operators
high right parentheses
exponent
multiplication, division
low addition, subtraction
![Page 62: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/62.jpg)
62
INFIX to POSTFIX
POLISH(Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.
1. Push “(“ into STACK and add “)” to the end of Q.
2. Scan Q from left to right and repeat Step 3 to 6 for each element of Q until the STACK is empty:
3. If an operand is encountered add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator is encountered then:
(a) repeatedly pop from STACK and to P each operator (on the top of STACK) which has the same precedence as or higher precedence this operator
(b) Add operator to STACK
![Page 63: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/63.jpg)
63
INFIX to POSTFIX
6. If a right parenthesis is encountered then:
(a) Repeatedly pop from STACK and add to P each operator( on the top of STACK) until a left parenthesis is encountered
(b) Remove the left parenthesis .[Do not add the left parenthesis to P]
7. Exit
![Page 64: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/64.jpg)
64
INFIX to POSTFIX
INFIX Expression: 3 + 2 * 4
POSTFIX Expression:7
6
5
4
2
1
![Page 65: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/65.jpg)
65
INFIX to POSTFIX
INFIX Expression: + 2 * 4
POSTFIX Expression: 37
6
5
4
2
1
![Page 66: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/66.jpg)
66
INFIX to POSTFIX
INFIX Expression: 2 * 4
POSTFIX Expression: 37
6
5
4
2
1
+
![Page 67: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/67.jpg)
67
INFIX to POSTFIX
INFIX Expression: * 4
POSTFIX Expression: 3 27
6
5
4
2
1
+
![Page 68: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/68.jpg)
68
INFIX to POSTFIX
INFIX Expression: 4
POSTFIX Expression: 3 27
6
5
4
2
1
+
*
![Page 69: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/69.jpg)
69
INFIX to POSTFIX
INFIX Expression:
POSTFIX Expression: 3 2 47
6
5
4
2
1
+
*
![Page 70: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/70.jpg)
70
INFIX to POSTFIX
INFIX Expression:
POSTFIX Expression: 3 2 4 *7
6
5
4
2
1
+
![Page 71: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/71.jpg)
71
INFIX to POSTFIX
INFIX Expression:
POSTFIX Expression: 3 2 4 * +7
6
5
4
2
1
![Page 72: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/72.jpg)
72
POSTFIX EXPRESSION SOLVING
This algorithm finds the VALUE of an arithmetic expression P written in postfix notation
1. Add a right parenthesis “)” at the end of P
2. Scan P from left to right and repeat step 3 and 4 for each element of P until the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator is uncounted then:
a. Remove the two elements of Stack, where A is the top element and B is the next top element
b. Evaluate B operator A
c. Pace the result of (b) back on STACK
![Page 73: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/73.jpg)
73
POSTFIX EXPRESSION SOLVING
5. Set VALUE equal to the top element on STACK
6. Exit
![Page 74: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/74.jpg)
74
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: 3 2 4 * +Push the numbers until operator comes 7
6
5
4
2
1
![Page 75: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/75.jpg)
75
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: 2 4 * +
7
6
5
4
2
1
3
![Page 76: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/76.jpg)
76
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: 4 * +
7
6
5
4
2
1
3
2
![Page 77: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/77.jpg)
77
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: * + Here we pop two time and perform multiplication on elements (4*2) and push the Result in to the stack
7
6
5
4
2
1
3
4
2
![Page 78: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/78.jpg)
78
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: +
7
6
5
4
2
1
3
8
![Page 79: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/79.jpg)
79
POSTFIX EXPRESSION SOLVING
POSTFIX Expression: 3 2 4 * +
7
6
5
4
2
1
11
![Page 80: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/80.jpg)
80
The N-Queens Problem
Can the queens be placed on the board so that no two queens are attacking each other in chess board
![Page 81: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/81.jpg)
81
The N-Queens Problem
Two queens are not allowed in the same rowTwo queens are not allowed in the same row
![Page 82: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/82.jpg)
82
The N-Queens Problem
Two queens are not allowed in the same row, or in the same column...Two queens are not allowed in the same row, or in the same column...
![Page 83: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/83.jpg)
83
The N-Queens Problem
Two queens are not allowed in Two queens are not allowed in the same row, or in the same the same row, or in the same column, or along the same column, or along the same diagonal.diagonal.r along the same diagonal.
![Page 84: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/84.jpg)
84
The N-Queens Problem
The number of queens, and the size of the board can vary.
The number of queens, and the size of the board can vary.
N Queens
N columns
![Page 85: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/85.jpg)
85
The 3-Queens Problem
The program uses a The program uses a stack to keep track of stack to keep track of where each queen is where each queen is placed.placed.
![Page 86: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/86.jpg)
86
The 3-Queens Problem
Each time the program decides to place a queen on the board, the position of the new queen is stored in a record which is placed in the stack.
ROW 1, COL 1
![Page 87: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/87.jpg)
87
The 3-Queens Problem
We also have an integer variable to keep track of how many rows have been filled so far.
ROW 1, COL 1
1 filled
![Page 88: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/88.jpg)
88
The 3-Queens Problem
Each time we try to place a new queen in the next row, we start by placing the queen in the first column
ROW 1, COL 1
1 filled
ROW 2, COL 1
![Page 89: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/89.jpg)
89
The 3-Queens Problem
if there is a conflict with another queen, then we shift the new queen to the next column.
ROW 1, COL 1
1 filled
ROW 2, COL 2
![Page 90: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/90.jpg)
90
The 3-Queens Problem
ROW 1, COL 1
1 filled
ROW 2, COL 3
When there are no When there are no conflicts, we stop and conflicts, we stop and add one to the value of add one to the value of filled.filled.
![Page 91: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/91.jpg)
91
The 3-Queens Problem
When there are no conflicts, we stop and add one to the value of filled.
ROW 1, COL 1
2 filled
ROW 2, COL 3
![Page 92: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/92.jpg)
92
The 3-Queens Problem
Let's look at the third row. The first position we try has a conflict
ROW 1, COL 1
2 filled
ROW 2, COL 3
ROW 3, COL 1
![Page 93: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/93.jpg)
93
The 3-Queens Problem
so we shift to column 2. But another conflict arises
ROW 1, COL 1
2 filled
ROW 2, COL 3
ROW 3, COL 2
![Page 94: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/94.jpg)
94
The 3-Queens Problem
and we shift to the third column.
Yet another conflict arises
ROW 1, COL 1
2 filled
ROW 2, COL 3
ROW 3, COL 3
![Page 95: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/95.jpg)
95
The 3-Queens Problem
and we shift to column 4. There's still a conflict in column 4, so we try to shift rightward again
ROW 1, COL 1
2 filled
ROW 2, COL 3
ROW 3, COL 4
![Page 96: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/96.jpg)
96
The 3-Queens Problem
but there's nowhere else to go.
ROW 1, COL 1
2 filled
ROW 2, COL 3
ROW 3, COL 4
![Page 97: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/97.jpg)
97
The 3-Queens Problem
When we run out of
room in a row: pop the stack, reduce filled by 1 and continue
working on the previous row.
ROW 1, COL 1
1 filled
ROW 2, COL 3
![Page 98: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/98.jpg)
98
The 3-Queens Problem
Now we continue working on row 2, shifting the queen to the right.
ROW 1, COL 1
1 filled
ROW 2, COL 4
![Page 99: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/99.jpg)
99
The 3-Queens Problem
This position has no conflicts, so we can increase filled by 1, and move to row 3.
ROW 1, COL 1
2 filled
ROW 2, COL 4
![Page 100: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/100.jpg)
100
The 3-Queens Problem
In row 3, we start again at the first column.
ROW 1, COL 1
2 filled
ROW 2, COL 4
ROW 3, COL 1
![Page 101: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/101.jpg)
101
The 3-Queens Problem
In row 3, we start again at the first column.
ROW 1, COL 1
3 filled
ROW 2, COL 4
ROW 3, COL 2
![Page 102: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/102.jpg)
102
QUEUES
A queue is a data structure that maintains a “first-in first-out” (FIFO) ordering.
In contrast, a stack maintains a “last-in first-out” (LIFO) ordering.
A queue adds new elements at the end. An element can only be removed at the front.
This is an abstraction of the “first-come first-served” practice.
![Page 103: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/103.jpg)
103
QUEUE OPERATIONS
A queue has two operations:
QINSERT
QDELETE
An enqueue operation adds new elements at the end of the queue or its tail. This is similar to the stack operation push; only that push now is done at the end of the array instead of at the front (or top).
A dequeue operation removes an element from the front of the array or its head.
![Page 104: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/104.jpg)
104
QUEUE INSERTION
QINSERT(rear, item)
1. IF rear == MAX_QUEUE_SIZE_1 then Print queue_full Return;
2. INFO [++rear] = item;
![Page 105: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/105.jpg)
105
QUEUE DELETION
QDELETE(front, rear)
1. IF front == rear then Return queue_empty( ); Return queue [++ front];
![Page 106: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/106.jpg)
106
QUEUE
OFFSET
DATA
0 1 2 3
0
rear
0
OFFSET
DATA
0 1 2 3
A
front
1
rear
1
OFFSET
DATA
0 1 2 3
A B
front
1
rear
2
OFFSET
DATA
0 1 2 3
B
front
2
rear
2
front
Insert A
Insert B
delete
![Page 107: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/107.jpg)
107
CIRCULAR QUEUE
When a new item is inserted at the rear, the to rear moves upwards.
Similarly, when an item is deleted from the queue the front arrow moves downwards.
After a few insert and delete operations the rear might reach the end of the queue and no more items can be inserted although the items from the front of the queue have been deleted and there is space in the queue.
![Page 108: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/108.jpg)
108
CIRCULAR QUEUE
To solve this problem, queues implement wrapping around. Such queues are called Circular Queues.
Both the front and the rear wrap around to the beginning of the array when they reached the MAX size of the queue.
It is also called as “Ring buffer”.
![Page 109: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/109.jpg)
109
CIRCULAR QUEUE INSERTION
QINSERT (QUEUE, N, FRONT, ITEM) This procedure insert an element ITEM into a queue.
1. [Queue already filled?]
IF ( FRONT==1 and REAR==N ) or FRONT ==REAR + 1,then: write: overflow, and Return
2.[Find new value of REAR] IF FRONT==NULL then [Queue initially empty.]
Set FRONT=1 and REAR=1
ELSE IF REAR ==N then Set REAR=1
ELSE set REAR=REAR+1
[End of if structure]
![Page 110: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/110.jpg)
110
CIRCULAR QUEUE INSERTION
3. Set QUEUE[REAR]=ITEM.[This inserts new element]
4. Return.
![Page 111: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/111.jpg)
111
CIRCULAR QUEUE DELETION
QDELETE(QUEE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the variable ITEM
1.[Queue already empty]
if FRONT=NULL then write UNDERFLOW, and Return
2. Set ITEM=QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT =REAR then [Queue has only one element to start]
Set FRONT=NULL and REAR=NULL
![Page 112: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/112.jpg)
112
CIRCULAR QUEUE DELETION
Else if FRONT ==N then
Set FRONT=1
Else
Set FRONT=FRONT +1
[End of if statement]
4.Return
![Page 113: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/113.jpg)
113
CIRCULAR QUEUE DELETION
EMPTY QUEUE
[2] [3] [2] [3]
[1] [4] [1] [4]
[0] [5] [0] [5]
front = 0 front = 0 rear = 0 rear = 3
J2
J1
J3
![Page 114: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/114.jpg)
114
CIRCULAR QUEUE DELETION
[2] [3] [2] [3]
[1] [4][1] [4]
[0] [5] [0] [5]
front =0rear = 5
front =4rear =3
J2 J3
J1 J4
J5 J6 J5
J7
J8 J9
![Page 115: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/115.jpg)
115
PRIORITY QUEUE
A priority queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules
1. An element of a higher priority is processed before any elements of lower priority
2. Two elements with the same priority are processed according to the order in which they were added to the queue
![Page 116: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/116.jpg)
116
ARRAY REPRESENTATION PRIORITY QUEUE
Use a separate queue for each level of priority
Each such queue will appear in its own circular array and must have its own pair of pointers FRONT and REAR
In fact, if each queue is allocated the same amount of space in two-dimensional array QUEUE can be used instead of linear array
![Page 117: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/117.jpg)
117
DELETION ON PRIORITY QUEUE
Deletion
This algorithm deletes and processes the first element in a priority queue maintained by a two-dimensional array QUEUE
1.[Find the first nonempty queue] Find the smallest k that FRONT!=NULL
2.Delete and process the front element in row K of QUEUE
![Page 118: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/118.jpg)
118
INSERTION ON PRIORITY QUEUE
Insertion
This algorithm adds an ITEM with priority number M to a priority queue maintained by a two-dimensional array QUEUE
1. Insert ITEM as the rear element in row M of QUEUE
2. Exit
![Page 119: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/119.jpg)
119
LINKED LIST
Linked list
Linear collection of self-referential class objects, called nodes
Connected by pointer links
Accessed via a pointer to the first node of the list
Subsequent nodes are accessed via the link-pointer member of the current node
Link pointer in the last node is set to null to mark the list’s end
Use a linked list instead of an array when
You have an unpredictable number of data elements
Your list needs to be sorted quickly
![Page 120: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/120.jpg)
120
TYPES OF LINKED LIST
Types of linked lists:
Singly linked list
Begins with a pointer to the first node
Terminates with a null pointer
Only traversed in one direction
Circular, singly linked
Pointer in the last node points back to the first node
Doubly linked list
Two “start pointers” – first element and last element
Each node has a forward pointer and a backward pointer
Allows traversals both forwards and backwards
Circular, doubly linked list
Forward pointer of the last node points to the first node and backward pointer of the first node points to the last node
![Page 121: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/121.jpg)
121
linked list
Start Node
A placeholder node at the beginning of the list, used to simplify list processing. It doesn’t hold any data
Tail Node
A placeholder node at the end of the list, used to simplify list processing
![Page 122: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/122.jpg)
122
Singly linked list
Single Linked ListConsists of data elements and reference to the
next Node in the linked list
First node is accessed by reference
Last node is set to NULL
A B CStart Tail
![Page 123: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/123.jpg)
123
SINGLE LINKED LIST INSERTION
INSFIRST(INFO, LINK, START, AVAIL, ITEM)
This algorithm inserts ITEM as the first node in the list
1. [OVERFLOW?] If AVAIL=NULL then :Write OVERFLOW and Exit
2. [Remove first node from AVIL list] Set NEW =AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM [Copies new data into new node]
4. Set LINK[NEW]=START [new node now points to original first node]
5. Set START=NEW [Changes START so it points to the node]
6. Exit
![Page 124: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/124.jpg)
124
SINGLE LINKED LIST INSERTION
s X c d a
current
1
start
![Page 125: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/125.jpg)
125
SINGLE LINKED LIST INSERTION
s X c d a
current
1
start
![Page 126: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/126.jpg)
126
SINGLE LINKED LIST INSERTION
s X c d a
current
1
start
![Page 127: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/127.jpg)
127
SINGLE LINKED LIST INSERTION
s X c d a
current
1
start
![Page 128: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/128.jpg)
128
SINGLE LINKED LIST INSERTION
s X c d a
current
1
start
![Page 129: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/129.jpg)
129
SINGLE LINKED LIST INSERTION
INSLOC(INFO, LINK,START, AVAIL, LOC, ITEM) This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts ITEM as the first node when LOC=NULL
1. [OVERFLOW ?] If AVAIL==NULL then write OVERFLOW and EXIT
2. [Remove first node from AVAIL list]
3. Set INFO[NEW]=ITEM[copies new data into new node]
4. If LOC==NULL then :[insert as first node] Set LINK[NEW]=Start and START=NEW
else [insert after node with location LOC] Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW
end of if structure
5. exit
![Page 130: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/130.jpg)
130
SINGLE LINKED LIST INSERTION
9
6 X532start
temp
current
![Page 131: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/131.jpg)
131
SINGLE LINKED LIST INSERTION
9
6 X532start
temp
current
![Page 132: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/132.jpg)
132
SINGLE LINKED LIST INSERTION
9
6 X532start
temp
current
![Page 133: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/133.jpg)
133
SINGLE LINKED LIST INSERTION
9
6 X532start
temp
current
![Page 134: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/134.jpg)
134
SINGLE LINKED LIST DELETION
DELETE(INFO, LINK,START, AVAIL, ITEM) This algorithm deletes from a linked list the first node N which contains the given ITEM of information
1. [Use procedure FIND given after this algorithm] FIND(INFO, LINK, START, ITEM, LOC, LOCP)
2. If LOC==NULL then: Write ITEM not in list and exit
3. If LOCP==NUL then Set START=LINK[START]
else : Set LINK[LOCP]=LINK[LOC]
3. {Return deleted node to the AVAIL list] Set LINK[LOC]=AVAIL and AVAIL=LOC
4. Exit
![Page 135: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/135.jpg)
135
SINGLE LINKED LIST DELETION
FINDB(INFO, START, ITEM, LOC, LOCP) This procedure finds the location LOC of first node N which contains ITEM and the location LOVP of the node preceding N. if ITEM does not appear in the list then the procedure sets LOC=NULL and if ITEM appears in the first node then it sets LOCP=NULL
1. [list empty?] if START==NULL then Set LOC =NULL and LOCP==NULL and return
2. [ITEM in first node ] if INFO[START]==ITEM then Set LOC=START and LOCP=NULL and return
3. Set SAVE=START and PRT=LINK[START]
4. Repeat steps 5 and 6 while PTR!=NULL
5. If INFO[PTR]==ITEM then Set LOC=PTR and LOCP=NULL
![Page 136: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/136.jpg)
136
SINGLE LINKED LIST DELETION
6. Set SAVE=PTR and PTR=LINK[PTR]
7. Set LOC=NULL
8. Return
![Page 137: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/137.jpg)
137
SINGLE LINKED LIST DELETION
6 X532start
LOCP LOC
![Page 138: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/138.jpg)
138
SINGLE LINKED LIST DELETION
6 X532start
LOCP LOC
![Page 139: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/139.jpg)
139
SINGLE LINKED LIST DELETION
6 X532start
LOCP LOC
![Page 140: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/140.jpg)
140
Double Linked Lists
Consists of nodes with two linked references one points to the previous and other to the next node
Maximises the needs of list traversals
Compared to single list inserting and deleting nodes is a bit slower as both the links had to be updated
It requires the extra storage space for the second list
A B Cstart
Tail
![Page 141: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/141.jpg)
141
Insertion Double Linked Lists
INSERTWL(INFO,FORW, BACK, STACK,AVAIL,LOCA,LOCB, ITEM)
1. [OVERFLOW?] If AVAIL==NULL then write OVER FLOW and exit
2. [Remove node from AVAIL list and copy new data into node] Set NEW=AVIAL, AVIAL=FORW[AVAIL] INFO[NEW]=ITEM
3. [Insert node into list] Set FORW[LOCA]=NEW, FORW[NEW]=LOCB BACK[LOCB]=NEW BACK[NEW]=LOCA
4. Exit
![Page 142: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/142.jpg)
142
Insertion Double Linked Lists
3 4 7start
TailNode A Node B
LOC A LOC B
7NEW
![Page 143: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/143.jpg)
143
Insertion Double Linked Lists
3 4 7start
TailNode A Node B
LOC A LOC B
7NEW
![Page 144: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/144.jpg)
144
Insertion Double Linked Lists
3 4 7start
TailNode A Node B
LOC A LOC B
7NEW
![Page 145: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/145.jpg)
145
Insertion Double Linked Lists
3 4 7start
TailNode A Node B
LOC A LOC B
7NEW
![Page 146: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/146.jpg)
146
Insertion Double Linked Lists
3 4 7start
TailNode A Node B
LOC A LOC B
7NEW
![Page 147: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/147.jpg)
147
Deletion Double Linked Lists
DELTWL(INFO, FORW, BACK, START, AVAIL, LOC)
1. [Delete node]
Set FORE[BACK[LOC]]=FORW[LOC]
Set BACK[FORW[LOC]]=BACK[LOC]
2. [Return node to AVAIL list]
Set FORW[LOC]=AVAIL and AVAIL=LOC
3. Exit
![Page 148: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/148.jpg)
148
Deletion Double Linked Lists
A B Cstart
Tail
LOC
![Page 149: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/149.jpg)
149
Deletion Double Linked Lists
A B Cstart
Tail
LOC
![Page 150: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/150.jpg)
150
Deletion Double Linked Lists
A B Cstart
Tail
![Page 151: Data Structures](https://reader036.fdocuments.in/reader036/viewer/2022081603/56813ffe550346895dab2e04/html5/thumbnails/151.jpg)
151
Deletion Double Linked Lists
A B Cstart
Tail