Question

In: Computer Science

Are the ff. implementations good or bad? Explain your answer. a. ArrayStack: push the items to...

Are the ff. implementations good or bad? Explain your answer.

a. ArrayStack: push the items to the front of the array (index 0), not at the back

b. SLLStack: use the SLL’s tail node as the top of the stack

c. SLLDeque: using a singly-linked list to implement the Deque ADT

d. DLLVector: using a doubly-linked list to implement the Vector ADT

Solutions

Expert Solution

Q1. ArrayStack : push the items to the front of the array(index 0), not at the back;

Ans: Stack follows the FIFO order. FIFO stands for first in first out.The element you add last will be removed or processed first..

Implementing an arraystack by pushing the items to the front of the array is a bad way of implementing the arraystack. It is because, before pushing a element to the front of the arraystack you have to push all elements to the right of the index 0 and then first position will be empty and after this you can store the element to the first position.

Q2.  SLLStack: use the SLL’s tail node as the top of the stack
Ans: It is also a bad way of implementing the STACK using the tail of the single linked list. It is beacause, pushing a element to the stack is taking a constant tiime. Therefore, it has a complexity of O(1). But poping an element from the the stack will take the linear time, it is because we cannot refer to the previous node of the tail node. The only way of reaching at that node is by travelling all the nodes from the head node with the help of head node and after that we can remove the element from the linked list. Therefore, it has the complexity of the O(n).

For example: Let we have a SLLStack as :

1 -> 2 -> 3 -> 4 where head is pointed to 1 and tail is pointed to 4.

Push Operation : Inserting a new element will take a constant time as we have the tail pointed to 4. we only have to make 4 pointed to 5 and new tail will become 5. Therefore push(5) will leads to
1 -> 2 -> 3 -> 4 -> 5

Pop Operation: it has the complexity of O(n). As head is pointed to the 1 and we have to pop the element from the stack, therefore it will remove 5 but there doen't exist any variable which is pointed to 4. Therefore we have to travel upto 4 in the LinkedList and then we have to make the 4 pointed to Null. As we have to traverse through the list in order to delete the 5. Therefore, it has the complexity of O(n).

After pop operation, SLLStack will become:

1 -> 2 -> 3 -> 4

Therefore, due to the complexity of O(n) for the pop operation. It is the bad way of implementing stack using tail node of the linked list.

Q3: SLLDeque: using a singly-linked list to implement the Deque ADT

Ans: It is the good way of implementing Deque ABT using a singly-linked list.

Explanation:

Push operation: In Deque, element can be added in front as well as the end of the deque. and suppose head is pointed to the first element and tail is pointed to the last element. Therefore, adding a elment to the front and at the end can be done in a constant time.. It is O(1) in this case as inserting element at the front and end in a list can be done in a constant time. if head is pointed to firist element and end is pointed to the last element.

Pop Operation : In Deque, element can be remove from front as well as from the end of the deque. As head is pointed to the first element and the tail is pointed to the last element. Here there are two scenario of complexity.

O(1) complexity- for removing the element from the front. It is because we can refer to the next element with the help of head node as it is pointed to the next element. Therefore, deleting the element from the front of the linked list can be done in a constant time.

O(n) complexity - for removing the element from the end. It is because we cannot refer to the previous node of the tail node. The only way of reaching at that node is by travelling all the nodes from the head node with the help of head node and after that we can remove the element from the linked list. Therefore, it has the complexity of the O(n).

Q4. DLLVector: using a doubly-linked list to implement the Vector ADT

Ans4. It is also a good way of implementing Vector Abstract Data Type using doubly-linked list.

Vector abstract data type provides resizable array means that you are not bound to add and remove only definite numbers of elements from the array but you can add and remove elements as much you want. As it involves adding and removing elements from the array. Using double linked list implementation. you can add and remove the elements in a constant time. So it is a good way of implementing the vector abstract data type using the double linked list.


Related Solutions

7a). Are mutations good or bad? Explain your answer. 7b). Explain why loss-of-function mutations are frequently...
7a). Are mutations good or bad? Explain your answer. 7b). Explain why loss-of-function mutations are frequently recessive, whereas gain-of-function mutations are frequently dominant. 7c). Briefly describe expanding nucleotide repeats. How do they account for the phenomenon of anticipation? 7d). What is the difference between a transition and a transversion? Which type of base substitution is usually more common and why? 3 marks
Explain the good/bad of the Dodd-Frank Act.
Explain the good/bad of the Dodd-Frank Act.
Suggest ways to reduce production costs for businesses Is monopoly good or bad? justify your answer
Suggest ways to reduce production costs for businesses Is monopoly good or bad? justify your answer
what is your professional understanding of social stratification and inequality. please explain. is it good, bad,...
what is your professional understanding of social stratification and inequality. please explain. is it good, bad, necessary, functional?
In microeconomics. Are corporations good or bad? Defind your position.
In microeconomics. Are corporations good or bad? Defind your position.
Explain your personal view on over and under-absorption :its good or bad effect to biz and...
Explain your personal view on over and under-absorption :its good or bad effect to biz and customer :the reasons to support your belief
Is having a trade deficit necessarily bad for the United States? Explain your answer.
Is having a trade deficit necessarily bad for the United States? Explain your answer.
What is an inferior good? Explain your answer with examples. Can be a Giffen good ever...
What is an inferior good? Explain your answer with examples. Can be a Giffen good ever have positive income elasticity of demand?
Is government debt good or bad? Please answer in 250 words or more.
Is government debt good or bad? Please answer in 250 words or more.
Would 0% unemployment be good or bad? Defend your answer How do you get from population...
Would 0% unemployment be good or bad? Defend your answer How do you get from population to labor force (i.e. what categories of people do you exclude?) How do you increase the labor force?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT