0% found this document useful (0 votes)
146 views3 pages

CS2334 Assignment 1 Details

Uploaded by

sze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views3 pages

CS2334 Assignment 1 Details

Uploaded by

sze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS2334 Assignment 1

Submission: Softcopy to canvas.


Due: October 27th, 2024, 8pm.
Each 24 hours late submission halves the score. 0 mark after 3 days.

Problem 1. Program Complexity. (30%)

1) Give the big O characterization of the following operations, assuming m and n are variables (18%).

a. int k=10;
for(int i=1001*n; i>=n; i--)
k=k+5*n;

b. int func(int n, int m){


if (n<100) return n;
else if (n<10000)
return func(n-2,m);
else
return func(n/2,m)
}

c. int t = 10;
for (int i=1; i<=m; i++){
for(int j=1; j*j<=n; j+=5){
t += (i+j);
}
}

d. int x=0;
for(int j=1, k=1; j*k<=n; ++j, ++k)
x++;

e. int t=10;
int x=10;
for (int j=1; j<=m/3; j=j*3){
t--;
x = x*x;
}

f. int c=0;
for(int i=n*n; i>0; i--)
for(int j=0; j<i; j++)
for(int k=i; k>j; k--)
c+=i*j*k;

2) Suppose your program takes 1ms to solve a problem of size 100. What is the maximum problem size
that can be solved in 100s if the running time is as follows (12%):
a. T(n)=log10n
b. T(n)=5√�
c. T(n)=2n√�
Problem 2. Linked Lists. (35%)

1) In a singly linked list L with more than three nodes, a pointer p points to a node that is guaranteed to
be neither the first nor the last node. What are the purposes of the following code blocks (15%)?
a.
q=L->next->next;
q->data=q->next->data;
q->next=q->next->next;

b.
q=L;
t=p->data;
p->data=q->data;
q->data=t;

c.
q=L;
while (q->next!=p) q=q->next
q->data=q->next->data;
q->next=q->next->next;

2) If instead L is a doubly linked list, refer to the example code provided above, fill in the code blocks to
fulfill the wanted purposes.

a. Insert a node s after the 2nd node (5%)

b. Insert a node s before the last node (5%)

c. Swap the positions of the first and the last node. Do not modify the values (data) of the nodes (10%)
Problem 3. Stacks and Queues. (35%)

1) Given an empty stack and the posh-in sequence 1, 2, 3, …, n and the pop-out sequence P1, P2, P3, …,
Pn. If P1=n, what is the value of Pi (1≤ i ≤ n)? Explain your answer (5%).

2) Describe in your own words how to use two queues to optimally implement a stack. What is the
optimal complexity (Big O) of push and pop functions for this stack (15%)?

3) Describe in your own words how to use two stacks to optimally implement a queue. What is the
optimal complexity (Bio O) of dequeue and enqueue functions for this queue (15%)?

You might also like