2c03-review - 00046

2c03-review - 00046 - ↑ celltype;): ↑ celltype; var p :...

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

View Full Document Right Arrow Icon
2 if (p .next <> nil) p := p .next ; while (p .next <> nil) do begin p .next := p .next .next; if (p .next <> nil) p := p .next ; end p := L1; while (p .next <> nil) do p := p .next ; p .next := L2 .next; return(L1); end The time complexity is O(m+n). c.[6] MIDDLE(L). The procedure returns the middle element if the list contains odd number of elements, and it returns a list containing two middle elements if the list contains even number of elements. For instance if L= 2,3,1,4,5,6, then MIDDLE(L)=1,4, if L= 2,3,1,4,5,6,10 then MIDDLE(L)=4. function MIDDLE(L:
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ↑ celltype;): ↑ celltype; var p : ↑ celltype; var n,i : integer; begin p := L; n:=0; while (p ↑ .next <> nil) do begin p := p ↑ .next ; n:= n+1; end if ((n mod 2) =1) then begin p := L; for (i:=1 to (n div 2) do p ↑ .next := p ↑ .next ↑ .next; p := p ↑ .next ; for (i:=1 to (n div 2) do p ↑ .next := p ↑ .next ↑ .next; end else begin p := L; for (i:=1 to (n div 2 )-1) do p ↑ .next := p ↑ .next ↑ .next; p := p ↑ .next ↑ .next ; for (i:=1 to (n div 2)-1) do p ↑ .next := p ↑ .next ↑ .next; end return(L); end...
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