Array and Linked List Operations, Exercises of Data Structures and Algorithms

Various exercises and questions related to arrays and linked lists. Topics covered include determining dimensions and sizes, accessing array elements, rectifying incorrect scores, deleting elements from arrays and linked lists, and analyzing the complexity of algorithms. The document also includes exercises on inserting data in linked lists and calculating the worst and average-case times for stack methods.

Typology: Exercises

2019/2020

Uploaded on 11/19/2022

henry-mafua
henry-mafua 🇳🇬

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Considering the following array of C.A scores for Six students in three assessments. Each row
represents the scores of a single student. Answer the questions that follow.
pop_quiz = {
{4.0, 3.75, 5.00},
{2.67, 3.83, 4.97},
{4.22, 3.89, 2.77},
{4.75, 4.11, 4.63},
{2.00, 5.00, 5.00}
{3.61, 4.21, 3.66}
}
(a) How many dimensions does the array have? (b) What is the length (size) of each dimension? (c)
What is the score stored at pop quiz [3][2]?
(d) The second test was recorded incorrectly for the fifth student. Their right score is 3.31. How
can you rectify that score?
(e) Are the elements of the array homogenous or heterogeneous?
(f) What data type could you use to represent the elements of the array?
Determine the time efficiency of the following algorithm and what complexity class does it
belong to?
BEGIN
Input the number of days from the user.
II. Declare and create an int array with the number of days as its length.
III. For each index in the array:
a. Input the temperature from the user.
b. Store the temperature in the array at that index.
IV. Initialize a sum to zero.
V. For each index in the array:
a. Add the element at that index to the sum.
VI.Calculate and print the average.
VII. Initialise a counter to zero.
VIII. For each index in the array
If the element at that index is greater than the average:
Increment the counter.
Print the counter.
END
3. Given the following array: {“red”, “orange”, “yellow”, “green”, “blue”},
(a) Show and briefly explain the process of deleting the element “red”.
(b) What is the complexity of this process? No calculations are needed.
4. Is the formula for finding the location of an element in a dynamic array different from the formula for finding the
location of an element in a static array? Justify your answer.
5. When a dynamic array expands, why can’t the existing elements be left in place and extra memory simply
be allocated at the end of the existing memory allocation?
6. Suppose a dynamic integer array 𝑎 with indices beginning at 0 has 100 elements and the line of code
𝑎[100] = 𝑎[5] is executed. How many array values m
pf3
pf4
pf5

Partial preview of the text

Download Array and Linked List Operations and more Exercises Data Structures and Algorithms in PDF only on Docsity!

  1. Considering the following array of C.A scores for Six students in three assessments. Each row represents the scores of a single student. Answer the questions that follow. pop_quiz = { {4.0, 3 .75, 5. 0 0}, {2.67, 3 .83, 4. 9 7}, {4.22, 3 .89, 2. 7 7}, {4.75, 4 .11, 4. 6 3}, {2.00, 5 .00, 5. 0 0} {3.61, 4.21, 3.66} } (a) How many dimensions does the array have? (b) What is the length (size) of each dimension? (c) What is the score stored at pop quiz [3][2]? (d) The second test was recorded incorrectly for the fifth student. Their right score is 3. 3 1. How can you rectify that score? (e) Are the elements of the array homogenous or heterogeneous? (f) What data type could you use to represent the elements of the array? Determine the time efficiency of the following algorithm and what complexity class does it belong to? BEGIN Input the number of days from the user. II. Declare and create an int array with the number of days as its length. III. For each index in the array: a. Input the temperature from the user. b. Store the temperature in the array at that index. IV. Initialize a sum to zero. V. For each index in the array: a. Add the element at that index to the sum. VI.Calculate and print the average. VII. Initialise a counter to zero. VIII. For each index in the array If the element at that index is greater than the average: Increment the counter. Print the counter. END
  2. Given the following array: {“red”, “orange”, “yellow”, “green”, “blue”}, (a) Show and briefly explain the process of deleting the element “red”. (b) What is the complexity of this process? No calculations are needed.
  3. Is the formula for finding the location of an element in a dynamic array different from the formula for finding the location of an element in a static array? Justify your answer.
  4. When a dynamic array expands, why can’t the existing elements be left in place and extra memory simply be allocated at the end of the existing memory allocation?
  5. Suppose a dynamic integer array 𝑎 with indices beginning at 0 has 100 elements and the line of code 𝑎[100] = 𝑎[5] is executed. How many array values m

LINKED LIST

1. HEAD given the following linked list. If we perform these four operations: i i. Insert “SHEEP” after “COW”. ii ii. Delete the last element. iii iii. Insert “BULL” at the front of the list. iv iv. Insert “SNAKE” at the end of the list. With the diagram, show the state of the list after each operation above and after the fourth operation: a. What does HEAD point to? b. What does the element with data value “DOG” points to? c. What is the last element? d. Which element points to “PIG”?

  1. Mention the steps to insert data at the start of a singly linked list?

3. Fill in the following

table, using Big-O

notation to give the worst

and average-case times

for each of

4. the stack methods for a

stack of size N.

3. Fill in the following table,

using Big-O notation to give

the worst and average-case

times for each of

the stack methods for a

stack of size N.

  1. Fill in the following table, using Big-O notation to give the worst and average-case times for each of the stack methods for a stack of size N.

9. In a grap

OPERATION WORST-CASE TIME AVERAGE-CASE TIME PEEK POP PUSH IS EMPTY IS FULL

  1. Describe what is output by the following code or explain why an error occurs. Node *p1, *p2; p1 = new(nothrow) Node; p2 = new(nothrow) Node; p1->data = 12; p2->data = 34; p1 = p2; p2->next = p1; cout << p1->data << " " << p2->data << endl; cout << p1->next->data << " " << p2-next->data << endl; STACKS
  2. Represent the stack below, would there be an overflow or underflow? Stack s; s.push( 50 ); s.push( 66 ); s.push( 25 ); s.pop( );

2. Fill in the following

table, using Big-O

notation to give the worst

and average-case times

for each of

3. the stack methods for a

stack of size N.