0% found this document useful (0 votes)
42 views

Assignment 1

1. The document provides instructions for a data structures and algorithms assignment involving 13 questions. Students are instructed to show their work and provide answers in the spaces provided. 2. The questions cover a range of data structures topics like arrays, linked lists, stacks, queues, and their related operations like insertion, deletion, traversal etc. Students are asked to trace code, determine time complexities, simulate operations and more. 3. Answers are to be provided step-by-step to demonstrate understanding of the concepts. The assignment aims to assess students' knowledge of core data structures.

Uploaded by

شماخ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Assignment 1

1. The document provides instructions for a data structures and algorithms assignment involving 13 questions. Students are instructed to show their work and provide answers in the spaces provided. 2. The questions cover a range of data structures topics like arrays, linked lists, stacks, queues, and their related operations like insertion, deletion, traversal etc. Students are asked to trace code, determine time complexities, simulate operations and more. 3. Answers are to be provided step-by-step to demonstrate understanding of the concepts. The assignment aims to assess students' knowledge of core data structures.

Uploaded by

شماخ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

KING KHALID UNIVERSITY Subject Code: 231CCS-6

College of Computer Science Data Structures & Algorithms


Dept. of Computer Science Assignment#1

Assignment#1
Important Instructions for Students
1. Write your name and ID.
2. Answer all the questions in the provided space.
3. All the questions have to solved step by step.
4. Total marks are 3.
Student ID: Student Name:

1) Show the content of the array nums after tracing the following java code.
int nums[]={5,3,7,9,1,6,4};
for(int i = 0, start = 0; i < nums.length; i++){
if(i == 0)
start = nums[i];
if(i == (nums.length -1)){
nums[i] = start;
break;
}
nums[i] = nums[i + 1];
}
Answer
2) What are the Time efficiency and Big-O for the following method?
void myFun(int [][]t, int n) {
int val=4;
int i,j;
for(i=0;i<=n;i*=2)
for(j=0;j<=n;j++)
t[i][j] = t[i][j]+ val*i;
for(i=0;i<=n;i+=val)
System.out.println(t[i][j]);
}
Answer

3) Consider a full stack S1 of size 4 and empty stacks S2 and S3 of size 4.


What are the contents of the stacks and their tops after each iteration?
while(!S1.empty())
if(!S2.full())
7
S2.push(S1.pop()); 4
while(!S2.empty()) 6
if(!S3.full()) 3
S3.push(S2.pop()); S1 top=3
Answer
4) What is the time efficiency and Big O for the following java code?
public void print(int n, int m){
for( int i=1; i<=m; i++ )
for( int j=1; j<=n; j++ )
System.out.println(n+10);
for( int i=1; i<=n; i+=2 )
System.out.println(n);
}
Answer

5) Show the content of the array M after execution the following code
int M[ ]={5,4,7,8,1,2};
int c, i, n=6;
c=M[0]
for(i=0;i<n-1;i++)
M[i]=M[i+1]
M[n-1]=c;
Answer
6) Suppose that you have these elements {2, 9, 1, 3, 6} show how you can perform the
following operations:
a. Create an ordered linked list for these elements.
b. Insert the element 5 to this linked list.
c. Delete the element 6 from this linked list.
Answer
7) Suppose that you have these elements {10, -40, 20, -30, 60} show how you can perform the
following operations:
a. Create an ordered linked list for these elements.
b. Insert the element 30 to this linked list.
c. Delete the element 40 from this linked list.
Answer
8) Convert these expressions to postfix using stack operations:
a) Z = a + ( x + 5 ) / 3
b) K = 9 * 7 * 2 – 3 + 6
c) M = a - ( c + 2 ) / ( b – 1) + 3
Answer
9) Consider an empty stacks S1 and S2 with size = 4, what is the content of the stacks and its
top after every push and pop operations.
int a[7]={4,3,8,6,2,1,10};
for(i=0;i<7;i++)
if(a[i]%2==0)
S1.push(a[i]);
else
S2.push(a[i])
for(i=0;i<3;i++){
S1.pop();
S2.pop();
}
Answer

10) Consider the following linked list

head 10 5 20 30 null

Trace the following pseudo-code and show the change in the pointers

p=head
while(p.link!=null)
{
r=p;
p=p.link;
}
r.link=null;
Answer

11) Suppose that you went to Panda (super market) in order to buy some things, and after you go to
the cashier to pay the account, you were surprised by the presence of a waiting line for your
number was the fourth. What is the mechanism that will be used for this scenario? And
simulate it.
Answer
12) Consider a full queue Q1 of size 4 and empty queues Q2 and Q3 of size 4. What are the
contents of the queues and their front and rear after each iteration?

while(!Q1.empty()) 3 7
if(!Q2.full()) 2 4
Q2.enqueue(Q1.dequeue());
while(!Q2.empty())
1 6
if(!Q3.full()) 0 3
Q3.enqueue(Q2.dequeue()); Q1 front=0 , rear=3

Answer
13) If you have a word that contains some characters, show how you can use the stack to
reverse this word. [ Hint: Use the word “madam” for your operation]
Answer

You might also like