215
Resmi N.G. References: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja

M2_Linear Data Structures

  • Upload
    resming

  • View
    44

  • Download
    1

Embed Size (px)

Citation preview

Page 1: M2_Linear Data Structures

Resmi N.G.

References:

Data Structures and Algorithms:

Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman

A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja

Page 2: M2_Linear Data Structures

Module 2� Module II (18 hours)

� Linear Data Structures - Stacks – Queues -Lists -

Dequeues - Linked List - singly, doubly linked and

circular lists - Application of linked lists - Polynomial

Manipulation - Stack & Queue implementation using

Array & Linked List - Typical problems - Conversion of

infix to postfix - Evaluation of postfix expression –

priority queues

10/25/2012 2CS 09 303 Data Structures - Module 2

Page 3: M2_Linear Data Structures

LIST

• Flexible data structure: grows and shrinks on demand.

• Elements can be accessed, inserted, or deleted at any position within

a list.

• Lists can be concatenated or split into sub-lists.

• Mathematically, a list is a sequence of zero or more elements of a

given type.

� Represented by a comma separated sequence of elements

a1,a2,…an where n>=0 and each ai is of a given type.

• n is the length of the list.

10/25/2012 3CS 09 303 Data Structures - Module 2

Page 4: M2_Linear Data Structures

Features Of List

� Can be linearly ordered according to their position on

the list.

� Concatenation and splitting possible

� Main application areas are:

=> Information retrieval

=> Programming language translation

=> Programming language simulation

10/25/2012 4CS 09 303 Data Structures - Module 2

Page 5: M2_Linear Data Structures

LIST OPERATIONS

� INSERT(x, p,L) : Inserts x at position p in list L.

� Move elements at p and following positions to next higher

positions.

� Insert x at position p in list L.

� If L is a1, a2, …, an, then L becomes a1, a2, …, ap-1, x, ap, …,

an.

� If p is END (L), then L becomes a1, a2, …, an, x.

10/25/2012 5CS 09 303 Data Structures - Module 2

Page 6: M2_Linear Data Structures

� END(L) : returns the last position of list L

� LOCATE(x, L) : returns the position of x on list L.

� If x appears more than once, it returns the position of first

occurrence of x.

� If x does not appear at all, then it returns END(L).

� RETRIEVE(p,L) : returns element at position p on list L.

� Result is undefined if p = END (L) or if L has no position p.

10/25/2012 6CS 09 303 Data Structures - Module 2

Page 7: M2_Linear Data Structures

� DELETE(p, L) : deletes the element at position p of list L.

� If list is a1,a2, …, an, then the list L becomes a1, a2, … ap-1, ap+1, …,an.

� NEXT(p, L): returns position following p on list L.

� PREVIOUS(p, L):returns position preceding p on list L.

� MAKENULL(L) : Makes the list L empty and returns the position

END(L).

� FIRST(L) : returns first position on list L.

� PRINTLIST(L) :prints elements of L in order of occurrence.

10/25/2012 7CS 09 303 Data Structures - Module 2

Page 8: M2_Linear Data Structures

Implementation Of List2 Ways

� Array Implementation

�Pointer Implementation(Linked List representation)

10/25/2012 8CS 09 303 Data Structures - Module 2

Page 9: M2_Linear Data Structures

Array Implementation Of List

� Elements are stored in contiguous cells of an array.

� Easily traversed

� Elements can be appended readily to the end of the list.

� Definition of LIST: is a record that contains 2 fields:

� an array of elements with maximum required length

� an integer “last” : indicating position of the last list element

in the array.

10/25/2012 9CS 09 303 Data Structures - Module 2

Page 10: M2_Linear Data Structures

Array Implementation Of List

(Definition)const

MAXLEN = 100;

type

LIST = record

elements:array[1..MAXLEN] of elementtype;

last :integer;

end;

position = integer;

10/25/2012 10CS 09 303 Data Structures - Module 2

Page 11: M2_Linear Data Structures

function END (var L: List) : position;

begin

return (L.last + 1) {gives total no. of elements in the

list}

end; {END}

10/25/2012 11CS 09 303 Data Structures - Module 2

END(L) Algorithm

0 1 2 3

50 107 15 201

L.last

Page 12: M2_Linear Data Structures

INSERTION Algorithmprocedure INSERT(x : elementtype; p : position; var L: LIST)

{INSERT places x at position p in list L}

var q:position;

begin

if L.last >= MAXLEN then

error (‘list is full’)

else if (p > L.last + 1 ) or ( p < 1) then

error(‘position does not exist’)

else

begin

for q := L.last downto p do

{shift elements at p, p+1…… down by one position}

L.elements[q+1] := L.elements[q];

L.last := L.last+1;

L.elements[p] := x;

end

end;{INSERT}

10/25/2012 12CS 09 303 Data Structures - Module 2

Page 13: M2_Linear Data Structures

Insertion

10/25/2012 CS 09 303 Data Structures - Module 2 13

0 1 2 3

47

� Insert (47, 0, L)

� L.last := L.last + 1 = -1 + 1 = 0

� L.elements[0] := 47;

L.last

p

p

Page 14: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 14

0 1 2 3

47

47 59

� Insert (59, 1, L)

� L.last := L.last + 1 = 0 + 1 = 1

� L.elements[1] := 59;

L.last

L.last p

p

Page 15: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 15

0 1 2 3

47 59

47 59 65

� Insert (65, 2, L)

� L.last := L.last + 1 = 1 + 1 = 2

� L.elements[2] := 65;

L.last

L.last p

p

Page 16: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 16

0 1 2 3

47 59 65

47 59 65

� Insert (53, 1, L)

� Shift elements down to insert at position 1. Start with q = L.last.

� L.elements[q+1] := L.elements[q]

� { Move element at position 2 to position 3.

� L.elements[2+1] = L.elements[2]

� L.elements[3] = L.elements[2] }

L.last

L.lastp

q+1

q

qp

Page 17: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 17

0 1 2 3

47 59 65

47 59 65

� Decrement q until q = p. So, q = 1.

� L.elements[q+1] := L.elements[q]

� { Move element at position 1 to position 2.

� L.elements[1+1] = L.elements[1]

� L.elements[2] = L.elements[1] }

L.last

L.lastp

q+1

q

qp

Page 18: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 18

0 1 2 3

47 59 65

47 53 59 65

� Now, q = p = 1. So, insert element at position 1.

� L.last := L.last + 1 = 2+1 = 3;

� L.elements[p] := L.elements[1] = 53;

L.last

L.lastp q

qp

Page 19: M2_Linear Data Structures

DELETION Algorithm

procedure DELETE (p:position; var L : LIST)

{DELETE removes the element at position p of list L}

var q:position;

begin

if ( p > L.last ) or ( p < 1) then

error(‘position does not exist’)

else

begin

L.last := L.last-1;

for q := p to L.last do

{shift elements at p+1, p+2, …up one position}

L.elements[q] := L.elements[q+1];

end

end;{DELETE}

10/25/2012 19CS 09 303 Data Structures - Module 2

Page 20: M2_Linear Data Structures

Deletion

10/25/2012 CS 09 303 Data Structures - Module 2 20

0 1 2 3

47 53 59 65

47 53 59 65

� Delete (1, L)

� L.last := L.last - 1 = 3-1= 2L.last

p

p L.last

Page 21: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 21

0 1 2 3

47 53 59 65

47 59 65

� Move element at location q+1 = 2 to location q = 1.

� L.elements[q] := L.elements[q+1];

L.lastp

pL.last

� To delete element at 1, shift elements at 2 and 3 up by one

position. Start with q = p. ie. q = 1.

q

q+1q

q+1

Page 22: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 22

0 1 2 3

47 59 59 65

47 59 65

� Move element at location q+1 = 3 to location q = 2.

� L.elements[q] := L.elements[q+1];

L.lastp

pL.last

� Increment q until q = L.last = 2.

� So, q = q + 1 = 2.

q q+1

q

Page 23: M2_Linear Data Structures

LOCATE Algorithm

function LOCATE( x:elementtype; var L:LIST) : position;

{LOCATE returns position of x on list L}

var q:position;

begin

for q:=1 to L.last do

if L.elements[q] = x then

return(q);

else

return( L.last + 1 ) {if not found}

end;{LOCATE}

10/25/2012 23CS 09 303 Data Structures - Module 2

Page 24: M2_Linear Data Structures

Removing Duplicates in a LIST

Procedure PURGE (Var L:List); {PURGE removes duplicate elements from list L}

Var p,q : position; {p will be the current position in L, and

q will move ahead to find equal elements}

Begin

(1) p := FIRST(L);

(2) While p <> END(L) do begin

(3) q := NEXT(p, L);

(4) while q <> END(L) do

(5) if same (RETRIEVE (p, L), RETRIEVE (q, L ) then

(6) DELETE(q,L);

(7) else

(8) q:=NEXT(q,L);

(9) p:=NEXT(p,L)

(10) End;

End; {PURGE}10/25/2012 24CS 09 303 Data Structures - Module 2

Page 25: M2_Linear Data Structures

Pointer Implementation Of List:

(LINKED LIST)

• Use of special data structure - Linked List

• Singly-linked cells which use pointers to link successive list

elements are used here.

• Does not require contiguous memory for storage.

• Allocates memory dynamically

• Linked List is made up of cells, and each cell contains:

� �Element of the list

� �Pointer to next cell on the list

• If the list is a1,a2,…an, the cell holding ai has a pointer to the cell

holding ai+1, for i = 1, 2, …n-1.

• Header holds no element but points to cell holding a1. The cell

holding an has a nil pointer.

10/25/2012 25CS 09 303 Data Structures - Module 2

Page 26: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 26

Page 27: M2_Linear Data Structures

Classification Of Linked Lists

� Linear or singly linked list

� Doubly Linked List

� Circular Linked List

� Circular Doubly linked List

10/25/2012 27CS 09 303 Data Structures - Module 2

Page 28: M2_Linear Data Structures

Linear Singly Linked List

� Definition

type

celltype = record

element:elementtype;

next: celltype;

end;

LIST= celltype;

position = celltype;

10/25/2012 28CS 09 303 Data Structures - Module 2

Page 29: M2_Linear Data Structures

Operations Of Singly Linked List

� END(L) Algorithm

Function END(var L : List) : position;

{END returns a pointer to the last cell of L}

Var

q:position;

begin

(1) q:=L;

(2) While q .next <> nil do

(3) q:= q .next;

(4) return(q)

end; {END}

10/25/2012 29CS 09 303 Data Structures - Module 2

Page 30: M2_Linear Data Structures

� Steps involved are :

� Find the correct location

� Insert the element

10/25/2012 CS 09 303 Data Structures - Module 2 30

Operations Of Singly Linked List

INSERTION

Page 31: M2_Linear Data Structures

Finding the Correct Location� Three possible positions:

� The front

� The end

� Somewhere in between the existing cells

10/25/2012 31CS 09 303 Data Structures - Module 2

Page 32: M2_Linear Data Structures

head

Inserting to the Front

� There is no work to find the location.

� Insertion at the beginning of the list

48 17 142head 93

10/25/2012 32CS 09 303 Data Structures - Module 2

No Worries!

Page 33: M2_Linear Data Structures

Inserting to the End

�Find the end of the list(when at NIL)

48 17 142head //93 //

No Worries!

10/25/2012 33CS 09 303 Data Structures - Module 2

Page 34: M2_Linear Data Structures

Inserting to the Middle

�Used when the order isimportant.

17 48 142head //93 //142

10/25/2012 34CS 09 303 Data Structures - Module 2

Page 35: M2_Linear Data Structures

Operations Of Singly Linked List(contd..)

10/25/2012 35CS 09 303 Data Structures - Module 2

� INSERTION Algorithm

Procedure INSERT(var x : elementtype, p : position);

{Inserts a cell with element ‘x’ after position ‘p’}

Var

temp : position;

Begin

(1) temp := p . next;

(2) New(p .next);

(3) p .next .element := x;

(4) p .next .next := temp;

End;{INSERT}

Page 36: M2_Linear Data Structures

Explanation� (1) temp := p . next;

� {address of the cell next to p is stored in temp}

10/25/2012 CS 09 303 Data Structures - Module 2 36

p Temp

2 6 8

head

4

Page 37: M2_Linear Data Structures

� (2) New(p .next);

� {A new cell is created and its address is assigned to p .next}

10/25/2012 CS 09 303 Data Structures - Module 2 37

p temp

p . next = address of new cell

p . next

2 6 8

head

4

Page 38: M2_Linear Data Structures

� (3) p .next .element := x;

� {Element at p .next is assigned as x}

10/25/2012 CS 09 303 Data Structures - Module 2 38

p temp

p . next . element = 5

p . next

2 6 8

head

4

5

Page 39: M2_Linear Data Structures

� (3) p .next . next := temp;

� {Next of p .next is assigned as temp}

10/25/2012 CS 09 303 Data Structures - Module 2 39

p temp

p . next

p . next . next = temp

2 6 8

head

4

5

Page 40: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 40

2 6 8

head

4 5

Page 41: M2_Linear Data Structures

Operations Of Singly Linked List(contd..)

� DELETION Algorithm

� Deletes the element after position p.

Procedure DELETE( var p:position);

Begin

p .next := p .next .next;

End;{DELETE}

10/25/2012 41CS 09 303 Data Structures - Module 2

Page 42: M2_Linear Data Structures

4 17

head

426

Deletion at the front

10/25/2012 CS 09 303 Data Structures - Module 2 42

Head has the address of first node

Page 43: M2_Linear Data Structures

4 17

head

426

10/25/2012 43CS 09 303 Data Structures - Module 2

Deletion at the front

Head is made to point to the next cell.

Page 44: M2_Linear Data Structures

4 17

head

42

10/25/2012 44CS 09 303 Data Structures - Module 2

Deletion from in between the cells

Page 45: M2_Linear Data Structures

4 17

head

42

10/25/2012 45CS 09 303 Data Structures - Module 2

Deletion from in between the cells

Page 46: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 46

p p . next p . next . next

p . next = p . next . next

Deletion from in between the cells

Page 47: M2_Linear Data Structures

Operations Of Singly Linked

List(contd..)

10/25/2012 47CS 09 303 Data Structures - Module 2

� LOCATE Algorithm

Function LOCATE( var x : elementtype; var L:LIST):position;

Var p : position;

Begin

p := L;

while p . next <> nil do

if p . next . element = x then

return (p);

else

p := p . next ;

return (p) {if not found}

End;{LOCATE}

Page 48: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 48CS 09 303 Data Structures - Module 2

Deleting a particular element

Page 49: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 49CS 09 303 Data Structures - Module 2

Start searching from the beginning of the list L.

Initially, p = L;

p

Page 50: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 50CS 09 303 Data Structures - Module 2

Check if p.next.element = 4.

NO. So, move onto next cell.

p = p.next

p

Page 51: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 51CS 09 303 Data Structures - Module 2

p

Check if p.next.element = 4.

YES. So, perform the operation

p.next = p.next.next

Page 52: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 52CS 09 303 Data Structures - Module 2

p .next = p.next.nextp

Page 53: M2_Linear Data Structures

4 17

head

426

Target = 4

10/25/2012 53CS 09 303 Data Structures - Module 2

Page 54: M2_Linear Data Structures

17

head

426

10/25/2012 54CS 09 303 Data Structures - Module 2

Page 55: M2_Linear Data Structures

Operations Of Singly Linked List(contd..)

� MAKENULL Algorithm

Function MAKENULL(Var L:LIST):position;

Begin

new(L);

L .next := nil;

return(L);

End; {MAKENULL}

10/25/2012 55CS 09 303 Data Structures - Module 2

Creates an empty list L

Page 56: M2_Linear Data Structures

Doubly Linked List� Each cell has a pointer to the next and previous cells

on the list.

� Given an element, it is easy to determine the preceding and following elements quickly.

� Can be used to traverse a list in both forward and backward directions.

10/25/2012 56CS 09 303 Data Structures - Module 2

Page 57: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 57

Page 58: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 58

Page 59: M2_Linear Data Structures

Doubly Linked ListDefinition

Type

celltype = record

element : elementtype;

next , previous : celltype;

End;

position = celltype;

newnode = celltype;

10/25/2012 59CS 09 303 Data Structures - Module 2

Page 60: M2_Linear Data Structures

Doubly Linked ListINSERTION ALGORITHM

Procedure INSERT( var element:elementtype; varpos:position)

Var Temp : position;

Begin

Temp := L;

i : =1;

while (i < pos and Temp < > NIL )

begin

Temp : = Temp . Next;

i : = i + 1;

End;

10/25/2012 60CS 09 303 Data Structures - Module 2

Page 61: M2_Linear Data Structures

If Temp < > NIL and i = pos then

begin

New( newNode);

newNode .element : = element;

newNode .next : = Temp .next;newNode . previous : = Temp;

Temp .next . previous : = newNode;Temp . next := newNode;

end { if }Else begin

error ( ‘position not found ’ )end; { INSERT}

10/25/2012 61CS 09 303 Data Structures - Module 2

Page 62: M2_Linear Data Structures

Explanation

10/25/2012 CS 09 303 Data Structures - Module 2 62

5 15 20

head

Check if i<pos, and temp<>NIL.

i=1, pos = 2 and temp<>NIL.

So, move forward.

temp = temp.next

i = i+1

temp

temp := L;

i : =1;

Page 63: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 63

5 15 20

head

temp Check if i<pos, and temp<>NIL.

i=2, pos = 2 and temp<>NIL.

So, do insertion.

Page 64: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 64

5 15 20

head

temp

If Temp < > NIL and i = pos then

begin

new( newNode); {Creates a new node in memory and assigns

its address to ‘newnode’.}

newNode .element : = element; {Inserts element at newnode.}

10

Page 65: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 65

5 15 20

head

temp

10 newNode .next : = temp .next;

Page 66: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 66

5 15 20

head

temp

10

newNode . previous : = temp;

Page 67: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 67

5 15 20

head

temp

10

temp .next . previous : = newNode;

temp . next

Page 68: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 68

5 15 20

head

temp

10

temp .next : = newNode;

Page 69: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 69

5 15 20

head

10

Page 70: M2_Linear Data Structures

Deletion in Doubly Linked List

10/25/2012 CS 09 303 Data Structures - Module 2 70

Page 71: M2_Linear Data Structures

DELETION ALGORITHM

Procedure DELETE ( Var p : position);

Begin

if p . Previous < > nil then

{ deleted cell not the first }

p . Previous . Next : = p . Next;

if p . Next < > nil then

{ deleted cell not the last }

p . Next . Previous : = p . Previous

End { DELETE}

10/25/2012 71CS 09 303 Data Structures - Module 2

Page 72: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 72

p

p . previous . next = p . next;

p. next . previous = p . previous ;

Page 73: M2_Linear Data Structures

STACK

• An Abstract Data Type

• Special List : insertions & deletions at one

end(top)

• Also called LIFO(Last In First Out) list.

10/25/2012 73CS 09 303 Data Structures - Module 2

Page 74: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 74

Page 75: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 75

Page 76: M2_Linear Data Structures

Operations performed on stack

� MAKENULL(S) : Make stack S an empty stack.

� TOP(S) : Return the top element of stack S.

� POP(S) : Delete the top element from the stack. {check

for stack empty condition}

� PUSH(x,S) : Insert the element x at the top of stack S.

{ Stack overflow condition should be checked}

� EMPTY(S) : Returns true if S is an empty stack.

10/25/2012 76CS 09 303 Data Structures - Module 2

Page 77: M2_Linear Data Structures

Stack Implementation

� Static implementation(Using arrays)

� Dynamic implementation(Using pointers)

10/25/2012 77CS 09 303 Data Structures - Module 2

Page 78: M2_Linear Data Structures

Definition of Array-Based Stack� type

� STACK = record

� top : integer;

� elements : array[1..MAXLENGTH] of elementtype;

� end;

10/25/2012 CS 09 303 Data Structures - Module 2 78

Page 79: M2_Linear Data Structures

Array Implementation

Procedure PUSH( var x:elementtype; Var s:STACK);

Begin

If s.top = MAXLENGTH -1 then

error(‘stack is full’)

Else

begin

s.top:=s.top+1;

s.elements[s.top]:=x;

End;

End;{PUSH}

10/25/2012 79CS 09 303 Data Structures - Module 2

Page 80: M2_Linear Data Structures

� Initial stack

10/25/2012 CS 09 303 Data Structures - Module 2 80

3

2

1

0

Top = -1

Page 81: M2_Linear Data Structures

� Push (12, S)

10/25/2012 CS 09 303 Data Structures - Module 2 81

s.top:=s.top+1 = -1 + 1 = 0;

s.elements[s.top]:= s.elements[0] = 12;

3

2

1

0 12 Top = 0

Page 82: M2_Linear Data Structures

� Push (34, S)

10/25/2012 CS 09 303 Data Structures - Module 2 82

s.top:=s.top+1 = 0 + 1 = 1;

s.elements[s.top]:= s.elements[1] = 34;

3

2

1 34

0 12

Top = 1

Page 83: M2_Linear Data Structures

� Push (47, S)

10/25/2012 CS 09 303 Data Structures - Module 2 83

s.top:=s.top+1 = 1 + 1 = 2;

s.elements[s.top]:= s.elements[2] = 47;

3

2 47

1 34

0 12

Top = 2

Page 84: M2_Linear Data Structures

� Push (53, S)

10/25/2012 CS 09 303 Data Structures - Module 2 84

s.top:=s.top+1 = 2 + 1 = 2;

s.elements[s.top]:= s.elements[3] = 53;

3 53

2 47

1 34

0 12

Top = 3

� Stack full (Top = max-1);

Page 85: M2_Linear Data Structures

Procedure POP( Var s:STACK);

begin

if EMPTY(s) then

error(‘stack is empty’)

else

s.top := s.top-1;

end;{POP}

10/25/2012 85CS 09 303 Data Structures - Module 2

Page 86: M2_Linear Data Structures

Procedure MAKENULL( Var s:STACK);

Begin

s.top:= -1;

End;{MAKENULL}

10/25/2012 86CS 09 303 Data Structures - Module 2

Page 87: M2_Linear Data Structures

Function EMPTY( Var s:STACK):boolean;

begin

If s.top<0 then

return(true);

else

return(false);

end;{EMPTY}

10/25/2012 87CS 09 303 Data Structures - Module 2

Page 88: M2_Linear Data Structures

Function TOP ( Var s:STACK):elementtype;

begin

If EMPTY(s) then

error(‘stack is empty’)

else

return(s.elements[s.top]);

end;{TOP}

10/25/2012 88CS 09 303 Data Structures - Module 2

Page 89: M2_Linear Data Structures

Applications of stack� Explicit and implicit recursive calls

� Conversion of Infix to postfix

� Postfix expression evaluation

� Conversion of infix to prefix

� Prefix expression evaluation

� Parenthesis checker

10/25/2012 89CS 09 303 Data Structures - Module 2

Page 90: M2_Linear Data Structures

Applications of stack� Expression : operands together with operator

Infix notation(A+B)

Prefix notation( +AB)

Postfix notation( AB+)

� Operator precedence

^ (Exponential ) ………….Highest

*, /(mul, div) ………… Next highest

+, - (add,sub) ………… Lowest

10/25/2012 90CS 09 303 Data Structures - Module 2

Page 91: M2_Linear Data Structures

Applications of stack

� Conversion from infix to postfix

Rules

1. Parenthesize the expression starting from left to right

2. The operands associated with the operator having higher

precedence will be parenthesized first.

3. The sub expression which has been converted to postfix

is to be treated as single operand.

4. Once the expression is converted to postfix form,

remove the parenthesis

10/25/2012 91CS 09 303 Data Structures - Module 2

Page 92: M2_Linear Data Structures

Infix to postfix conversion

algorithmP =>postfix expression

Q =>infix 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 eachelement of Q until the stack is empty.

(3) If an operand is encountered, add it to P.

(4) If an operator # is encountered, then

(a) Repeatedly pop from stack and add to P eachoperator (on the top of stack) which has equal or higherprecedence than #.

10/25/2012 92CS 09 303 Data Structures - Module 2

Page 93: M2_Linear Data Structures

(b) Add # to stack.

5. If a left parenthesis is encountered, push it onto the

stack.

6. If a right parenthesis is encountered then

(a) Repeatedly pop from stack and add to P, until a

left parenthesis is encountered.

(b) Remove the left parenthesis.

[ do not add the left parenthesis to P].

7. Exit

# �Symbolizes any operator in Q

10/25/2012 93CS 09 303 Data Structures - Module 2

Page 94: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 94

Page 95: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 95

Page 96: M2_Linear Data Structures

Evaluating Postfix expression(1) Add a right parenthesis “)” at the end of P[this acts as a

sentinel].

(2) Scan P from left to right and repeat steps 3 and 4 foreach element of P until the sentinel “)” is encountered.

(3) If an operand is encountered, put it on STACK.

(4) If an operator # is encountered, then

(a) Remove the two top elements of STACK ,where A isthe top element and B is the next-to-top element.

(b) Evaluate B # A.

(c) Place the result onto STACK.

5) RESULT equal to the top element on stack.

6) Exit

10/25/2012 96CS 09 303 Data Structures - Module 2

Page 97: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 97

Evaluate 45+72-*

Page 98: M2_Linear Data Structures

Evaluate 59+2*65*+

10/25/2012 CS 09 303 Data Structures - Module 2 98

Page 99: M2_Linear Data Structures

Infix to prefix conversion algorithmI => infix expression

O => output expression

P => prefix expression

(1) Push “)“ onto stack and add “(” to the beginning of I.

(2) Scan I from right to left and repeat steps 3 to 6 for eachelement of I until the stack is empty.

(3) If an operand is encountered, add it to O.

(4) If an operator # is encountered, then

(a) Repeatedly pop from stack and add to O eachoperator (on the top of stack) which has higherprecedence than #.

10/25/2012 99CS 09 303 Data Structures - Module 2

Page 100: M2_Linear Data Structures

(b) Add # to stack.

5. If a right parenthesis is encountered, push it onto the

stack.

6. If a left parenthesis is encountered then

(a) Repeatedly pop from stack and add to O, until a

right parenthesis is encountered.

(b) Remove the right parenthesis.

[ do not add the right parenthesis to O].

7. Reverse O to get the prefix expression P.

8. Exit

# �Symbolizes any operator in I.10/25/2012 100CS 09 303 Data Structures - Module 2

Page 101: M2_Linear Data Structures

Convert (A*B-F*H)^D

10/25/2012 CS 09 303 Data Structures - Module 2 101

Symbol Scanned Operator Stack Output Expression

D ) D

^ )^ D

) )^) D

H )^) DH

* )^)* DH

F )^)* DHF

- )^)- DHF*

B )^)- DHF*B

* )^)-* DHF*B

A )^)-* DHF*BA

( )^ DHF*BA*-

( (Empty) DHF*BA*-^

Result: ^-*AB*FHD

Page 102: M2_Linear Data Structures

Home Work� Convert to prefix:

� A+ [ B+C - D+E] * F / G

� +A/*+-+BCDEFG

� (a–b)/c*(d + e – f / g)

� */-abc-+de/fg

10/25/2012 CS 09 303 Data Structures - Module 2 102

Page 103: M2_Linear Data Structures

Convert (a–b)/c*(d + e – f / g)

10/25/2012 CS 09 303 Data Structures - Module 2 */-abc-+de/fg103

Page 104: M2_Linear Data Structures

Evaluating Prefix expression(1) Add a left parenthesis “(” at the beginning of P[this acts

as a sentinel].

(2) Scan P from right to left and repeat steps 3 and 4 foreach element of P until the sentinel “(” is encountered.

(3) If an operand is encountered, push it onto STACK.

(4) If an operator # is encountered, then

(a) Remove the two top elements of STACK ,where A isthe top element and B is the next-to-top element.

(b) Evaluate A # B.

(c) Place the result onto STACK.

5) RESULT equal to the top element on stack.

6) Exit

10/25/2012 104CS 09 303 Data Structures - Module 2

Page 105: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 105

Evaluate *+45-72

Page 106: M2_Linear Data Structures

Parenthesis Checker

� To check whether a mathematical expression is properly

parenthesized or not.

� 3 sets of grouping symbols

�The standard parenthesis ()

�The braces { }

� The brackets []

10/25/2012 106CS 09 303 Data Structures - Module 2

Page 107: M2_Linear Data Structures

Algorithm for Parenthesis

Checker

� Make an empty stack.

� Read symbols until end of file is reached.

� If the symbol is an opening symbol, push it onto the stack.

� If it is a closing symbol, do the following:

� If the stack is empty, then report an error.

� Otherwise, pop the stack. If the symbol popped does not

match the closing symbol, then report an error.

� At the end of the file, if the stack is not empty, then report an error.

10/25/2012 107CS 09 303 Data Structures - Module 2

Page 108: M2_Linear Data Structures

Algorithm for parenthesis checkerBegin

read expression

for each character c in expression

if(current symbol or character is a left symbol) then

push the character onto stack.

else if (current symbol is a right symbol ) then

if(stack is empty) then

print (“Error no matching open symbol”)

exit

10/25/2012 108CS 09 303 Data Structures - Module 2

……

Page 109: M2_Linear Data Structures

else

pop a symbol s from the stack

if (s does not correspond to c ) then

print “ error incorrect nesting of symbols”

exit

end {if}

end {if}

end {if}

end {for}

10/25/2012 109CS 09 303 Data Structures - Module 2

……

Page 110: M2_Linear Data Structures

if (stack is not empty ) then

print “ error missing closing symbol”

else

print “ input expression is OK”

end {if}

END

10/25/2012 110CS 09 303 Data Structures - Module 2

Page 111: M2_Linear Data Structures

Self-Study (Homework)� Stack using Linked List

� Write algorithms to:

� Push an element onto stack

� Pop an element

� Create an empty stack

� Check if stack is empty

� Return the top element from the stack

10/25/2012 CS 09 303 Data Structures - Module 2 111

Page 112: M2_Linear Data Structures

QUEUES

• Abstract data type which is special kind of List where items are

inserted at one end (rear) and deleted from the other end

(front).

• Insertion :Rear

• Deletion :Front

• Also called FIFO(First In First Out) list.

10/25/2012 112CS 09 303 Data Structures - Module 2

Page 113: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 113

front rear

Page 114: M2_Linear Data Structures

QUEUE OPERATIONS

� MAKENULL(Q) : makes queue Q an empty list

� FRONT(Q) : returns first element of Q

� ENQUEUE(x, Q) : inserts element x at end of Q

� DEQUEUE(Q) : deletes first element of Q

� EMPTY(Q) : returns true if Q is an empty queue.

10/25/2012 114CS 09 303 Data Structures - Module 2

Page 115: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 115

Page 116: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 116

Page 117: M2_Linear Data Structures

IMPLEMENTATION OF QUEUE

� Using Arrays

(Static implementation)

� Using Pointers

(Dynamic implementation)

10/25/2012 117CS 09 303 Data Structures - Module 2

Page 118: M2_Linear Data Structures

Array Implementation� Type

� QUEUE = record

� elements : array[1..MAXLENGTH] of

� elementtype;

� front, rear: integer;

� end;

10/25/2012 CS 09 303 Data Structures - Module 2 118

Page 119: M2_Linear Data Structures

Queue operations using Arrays(1) Insertion Operation

procedure ENQUEUE( Var data:integer, Var Q: QUEUE)

begin

read(data);

If Q.rear >= SIZE then begin

error(‘Queue Overflow”); exit();

end

else

Q.rear := Q.rear+1;

end;

Q.elements[Q.rear] := data;

end;{ENQUEUE}

10/25/2012 119CS 09 303 Data Structures - Module 2

Page 120: M2_Linear Data Structures

(1)Empty Queue

(2)ENQUEUE(10,Q)

(3) ENQUEUE(3,Q)

10

10 3

frontrear

0 1 2 3

0 1 2 3

Rear := -1

Front := -1

0 1 2 3

front rear

10/25/2012 120CS 09 303 Data Structures - Module 2

Front := 0

Rear := rear + 1 = 0

Q[Rear] := Q[0] = 10

Rear = rear + 1 = 0 + 1 = 1

Q[Rear] := Q[1] = 3

frontrear

Page 121: M2_Linear Data Structures

(4)ENQUEUE(17,Q)

(5)ENQUEUE(29,Q)

10/25/2012 121CS 09 303 Data Structures - Module 2

Rear := rear + 1 = 2+1 = 3

Q[Rear] := Q[3] = 29

10 3 17

0 1 2 3

front rear

10 3 17 29

0 1 2 3

front rear

Rear := rear + 1 = 1 + 1 =2

Q[Rear] := Q[2] = 17

� Queue Full (rear = max - 1)

Page 122: M2_Linear Data Structures

(2) Deletion Operation

Function DEQUEUE(Var Q: QUEUE) : elementtype;

Begin

if Q.rear < Q.front then begin

Q.front:=0; Q.rear:= -1;

error(‘Queue is empty”); exit();

end

else begin

dequeue := Q.elements[Q.front];

Q.front:= Q.front+1;

end;

end;{DEQUEUE}

10/25/2012 122CS 09 303 Data Structures - Module 2

Page 123: M2_Linear Data Structures

(1)DEQUEUE(Q)

(1)DEQUEUE(Q)

(2)DEQUEUE(Q)

10/25/2012 123CS 09 303 Data Structures - Module 2

3 17 29

0 1 2 3

front rear

17 29

0 1 2 3

front rear

data := Q[front] = Q[0] =10

front := front + 1 = 0 + 1 = 1

data := Q[front] = Q[1] = 3

front := front + 1 = 1 + 1 = 2

data := Q[front] = Q[2] = 17

front := front + 1 = 2 + 1 = 3

29

0 1 2 3

front rear

Page 124: M2_Linear Data Structures

(4)DEQUEUE(Q)

10/25/2012 124CS 09 303 Data Structures - Module 2

0 1 2 3

front = 4rear

data := Q[front] = Q[3] = 29

front := front + 1 = 3 + 1 = 4

� Queue empty (rear < front)

Page 125: M2_Linear Data Structures

Queue operations using Pointers

(1) Definition of Queue

type

celltype = record

element: elementtype;

next : celltype;

end;

type

QUEUE = record

front, rear : celltype;

end;

10/25/2012 125CS 09 303 Data Structures - Module 2

Page 126: M2_Linear Data Structures

(1) To Make a NULL QUEUE

procedure MAKENULL(Var Q:QUEUE);

begin

new(Q.front);{create header cell }

Q.front .next := nil;

Q.rear := Q.front; {header is both first &last cell }

end;{MAKENULL}

10/25/2012 126CS 09 303 Data Structures - Module 2

Page 127: M2_Linear Data Structures

(2) Check if QUEUE is empty

function EMPTY(Var Q:QUEUE): boolean;

begin

if Q.front = Q.rear then

return(true);

else

return(false);

end;{EMPTY}

10/25/2012 127CS 09 303 Data Structures - Module 2

Page 128: M2_Linear Data Structures

� (3) Returning first element from a queue

� function FRONT(Var Q:QUEUE):elementtype

� begin

� If EMPTY(Q) then

� error(‘Queue is empty’);

� else

� return(Q . front . next . element)

� end;{FRONT}

10/25/2012 128CS 09 303 Data Structures - Module 2

Page 129: M2_Linear Data Structures

� (4) Insertion into a queue

� Procedure ENQUEUE( var x:elementtype;Var

Q:Queue);

� begin

� new(Q.rear . next);

� {add new cell to rear of queue}

� Q.rear := Q.rear . next;

� Q. rear .element := x;

� Q.rear .next:=nil;

� end; {ENQUEUE}

10/25/2012 129CS 09 303 Data Structures - Module 2

Page 130: M2_Linear Data Structures

� (5) Deletion from a queue

� Function DEQUEUE(Var Q:Queue): elementtype;

� Begin

� if EMPTY(Q) then

� error(‘queue is empty’)

� else begin

� dequeue := Q.front .element;

� Q.front := Q.front .next;

� end;

� end; {DEQUEUE}

10/25/2012 130CS 09 303 Data Structures - Module 2

Page 131: M2_Linear Data Structures

Different Types Of Queue

� Circular Queue

� Double ended Queue

� Priority Queue

10/25/2012 131CS 09 303 Data Structures - Module 2

Page 132: M2_Linear Data Structures

Circular Queue

10/25/2012 CS 09 303 Data Structures - Module 2 132

Page 133: M2_Linear Data Structures

CIRCULAR QUEUE

� Q[1] following Q[n]

� Conditions of circular Queue:

�Insertion Condition

�Deletion Condition

�QUEUE FULL

rear = (rear+1) % SIZE

front = (front + 1) % SIZE

rear = front

10/25/2012 133CS 09 303 Data Structures - Module 2

Page 134: M2_Linear Data Structures

Algorithms of Circular Queue� Insertion

Procedure ENQUEUE(Var data:integer,Var Q : QUEUE);

Begin

if Q.front = ((Q.rear + 1) % SIZE) then

begin

error(‘Queue Full’)

exit();

end

else begin

Q.rear := (Q.rear + 1) % SIZE;

end;

if Q.front = -1 then begin

Q.front := 0; Q.rear := 0;

end;

read(data);

Q.elements[Q.rear] := data;

end;{ENQUEUE}

10/25/2012 134CS 09 303 Data Structures - Module 2

Page 135: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 135

0 1

23

Rear=-1 Front=-1

Queue is empty (front = rear = -1)

Page 136: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 136

0 1

23

Rear = 0

Front = 0

Enqueue (14, Q)

Rear := (rear + 1) % SIZE = (-1 + 1) % 4 = 0 % 4 = 0

Q.elements[0] := 14;

14

Page 137: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 137

0 1

23

Rear = 1

Front = 0

Enqueue (42, Q)

Rear := (rear + 1) % SIZE = (0 + 1) % 4 = 1 % 4 = 1

Q.elements[1] := 42;

1442

Page 138: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 138

0 1

23Rear = 2

Front = 0

Enqueue (20, Q)

Rear := (rear + 1) % SIZE = (1 + 1) % 4 = 2 % 4 = 2

Q.elements[2] := 20;

1442

20

Page 139: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 139

0 1

23Rear = 3

Front = 0

Enqueue (37, Q)

Rear := (rear + 1) % SIZE = (2 + 1) % 4 = 3 % 4 = 3

Q.elements[3] := 37;

1442

2037

Queue full

Page 140: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 140

0 1

23Rear = 3

Front = 0

Queue full

front := (rear + 1) % SIZE = (3 + 1) % 4 = 4 % 4 = 0

1442

2037

Page 141: M2_Linear Data Structures

� Deletion

Function DEQUEUE(Var Q : QUEUE): elementtype;

begin

if Q.front = -1 then begin

error(‘Queue is empty’)

exit();

end

else dequeue := Q.elements[Q.front];

Q.front := (Q.front + 1) % SIZE;

end;{DEQUEUE}

10/25/2012 141CS 09 303 Data Structures - Module 2

Page 142: M2_Linear Data Structures

Is this situation possible in a

circular queue?

10/25/2012 CS 09 303 Data Structures - Module 2 142

Page 143: M2_Linear Data Structures

Double-ended Queue (DEQUEUE)� Homogeneous list

� Insertion &deletion can be done at both ends.

� 2 types of dequeue:

(1) Input restricted queue (insertion: rear end

deletion: both ends)

An input-restricted dequeue is one where deletion can be madefrom both ends, but insertion can only be made at one end.

(2)Output restricted queue (insertion: both ends

deletion: front end)

An output-restricted dequeue is one where insertion can bemade at both ends, but deletion can be made from one endonly.

10/25/2012 143CS 09 303 Data Structures - Module 2

Page 144: M2_Linear Data Structures

Operations on Dequeue

(1) Add an element at the rear end ( insert right)

(2)Delete an element from the front end ( delete left )

(3) Add an element at the front end ( insert left )

(4) Delete an element from the rear end (delete right )

10/25/2012 144CS 09 303 Data Structures - Module 2

Page 145: M2_Linear Data Structures

Insert an element at the right side of

dequeue

procedure insertRightDequeue(Var data: integer, Var Q: QUEUE);

Begin

read(data);

if ((Q.left = 0 && Q.right = MAX - 1) || (Q.left = Q.right + 1)) then

begin

error(‘Queue overflow ‘);

exit();

end;

10/25/2012 145CS 09 303 Data Structures - Module 2

……

Page 146: M2_Linear Data Structures

if Q.left = -1 then begin

Q.left := 0; Q.right := 0;

end

else begin

if Q.right = MAX-1 then

Q.right := 0;

else

Q.right := Q.right+1;

end;

Q.elements[Q.right] := data;

end; {insertRightDequeue}

10/25/2012 CS 09 303 Data Structures - Module 2 146

Page 147: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 147

0 1 2 3

� Empty Queue (If left = right = -1)

l r

Insert an element at the right side of

dequeue

Page 148: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 148

0 1 2 3

5

� insertRightDequeue ( 5, Q)

left = 0 right = 0

� Set left = 0, right = 0;

� Insert at 0.

� Q.elements[right] : = Q.elements[0] = 5;

Page 149: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 149

0 1 2 3

5 2

� insertRightDequeue ( 2, Q)

� Increment right.

� Right := right + 1 = 0 + 1 = 1;

� Insert at right.

� Q.elements[right] : = Q.elements[1] = 2;

left = 0 right = 1

Page 150: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 150

0 1 2 3

5 2 10

� insertRightDequeue ( 10, Q)

� Increment right.

� Right := right + 1 = 1 + 1 = 2;

� Insert at right.

� Q.elements[right] : = Q.elements[2] = 10;

left = 0 right = 2

Page 151: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 151

0 1 2 3

5 2 10 17

� insertRightDequeue ( 17, Q)

� Increment right.

� Right := right + 1 = 2 + 1 = 3;

� Insert at right.

� Q.elements[right] : = Q.elements[3] = 17;

left = 0 right = 3

� Queue full (left = 0 and right = MAX – 1 = 4 – 1 = 3)

Page 152: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 152

0 1 2 3

2 10 17

� Perform a delete operation.

� So, left will be incremented by one.

� left := left + 1 = 0 + 1 = 1;

left = 1 right = 3

Page 153: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 153

0 1 2 3

15 2 10 17

� insertRightDequeue (15, Q)

� 15 can be inserted at 0.

� If left <> -1 and right = MAX – 1, there are vacant positions at the

beginning of the queue.

� Set right = 0 and insert at 0.

left = 1right = 0

� Queue full (left = right + 1= 0 + 1 = 1)

Page 154: M2_Linear Data Structures

Insert an element at the left side of

dequeue

procedure insertLeftDequeue(Var data:integer, Var Q: QUEUE);

Begin

read(data);

if((Q.left = 0 && Q.right = MAX - 1) | | (Q.left = Q.right+1))

then begin

error(‘Queue overflow ‘);

exit();

end

10/25/2012 154CS 09 303 Data Structures - Module 2

Page 155: M2_Linear Data Structures

if Q.left = -1 then begin

Q.left := 0; Q.right := 0;

end

else begin

if Q.left = 0 then

Q.left := MAX-1;

else

Q.left := Q.left-1;

end;

Q.elements[Q.left] := data;

end; {insertLeftDequeue}

10/25/2012 CS 09 303 Data Structures - Module 2 155

Page 156: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 156

0 1 2 3

� Empty Queue (If left = right = -1)

l r

Insert an element at the left side of

dequeue

Page 157: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 157

0 1 2 3

5

� insertLeftDequeue ( 5, Q)

left = 0 right = 0

� Set left = 0, right = 0;

� Insert at 0.

� Q.elements[left] : = Q.elements[0] = 5;

Page 158: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 158

0 1 2 3

5 2

� insertLeftDequeue ( 2, Q)

� If left = 0, set left = MAX - 1.

� left := MAX - 1 = 4 - 1 = 3;

� Insert at left.

� Q.elements[left] : = Q.elements[3] = 2;

left = MAX- 1right = 0

Page 159: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 159

0 1 2 3

5 10 2

� insertLeftDequeue ( 10, Q)

� Decrement left.

� left := left - 1 = 3 - 1 = 2;

� Insert at left.

� Q.elements[left] : = Q.elements[2] = 10;

right = 0 left = 2

Page 160: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 160

0 1 2 3

5 17 10 2

� insertLeftDequeue ( 17, Q)

� Decrement left.

� left := left - 1 = 2 - 1 = 1;

� Insert at left.

� Q.elements[left] : = Q.elements[1] = 17;

right = 0 left = 1

� Queue full (left = right + 1 = 0 + 1 = 1)

Page 161: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 161

0 1 2 3

17 10 2

� Perform a delete operation.

� Right will be set to MAX – 1 so that element at zero is deleted first.

� right := MAX - 1 = 4 - 1 = 3;

left = 1 right = 3

Page 162: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 162

0 1 2 3

15 2 10 17

� insertLeftDequeue (15, Q)

� 15 can be inserted at 0.

� If left <> -1 and left <> 0, there are vacant positions at the beginning

of the queue.

� Set left := left - 1 = 2 -1 = 0 and insert at 0.

left = 0

� Queue full (left = 0 and right = MAX - 1)

right = 3

Page 163: M2_Linear Data Structures

Dequeue Operations(Normal

Queue Operations)� Insert Right (Rear) and Delete Left (Front)

� Insert Left (Front) and Delete Right (Rear)

10/25/2012 CS 09 303 Data Structures - Module 2 163

Page 164: M2_Linear Data Structures

Delete an element from the right side

of dequeue

procedure deleteRightDequeue(Var Q: QUEUE);

Begin

Var data: integer;

if((Q.left = -1) then begin

error(‘Queue underflow ‘);

exit();

end;{if}

data := Q.elements[Q.right];

10/25/2012 164CS 09 303 Data Structures - Module 2

Page 165: M2_Linear Data Structures

if (Q.left = Q.right ) then begin {only one element}

Q.left:= -1; Q.right := -1;

end;{if}

if (Q.right = 0) then

Q.right = MAX -1;

else

Q.right = Q.right -1;

end; { deleteRightDequeue}

10/25/2012 CS 09 303 Data Structures - Module 2 165

Page 166: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 166

0 1 2 3

5 2 10 17

� deleteRightDequeue ( Q)

� Right = 0.

� So, delete the first element.

� data := Q.elements[right] = Q.elements[0] = 5;

� Set Right := MAX - 1 = 3; {Next deletion occurs at right = 3}.

left = 1right = 0

Delete an element from the right side

of dequeue

Page 167: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 167

0 1 2 3

2 10 17

� deleteRightDequeue ( Q)

� Right = 3.

� data := Q.elements[right] = Q.elements[3] = 17;

� Set Right := right - 1 = 3 – 1 = 2;

left = 1 right = 3

Page 168: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 168

0 1 2 3

2 10

� deleteRightDequeue ( Q)

� Right = 2.

� data := Q.elements[right] = Q.elements[2] = 10;

� Set Right := right - 1 = 2 – 1 = 1;

left = 1 right = 2

Page 169: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 169

0 1 2 3

2

� deleteRightDequeue (Q)

left = 1 right = 1

� data : = Q.elements[right] : = Q.elements[1] = 5;

� Left = right = 1; {only one element}

� Set left := -1, right := -1; {Queue becomes empty}

Page 170: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 170

0 1 2 3

� Empty Queue (left = right = -1)

� Cannot perform deletion

l r

� deleteRightDequeue (Q)

Page 171: M2_Linear Data Structures

Delete an element from the left side

of dequeue

procedure deleteLeftDequeue(Var data:integer,Var Q:

QUEUE);

Begin

if((Q.left = -1) then begin

error(‘Queue underflow ‘);

exit();

end;{if}

data:=Q.elements[Q.left];

10/25/2012 171CS 09 303 Data Structures - Module 2

Page 172: M2_Linear Data Structures

if Q.left = Q.right then begin {only one element}

Q.left:= -1; Q.right := -1;

end;{if}

if ( Q.left = MAX - 1 ) then

Q.left = 0;

else

Q.left = Q.left + 1;

end; { deleteLeftDequeue}

10/25/2012 CS 09 303 Data Structures - Module 2 172

Page 173: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 173

0 1 2 3

12 27 60 18

� deleteLeftDequeue ( Q)

� Left = 0.

� So, delete the first element.

� data := Q.elements[left] = Q.elements[0] = 12;

� Set left := left + 1 = 1; {Next deletion occurs at left = 1}.

left = 0 right = 3

Delete an element from the left side of

dequeue

Page 174: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 174

0 1 2 3

27 60 18

� deleteLeftDequeue ( Q)

� Left = 1.

� data := Q.elements[left] = Q.elements[1] = 27;

� Set left := left + 1 = 1 + 1 = 2;

left = 1 right = 3

Page 175: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 175

0 1 2 3

60 18

� deleteLeftDequeue ( Q)

� Left = 2.

� data := Q.elements[left] = Q.elements[2] = 60;

� Set left := left + 1 = 2 + 1 = 3;

left = 2 right = 3

Page 176: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 176

0 1 2 3

18

� deleteLeftDequeue (Q)

left = 3 right = 3

� data : = Q.elements[left] : = Q.elements[3] = 18;

� Left = right = 3; {only one element}

� Set left := -1, right := -1; {Queue becomes empty}

Page 177: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 177

0 1 2 3

� Empty Queue (left = right = -1)

� Cannot perform deletion

l r

� deleteLeftDequeue (Q)

Page 178: M2_Linear Data Structures

Example� Insert right (rear)

10/25/2012 CS 09 303 Data Structures - Module 2 178

0 1 2 3

21

left = 0 right = 0

Page 179: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 179

� Insert left (front)

0 1 2 3

21 42

left = 3right = 0

Page 180: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 180

� Insert right (rear)

0 1 2 3

21 35 42

left = 3right = 1

Page 181: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 181

� Insert left (front)

0 1 2 3

21 35 5 42

left = 2right = 1

Page 182: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 182

� Delete left (front)

0 1 2 3

21 35 42

left = 2right = 1

Page 183: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 183

� Delete right (rear)

0 1 2 3

21 42

left = 2right = 0

Page 184: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 184

� Delete right (rear)

0 1 2 3

42

left = 2 right = 3

Page 185: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 185

� Delete left (rear)

0 1 2 3

l ,r = -1

Page 186: M2_Linear Data Structures

PRIORITY QUEUE

� Collection of elements such that each element is assigned a

priority number.

�Rules are:

� An element of higher priority is processed before any

element of lower priority.

� Elements of same priority are processed according to the

order in which they were added into the queue.

10/25/2012 186CS 09 303 Data Structures - Module 2

Page 187: M2_Linear Data Structures

Two types of priority queues� Ascending priority queue

� Items are inserted arbitrarily and element with least priority

is removed.

� Descending priority queue

� Items are inserted arbitrarily and element with highest

priority is removed.

10/25/2012 CS 09 303 Data Structures - Module 2 187

Page 188: M2_Linear Data Structures

Implementation�Implementation of priority queue:

(1) One way list representation

(2) Multiple Queues (one for each priority - Array

representation)

(3) Maximum or minimum heap

10/25/2012 CS 09 303 Data Structures - Module 2 188

Page 189: M2_Linear Data Structures

One Way List Representation

� Each node contains:

� INFO: Information field

� PRT : Priority number

� NEXT: Next pointer

A node x precedes a node y in the list when x has higher

priority than y or when both have same priority but x was

inserted before y.

10/25/2012 189CS 09 303 Data Structures - Module 2

Page 190: M2_Linear Data Structures

� Operations performed:

� Searching an element with maximum priority

� Inserting an element

� Deleting an element with maximum priority

10/25/2012 CS 09 303 Data Structures - Module 2 190

Page 191: M2_Linear Data Structures

� Insert A with priority number 2.

10/25/2012 CS 09 303 Data Structures - Module 2 191

Start

A 2 NULL

Page 192: M2_Linear Data Structures

� Insert B with priority number 1.

10/25/2012 CS 09 303 Data Structures - Module 2 192

Start

B 1 A 2 NULL

Page 193: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 193

Start

B 1 A 2 C 2 NULL

� Insert C with priority number 2.

Page 194: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 194

Start

B 1 A 2 C 2 NULL

� Delete operation deletes element with highest priority.

Page 195: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 195

Start

A 2 C 2 NULL

� Delete operation deletes element with highest priority.

Page 196: M2_Linear Data Structures

Insertion algorithm of one way list

representation of priority queue

(1) Traverse the one – way list until you find a node X

whose priority number exceeds N (item to be inserted).

(2) Insert ITEM N in front of node X.

(3) If no such node is found, insert ITEM as the last element

of the list.

10/25/2012 196CS 09 303 Data Structures - Module 2

Page 197: M2_Linear Data Structures

Deletion algorithm of one way list

representation of priority queue

(1) Set ITEM := INFO[START] { This saves the data in the

first node }

(2) Delete first node from the list

(3) Process ITEM

(4) Exit

10/25/2012 197CS 09 303 Data Structures - Module 2

Page 198: M2_Linear Data Structures

Array Implementation� As multiple queues.

� Multiple queues are defined in the same array queue[] of

size n.

� Each queue i is allocated a predefined space bounded by

array indices.

� A queue is maintained for each priority.

� In order to process an element of the priority queue,

element from non-empty highest priority number queue is

accessed.

10/25/2012 CS 09 303 Data Structures - Module 2 198

Page 199: M2_Linear Data Structures

10/25/2012 CS 09 303 Data Structures - Module 2 199

Queue1 Queue2 Queue3 Queue4

front[0] front[1] front[2] front[3]rear[0] rear[1] rear[2] rear[3]

Page 200: M2_Linear Data Structures

Insertion algorithm of array

representation of priority queue

(1) Insert ITEM as the rear element in row M of QUEUE.

(2)Exit

10/25/2012 200CS 09 303 Data Structures - Module 2

Page 201: M2_Linear Data Structures

Deletion algorithm of array

representation of priority queue

(1)Find the smallest k such that FRONT[K]!=NULL.

{Find the first non-empty queue}

(2)Delete and process the front in row k of queue.

(3) Exit

10/25/2012 201CS 09 303 Data Structures - Module 2

Page 202: M2_Linear Data Structures

Comparison of array & one way

list representation

� Array :Time efficient

One way list :Space efficient

10/25/2012 202CS 09 303 Data Structures - Module 2

Page 203: M2_Linear Data Structures

Applications of Queue� Round robin techniques :Process scheduling

� Print server routines

� All types of customer service software(Railway/airticket reservation)

10/25/2012 203CS 09 303 Data Structures - Module 2

Page 204: M2_Linear Data Structures

Application of linked lists

Polynomial Manipulation

� Polynomial of single variable of degree m

� f(x) = amxm + am-1xm-1 + … + aix

i + … + a1x + a0

� ai are non-zero coefficients with exponents i.

� Each term is represented by a node with three fields,

which represent the coefficient, exponent of a term and a

pointer to the next term.

10/25/2012 CS 09 303 Data Structures - Module 2 204

Page 205: M2_Linear Data Structures

� A term of a polynomial of single variable x:

� p = 6x5 + 2x3 + x + 4

� Store the polynomial in descending order of powers.

10/25/2012 CS 09 303 Data Structures - Module 2 205

powerx coef next

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

Page 206: M2_Linear Data Structures

� A term of a polynomial of variables x, y, z:

� p = 2x3 + 3xy + y2 + yz

� Store the polynomial such that terms are arranged in

descending order of powers of x first, then y and then z.

10/25/2012 CS 09 303 Data Structures - Module 2 206

Power x Power y Power z coef next

3 0 0 2 1 1 0 3 0 2 0 1 0 1 1 1

Page 207: M2_Linear Data Structures

Addition of Two Polynomials of

Single variable� p = 6x5 + 2x3 + x + 4

� q = 2x4 + 10 x + 1

10/25/2012 CS 09 303 Data Structures - Module 2 207

6x5 + 2x3 + x + 4

2x4 + 10 x + 1

6x5 + 2x4 + 2x3 + 11 x + 5

Page 208: M2_Linear Data Structures

� p.powerx > q.powerx (Add first term of p. Increment ptrp)

10/25/2012 CS 09 303 Data Structures - Module 2 208

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

4 2 1660

1500

1 10 1700

1660

0 1 NULL

1700

5 6

1826

ptrq

ptrp

ptrr

Page 209: M2_Linear Data Structures

� p.powerx < q.powerx

10/25/2012 CS 09 303 Data Structures - Module 2 209

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

4 2 1660

1500

1 10 1700

1660

0 1 NULL

1700

5 6 1870

1826

4 2 Null

1870

ptrq

ptrr

ptrp

Page 210: M2_Linear Data Structures

� p.powerx > q.powerx

10/25/2012 CS 09 303 Data Structures - Module 2 210

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

4 2 1660

1500

1 10 1700

1660

0 1 NULL

1700

5 6 1870

1826

4 2 1860

1870

ptrq

ptrp

ptrr

3 2 Null

1860

Page 211: M2_Linear Data Structures

� p.powerx = q.powerx (Add the coefficients and increment both the pointers)

10/25/2012 CS 09 303 Data Structures - Module 2 211

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

4 2 1660

1500

1 10 1700

1660

0 1 NULL

1700

5 6 1870

1826

4 2 1860

1870

ptrp

3 2 1807

1860

1 11 Null

1807ptrr

ptrq

Page 212: M2_Linear Data Structures

� p.powerx = q.powerx (Add the coefficients)

10/25/2012 CS 09 303 Data Structures - Module 2 212

5 6 1570

1526

3 2 1607

1570

1 1 1750

1607

0 4 NULL

1750

4 2 1660

1500

1 10 1700

1660

0 1 NULL

1700

5 6 1870

1826

4 2 1860

1870

ptrp

3 2 1807

1860

1 11 1950

1807

ptrq

0 5 NULL

1950ptrr

Page 213: M2_Linear Data Structures

Algorithm for polynomial addition

(single variable)1. Repeat upto step 3 while there are terms in both polynomials yet

to be processed.

2. Obtain values for terms from each polynomial.

3. If the powers associated with the two terms are equal then

� If the sum of coefficients of both terms is not zero then

� Insert sum of the coefficients into the sum polynomial.

� Obtain the next terms from both polynomials.

10/25/2012 CS 09 303 Data Structures - Module 2 213

Page 214: M2_Linear Data Structures

� Else if the power of first polynomial > power of the

second polynomial then

� Insert the term from first polynomial into sum polynomial.

� Obtain the next term in the first polynomial.

� Else

� Insert the term from second polynomial into sum

polynomial.

� Obtain the next term in the second polynomial.

4. Copy remaining terms from non-empty polynomial into sum

polynomial.

5. Return the address of the sum polynomial.

10/25/2012 CS 09 303 Data Structures - Module 2 214

Page 215: M2_Linear Data Structures

Thank You

10/25/2012 CS 09 303 Data Structures - Module 2 215