Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer all the question in Lab2 using Java, Thank you Lab2: Tasks: 1. Using your favorite programming language, implement a singly linked list with

Please answer all the question in Lab2 using Java, Thank you

Lab2:

Tasks:

1. Using your favorite programming language, implement a singly linked list with the following operations: addFirst, removeFirst, addLast, removeLast, getFirst, getLast, size.

2. [2 bonus marks*] Write and implement a recursive algorithm that shows all the elements of the single linked list, starting from the head. Explain the worst-case running time of your algorithm.

3. Implement a stack on the singly linked list with the operations of Lab Assignment 1. Hint: Using the same Stack class you implemented, change the array to an object of the singly linked list class. The functionality of push and pop is now based on the methods of the linked list class.

4. Practical application 1: Balanced parentheses.

(a) Implement the balanced-bracket checker algorithm of Lab Assignment 1 using the singly linked list implementation of the stack.

(b) Explain how your algorithm checks for balanced parenthesis in O(n), when the input is a string of length n.

5. Implement a queue on the singly linked list of Item #1, with the following operations: enqueue, dequeue, front, size, isEmpty. Note: both operations, enqueue and dequeue, must run in O(1).

6. Practical application 2: The merge problem. Given two sorted lists of integers, A and B, the aim is to merge A and B, producing a sorted list S.

(a) Write an algorithm that stores A and B in two queues (one in each queue), merges A and B, and outputs a sorted list S in O(n) worst-case time. Note: your algorithm must use queues for A, B and S.

(b) Implement your algorithm in your favorite programming language.

(c) Explain how your algorithm runs in O(n), where n is the length of S.

(d) [2 bonus marks*] Run your algorithm on randomly generated sorted lists A and B of different sizes: 100, 200, 300, , 2000 and record the CPU time taken by the algorithm. Create a table (or a plot) and explain how your algorithm runs in O(n) by inspecting your results.

I have also attached the contents of Lab1 below: 1. Using your favorite programming language (Java is suggested), design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation.

2. Practical application 1: Arithmetic operations.

(a) Design an algorithm that takes a string, which represents an arithmetic operation, as input and checks whether or not the brackets are correct (balanced) or incorrect (unbalanced). The input string may contain a combination of the following characters: {,},[,],(,),0,1,2,3,4,5,6,7,8,9,+,-,*,/. An obvious hint for this algorithm is to use the stackbased solution discussed in class as a template, and hence you can use the stack implemented in #1. Your algorithm must not check the correctness of the arithmetic operators/operands, but only check for balanced brackets. Your algorithm should also show an error message when the string contains a character that is not one of those listed above. Provide the pseudocode of the algorithm.

(b) Implement the algorithm of (a) in your favorite programming language. Run the program in several cases, including the following (more examples to be asked when the assignment is submitted):

i. (9*[3*{[(3+3)/5]*7}])

ii. {3*(2+[3-[4/[6/9]]]})

iii. ((3*(9-(4*(6-5))))

iv. {2-{3*{6/[[[(((9-0)))]]]}}/7}

(c) Assuming an input string of size n:

(i) what is the worst-case time that your algorithm takes to decide whether or not the string is correct (balanced) or incorrect (unbalanced)?

(ii) Why? Give your answers in terms of the O-notation.

3. Practical application 2: Context-free language recognizer.

(a) Design an algorithm that recognizes (accepts) strings of the following context-free language: L = {0n 1 n : where n 1}. That is, strings that contain a number of zeros followed by the same number of ones. Examples of strings in the language are: 01, 0011, 000111, etc. Strings that are not in the language are, for example, 10, 00, 111, 1100, 0101, and many more. The algorithm should take a string of 0s and 1s as input and output Yes if the string belongs to the language, and No if the string doesnt belong to the language. The algorithm should use a stack. When it reads a 0 from the string, it pushes 0 onto the stack. When it reads a 1, it checks if the stack has a 0, in which case that 0 is popped from the stack. When the stack is empty and the end of the string is reached, the string is recognized as correct (Yes). Otherwise, the string is not in the language (No).

(b) Implement the algorithm of (a) in your favorite programming language and run it on different strings as follows: 01, 000111, 00000001111111, and 10, 00, 00111, 0101, and others.

(c) Assuming an input string of size n:

(i) what is the worst-case time that your algorithm takes to decide whether or not the string belongs to the language?

(ii) Why? Give your answers in terms of the O-notation.

(d) [2 bonus marks] Run your algorithm on strings of lengths n = 2, 4, 8, 16, 32, , 220 . Compute the running time in nanoseconds and draw a table with the results. Draw a table or a plot of the results for different values of n and compare the results obtained with the complexity you found in (c). Note: if all (or most of) the running times become 0, try running the program 1,000 times and compute the average.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probability & Statistics For Engineers & Scientists

Authors: Ronald E. Walpole, Raymond H. Myers, Sharon L. Myers, Keying

7th Edition

9789813131279, 130415294, 9813131276, 978-0130415295

Students also viewed these Databases questions