Question

In: Computer Science

In C++ Question 1) Theoretical Questions 1.1) Why is it better to store the tail pointer...

In C++

Question 1) Theoretical Questions

1.1) Why is it better to store the tail pointer rather than the head pointer in a circular singular linked list?

1.2) What are the main advantages of a doubly linked list compared to a singularly linked list?

Question 2) Practical Questions

Suppose a doubly-linked list makes use of a class called DLNode, defined as follows:

template<class T>

struct DLNode

{

T data;

DLNode<T> * next;

DLNode<T> * prev;

};

Further suppose the linked list class maintains a pointer to the first node in the list as a member variable called DLNode<T> *head.

2.1) Suppose left and right are variables of type DLNode<int>* pointing to two nodes that are adjacent in a doubly-linked list, i.e left->next points to right. Both nodes have predecessors and successors in the list. Write the code taht will swap the nodes in the list by adjusting pointers. Do not change the value of the data field of either nodes.

2.2) Given a circular doubly-linked list of 5 integers (1,2,3,4,5), write a function printAll() to output the list in the following order: (1,5,2,4,3). The function should work on any odd number of nodes.

Solutions

Expert Solution

1.1

My understanding...

Advantages:

  • Inserting at the end is O(1) instead of O(N).
  • If the list is a Doubly Linked List, then removing from the end is also O(1) instead of O(N).

Disadvantage:

  • Takes up a trivial amount of extra memory: 4-8 bytes.
  • The implementer has to keep track of the tail.

Looking at these advantages and disadvantages, I can't see why a Linked List would ever avoid using a tail pointer.

1.2

Following are advantages/disadvantages of doubly linked list over singly linked list.

Advantages over singly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
3) We can quickly insert a new node before a given node.
In singly linked list, to delete a node, pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using previous pointer.


Disadvantages over singly linked list
1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though (See this and this).
2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.


Related Solutions

Theoretical Framework Written Assignments Culminating Project Part V: Research Questions and Theoretical Framework Research Question: Latham...
Theoretical Framework Written Assignments Culminating Project Part V: Research Questions and Theoretical Framework Research Question: Latham (2014) suggested that the foundation of any good research is a good question. We are interested in developing management questions related to the research problem because answers to these questions offer actionable information. (Page 46-54) Theory: Identifying one or two theories enables the researcher to address the research questions. In addition, a theory can help to recognize which constructs in the conceptual framework are...
QUESTION ONE: Refer to the information provided below and answer the following questions: 1.1 Calculate the...
QUESTION ONE: Refer to the information provided below and answer the following questions: 1.1 Calculate the Payback period of the first alternative ( answer expressed in years, months and days) 1.2 Calculate the Accounting Rate of Return (on average investment) of the first alternative. 1.3 On the basis of the Net Present Value, which alternative should be chosen? provide relevant calculations (annuity discount factor @ 12% = 3.6048) 1.4 Calculate the internal Rate of Return of the first alternative (Annuity...
Question 1: Which of the following description best explains why the median may be a better...
Question 1: Which of the following description best explains why the median may be a better measure to describe the number of hours slept than the mode or mean? Question 2: Which measure of central tendency (the mean, median or the mode) best describes the pattern of data for Saturday night? What about for Tuesday night? Explain and defend your answer. Make sure you take a look at the data before you answer this question, and think about whether there...
Discussion Questions 1. Provide three reasons why a theoretical yield may differ from an experimental yield...
Discussion Questions 1. Provide three reasons why a theoretical yield may differ from an experimental yield even when properly following this procedure. 2. When determining the limiting reactant is it reasonable to make the determination that water is not the limiting reactant without calculations. Why or why not? 3. How would the results be affected if the alum was not weighed to a constant mass during the step 6 of the second week’s procedure? 4. If your starting source of...
PLEASE ANSWER ALL 5 QUESTIONS 1. Explain why it is better to wear mittens than gloves...
PLEASE ANSWER ALL 5 QUESTIONS 1. Explain why it is better to wear mittens than gloves on a cold day. 2. Why do animals that have hollow, air-filled tubes for hair have an advantage in cold climates? 3. does a tile floor feel colder than the area rug covering it? 4. Why is it better to wear several layers of clothing than one coat of equal weight? 5. New windows typically have double or even triple panes of glass with...
Question 1 If the theoretical value of a quantity is 3.142, and the calculated value is...
Question 1 If the theoretical value of a quantity is 3.142, and the calculated value is 3.146, what is the percent error? (closest value) Group of answer choices 0.1 % 0.2% 0.3% Question 2 In an experiment to measure the value of pi, the following results are obtained for pi: 3.14                    3.13                 3.24                 3.06                 3.02                                         If the correct value is 3.14159, calculate the Percent Error (closest value) Group of answer choices 0.3% 0.7% 1 % 2% Question 3 What determines the number of significant...
Question 1.1 C Ltd makes two products, Alpha and Beta. The following data is relevant for...
Question 1.1 C Ltd makes two products, Alpha and Beta. The following data is relevant for year 3: Material M £2 per unit Material N £3 per unit Direct labour is paid £10 per hour. Production overhead cost is estimated to be £200,000, which includes £25,000 for depreciation of property and equipment. Production overhead cost is absorbed into product costs using a direct labour hour absorption rate. Each finished unit requires: Alpha Beta Material M 12 units   12 units Material...
These questions tail off of each other so I will post the four parts here: 1....
These questions tail off of each other so I will post the four parts here: 1. ABC Inc., began the year with 10,000 units in stock but finished with 5,000 units. It produced 45,000 units for the period. Its selling price is $12 per unit, variable manufacturing cost is $5 per unit, and variable selling is $3 per unit. Fixed manufacturing and selling costs are $100,000 and $72,000 respectively. The firm notes that variable cost per unit (both mfg and...
Question 1 Marbles is a calico cat with a Manx tail. She wants to have kittens...
Question 1 Marbles is a calico cat with a Manx tail. She wants to have kittens with a male calico, but can't seem to find one. Alas, male calicos are rare; the only ones have the sex chromosome constitution XXY. Male calicos are therefore rare because ___________. a) all male cats have a dominant gene on the y that masks the calico gene on the x b) most male cats only have one y chromosome, so it cannot be shut...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information...
[ Write in C not C++] 1. Define a C structure Covid19Info to store the information of COVID 19 cases of countries around the world. It keeps the name of the country, number of COVID 19 cases, number of deaths, current test positive rate, etc. After defining the structure, declare a variable of Covid19Info type with some initial values for Bangladesh. [8]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT