Linear Data Structure Final
Linear Data Structure Final
Lecture 3
Linear Data Structure
1
Linear Arrays
• Linear array is a list of finite number n of
homogeneous data elements
• Elements are referenced respectively by an index set
consisting of n consecutive numbers
• Elements are stored respectively in successive
memory locations
2
Linear Arrays in Memory
• If LA is a linear array in the memory of the
computer, the elements of LA are stored in
successive memory cells
• Computer needs to keep track of the address
of the first element of LA, denoted by
Base(LA)
• Computer calculates the address of any
element of LA by the following formula
LOC(LA[K]) = Base(LA) +w(K – Lower bound)
Where w is the number of words per memory cell for the array LA
3
Traversing a Linear Array
• Linear array LA has lower bound LB and upper
bound UB and LA is traversed by applying an
operation PROCESS to each element of LA
1. [Initialize counter] Set K:=LB
2. Repeat Steps 3 and 4 while K≤ UB
3. [Visit element] Apply PROCESS to LA[K]
4. [Increase counter] Set K:=K+1
[End of Step 2 loop]
5. Exit.
4
Traversing a Linear Array
• Linear array traversing algorithm using repeat-for
loop
1. Repeat for K=LB to UB
Apply PROCESS to LA[K]
[End of loop]
3. Exit.
5
Inserting into Linear Array
• This algorithm inserts an element ITEM into the Kth
position in LA
1. Set J:=N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move Jth element downward] Set LA[J+1]:=LA[J]
4. [Decrease counter] Set J:=J-1.
[End of Step 2 loop]
5. [Insert element] Set LA[K] := ITEM
6. [Reset N] Set N:=N+1
7. Exit
Here LA is linear array with N elements and K is a positive
integer such that K ≤ N
6
Deleting from a Linear Array
• This algorithm deletes Kth element from a linear
array LA and assigns it to a variable ITEM
1. Set ITEM:=LA[K]
2. Repeat for J = K to N-1
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of loop]
3. [Reset the number N of elements in LA] Set N=N-1
4. Exit
Here LA is linear array with N elements and K is a positive
integer such that K ≤ N
7
Representation of Two-Dimensional Arrays in
memory
• If A be a two dimensional m x n array, the array will
be represented in memory by a block of m.n
sequential memory locations
• The programming language will store the array A
either
– Column by column also called column major order, or
– Row by row, in row major order
8
Linked List
• A linear collection of data elements, called nodes, where the
linear order is given by means of pointers.
• Each node is divided into two parts:
– Information of the element (also called Data)
– Link field or nextpointer field containing the address of the next node
(Also called Next)
• It may be either singly linked or doubly linked, it may be
sorted or not, and it may be circular or not
9
Linked List
• Searching a linked list
– The procedure SEARCH(INFO, LINK, START, ITEM, LOC)
finds the location LOC of the node where ITEM first
appears in LIST, or sets LOC = NULL
1. Set PTR := START
2. Repeat Step 3 while PTR ≠ NULL:
3. If ITEM = INFO[PTR], then:
Set LOC := PTR, and Exit.
Else:
Set PTR:=LINK[PTR] [PTR points to next node]
[End of If structure]
4. Set LOC:=NULL
5. Exit
10
1 2 3 4 5 6 7
• Inserting P at the
beginning of the list Start = 1
1. Insert at Data[Avail] Avail = 6
2. Set inserted position, Data[6] = P
I.P. = Avail
I.P. = 6
3. Set Avail = Next [Avail]
4. Set Next[I.P.] = Start
Avail = 7
5. Set Start = I.P. Next[6]=1
Start = 6
1 2 3 4 5 6 7
Data A C B E D P
Next 2 4 0 5 3 1 8
11
1 2 3 4 5 6 7
12
1 2 3 4 5 6 7 8
14
1 2 3 4 5 6 7 8
Data A C B E
Linked List Next 2 4 0 3 6 7 8 ….
15
Stack
• Is a list of elements in
which element may be Push( GG) Pop( )
8 Top
Top
inserted or deleted only Top 7 8
8
at the top 6 FF 7 GG
• The elements are 7
5 EE 6 FF
removed in reverse order 6 FF
4 DD 5 EE
of that in which they were 5 EE
3 CC 4 DD
inserted 2 BB 3 CC
4 DD
• Push – inserting an 3 CC
1 AA 2 BB
element 2 BB
1 AA
• Pop – removing an 1 AA
element
16
Stack
• Array Representation
• Linked Representation
17
Stack
• Arithmetic Expression
– Infix Expression : A+B C*D E-F G/H
– Postfix Expression
– Prefix Expression
– Polish Notation: Named after the Polish mathematician
Jan Lukasiewicz
– In Polish notation (also called prefix notation) operator
symbol is placed before its two operands, for example:
+AB *CD -EF /GH
18
Stack
• Polish Notation
– We translate infix expression into Polish notation using
brackets [ ] to indicate partial translation:
(A+B)*C = [+AB]*C = *+ABC
A+(B*C) = A+[*BC] = +A*BC
(A+B)/(C-D) = [+AB]/[-CD] = /+AB-CD
– The order in which the operation are to be performed is
completely determined by the positions of the operators and
operands in the expression
– Parentheses are not needed when writing expressions in Polish
notation
– In reverse Polish notation (also called postfix or suffix) operator
symbol is placed after its two operands:
AB+ CD* EF- GH/
19
Stack
• Arithmetic Expression
– The computer usually evaluates an arithmetic expression
written in infix notation in two steps
– First it converts the expression to postfix notation
– Then it evaluates the postfix expression
– The stack is the main tool that is used to accomplish the
given task
20
Stack
• Infix to Postfix Notation
a) (A-B)*(D/E)
= [AB-]*[DE/]
= AB-DE/*
21
Stack
• Infix to Postfix Notation
b) (A+B↑D)/(E-F)+G
= (A+[BD↑])/[EF-]+G
= [ABD↑+]/[EF-]+G
= [ABD↑+EF-/]+G
= ABD↑+EF-/G+
22
Stack
• Infix to Postfix Notation
c) A*(B+D)/E-F*(G+H/K)
= A*[BD+]/E-F*(G+[HK/])
= A*[BD+]/E-F*[GHK/+]
= [ABD+*]/E-[FGHK/+*]
= [ABD+*E/]-[FGHK+*]
= ABD+*E/FGHK/+*-
23
Stack
• Postfix to Infix Notation
d) A*(B+D)/E-F*(G+H/K)
= A*[BD+]/E-F*(G+[HK/])
= A*[BD+]/E-F*[GHK/+]
= [ABD+*]/E-[FGHK/+*]
= [ABD+*E/]-[FGHK+*]
= ABD+*E/FGHK/+*-
24
Stack
• Infix to Postfix Using Stack
– Let Q is an arithmetic expression written in infix and P is the
equivalent postfix expression
1. Push “(” onto Stack and add “)” to the end of Q
2. Scan Q from left to right and repeat Steps 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 the Stack and add to P each operator which
has the same precedence as or higher precedence than Ⓧ
b. Add Ⓧ to Stack
6. If right parenthesis is encountered, then:
a. Repeatedly pop from Stack and add to P each operator until a left
parenthesis is encountered
b. Remove the left parenthesis and do not add it to P
7. Exit
25
Scan Stack Expression (P)
D ( D
Stack
• Infix to Postfix using
Stack Example
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C)
26
Scan Stack Expression
D ( D
+ (+ D
Stack
• Infix to Postfix using
Stack Example
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C)
27
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
28
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using
Stack Example
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C)
29
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C)
30
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C)
31
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C)
32
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
33
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
34
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
35
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
36
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
37
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
38
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
39
Scan Stack Expression
D ( D
+ (+ D
Stack ( (+( D
E (+( DE
• Infix to Postfix using / (+(/ DE
Stack Example F (+(/ DEF
D+(E/F-(D/A↑E)/B)*C - (+(- DEF/
Q=D+(E/F-(D/A↑E)/B)*C) ( (+(-( DEF/
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
40
Scan Stack Expression
D ( D
+ (+ D
Stack (
E
(+(
(+(
D
DE
- (+(- DEF/
D+(E/F-(D/A↑E)/B)*C
( (+(-( DEF/
Q=D+(E/F-(D/A↑E)/B)*C)
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
B (+(-/ DEF/DAE↑/B
41
Scan Stack Expression
D ( D
+ (+ D
Stack (
E
(+(
(+(
D
DE
- (+(- DEF/
D+(E/F-(D/A↑E)/B)*C
( (+(-( DEF/
Q=D+(E/F-(D/A↑E)/B)*C)
D (+(-( DEF/D
/ (+(-(/ DEF/D
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
B (+(-/ DEF/DAE↑/B
) (+ DEF/DAE↑/B/-
42
+ (+ D
( (+( D
E (+( DE
Stack / (+(/ DE
F (+(/ DEF
A (+(-(/ DEF/DA
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
B (+(-/ DEF/DAE↑/B
) (+ DEF/DAE↑/B/-
* (+* DEF/DAE↑/B/-
43
( (+( D
E (+( DE
/ (+(/ DE
- (+(- DEF/
↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
B (+(-/ DEF/DAE↑/B
) (+ DEF/DAE↑/B/-
* (+* DEF/DAE↑/B/-
C (+* DEF/DAE↑/B/-C
44
E (+( DE
/ (+(/ DE
F (+(/ DEF
Stack -
(
(+(-
(+(-(
DEF/
DEF/
A (+(-(/ DEF/DA
D+(E/F-(D/A↑E)/B)*C
Q=D+(E/F-(D/A↑E)/B)*C) ↑ (+(-(/↑ DEF/DA
E (+(-(/↑ DEF/DAE
) (+(- DEF/DAE↑/
/ (+(-/ DEF/DAE↑/
B (+(-/ DEF/DAE↑/B
) (+ DEF/DAE↑/B/-
* (+* DEF/DAE↑/B/-
C (+* DEF/DAE↑/B/-C
) DEF/DAE↑/B/-C*+
45
Stack
• Postfix to Infix or Evaluation of Postfix Expression
– Finds Value of a postfix arithmetic expression P
1. Add a right parenthesis “)” at the end of P
2. Scan P from left to right and repeat Steps 3 and 4 for
each element of P until the “)” is encountered
3. If an operand is encountered, put it on Stack
4. If an operator Ⓧ is encountered, then:
a. Remove the top two elements of Stack, where B is the top
element and A is the next to top element
b. Evaluate A Ⓧ B
c. Place the result back in Stack
5. Set Value equal to the top element of Stack
6. Exit
46
Scan Stack
D D,
E D, E
Stack F D, E, F
• Postfix to Infix / D, (E/F)
D D, (E/F), D
or Evaluation of
A D, (E/F), D, A
Postfix Expression E D, (E/F), D, A, E
Example ↑ D, (E/F), D, (A↑E)
/ D, (E/F), (D / (A↑E) )
DEF/DAE↑/B/-C*+ B D, (E/F), (D / (A↑E) ), B
/ D, (E/F), ( (D / (A↑E) ) / B)
– D, (E/F) – ( (D / (A↑E) ) / B)
P=DEF/DAE↑/B/-C*+)
C D, (E/F) – ( (D / (A↑E) ) / B), C
* D, ( (E/F) – ( (D / (A↑E) ) / B) * C))
+ D + ( (E/F) – ( (D / (A↑E) ) / B) * C))
47