2c03-review - 00094

2c03-review - 00094 - 1 ↑ .next ↑ .next ; tmp2 := p 2...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
7 while (p .next <> nil) do begin if ((n mod 3 = 0) or (n mod 3 = 2)) the begin p .next := p .next .next; n:= n+1;end p := p .next ; n:= n+1; end end The time complexity is O(n). b.[5] SHUFFLE(L 1 ,L 2 ). This procedure shuffles the list, the result has the appropriate elements of the first list follows by the appropriate elements of the second list, see the example. For instance if L 1 = 2,3,1,4,5,6,7,8, L 2 = a,c,b,d,f, then SHUFFLE(L 1 ,L 2 ) = 2,a,3,c,1,b,4,d,5,f,6,7,8, L 1 = 1,2,3, L 2 = a,b,c,d,e,f, then SHUFFLE(L 1 ,L 2 ) = 1,a,2,b,3,c,d,e,f. procedure SHUFFLE(var L 1 ,L 2 : celltype;); var p 1 ,p 2 ,tmp1,tmp2: celltype; begin p 1 := L 1 ; p 2 := L 2 ; while (p 1 .next <> nil and p 2 .next <> nil) do begin tmp1 := p
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 ↑ .next ↑ .next ; tmp2 := p 2 ↑ .next ↑ .next ; p 1 ↑ .next ↑ .next := p 2 ↑ .next; p 2 ↑ .next ↑ .next := tmp1; p 1 := p 1 ↑ .next ↑ .next ; p 2 ↑ .next := tmp2 ; end if (p 1 ↑ .next = nil) then p 1 ↑ .next:= p 2 ↑ .next ; return(L 1 ) end The time complexity is O(min(m,n)). c.[5] REV1(L). The procedure reverse all the list except the first element. For instance if L= 2,3,1,4,5,6, then REV1(L)= 2,6,5,4,1,3. procedure REV1(var L: ↑ celltype;); var p,tmp: ↑ celltype; begin if ((L ↑ .next = nil) or (L ↑ .next ↑ .next = nil)) return; p := L ↑ .next ↑ .next;...
View Full Document

This note was uploaded on 12/10/2009 for the course CAS 2c03 taught by Professor Janicki during the Spring '03 term at McMaster University.

Ask a homework question - tutors are online