{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2c03-review - 00045 - p:= L n:=1 while(p ↑.next<> nil...

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

View Full Document Right Arrow Icon
1 SE2C03. Sample solutions to the assignment 2. Total of this assignment is 151 pts. Each assignment is worth 5%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and resubmit to me during class, office hours, or just slip under the door to my office. 1.[ 22] Write procedures to implement the following operations on singly linked lists. What is the time complexity of each operation? Assume a pointer implementation. a.[4] NO3ONLY(L). The procedure deletes from L all elements on 3k, k>0, positions. For instance if L=2,3,1,5,7,8,7 then NO3ONLY(L) = 2,3,5,7,7, i.e. 1 and 8 are removed. procedure NO3ONLY(var L: celltype;); var p : celltype; var n : integer; begin
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: p := L; n:=1; while (p ↑ .next <> nil) do begin if ((n mod 3) = 0) 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.[6] C1H2H(L 1 ,L 2 ). This procedure first deletes odd elements from the first list and even elements from the second list and then concatenates the outcomes. For instance if L 1 = 2,3,1,4,5,6, L 2 = a,c,b,d,f, then C1H2H(L 1 ,L 2 ) = 3,4,6,a,b,f. function C1H2H(L1, L2: ↑ celltype;): ↑ celltype; var p : ↑ celltype; var n : integer; begin {delete odd elements in List 1} p := L1; while (p ↑ .next <> nil) do begin p ↑ .next := p ↑ .next ↑ .next; if (p ↑ .next <> nil) p := p ↑ .next ; end {delete even elements in List 2} p := L2;...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online