Question

# What is the difference between a doubly and singly linked list and what would this code...

What is the difference between a doubly and singly linked list and what would this code look like if it were a doubly linked list?
template <typename Object>
class List
{
private:
struct Node
{
Object data;
Node   *prev;
Node   *next;

Node( const Object & d = Object{ }, Node * p = nullptr, Node * n = nullptr )
: data{ d }, prev{ p }, next{ n } { }

Node( Object && d, Node * p = nullptr, Node * n = nullptr )
: data{ std::move( d ) }, prev{ p }, next{ n } { }
};

A singly linked list is a linked list where the node contains the data and a pointer to the next node in the list

A doubly linked list is type of linked list where the node contains the data and a pointer to the next node as well as the previous node in the list

It allows traversal only in one direction means its head pointer till its ends with NULL

It allows a traversal in both direction means its can go from its head to forward(through next pointer) and backward (through previous pointer)

It uses less memory per node (single pointer)

It uses more memory per node(two pointers)

Structure of single link list is as follow:

It can be seen above, it can go in only forward direction through next pointer.

It can be seen above, it can go in forward and backward direction.

The given code has following information:

1. A class name List.

2. List contains a structure Node which is private.

3. Structure Node contains a Data of Object type and two pointers prev and next, which are pointing to node itself. Basically, it is declaration of doubly link list.

4.Two constructors are given, both have given default parameters.

5. 1st constructor has initialized the structure members with parameters given in constructor call, if parameter is not given, then it initialize with default parameter given in declaration i,e a new object is created and assigned to data member, prev and next pointer is assigned to NULL.

6. 2nd constructor also the same as 1st constructor, but it has 1st parameter has not default values, so it is necessary to give 1st parameter value. Here it copies the data values of 'd' to data member of structure.

#### Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

• ### 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

• ### v. 15 points) implementing data structure operations C++ code for a new public member function to...

v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList...

• ### (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

(The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

• ### Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

• ### What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

• ### Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

• ### Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

• ### Double linked list implementation of PutItem function. How to fix my code to get desired output b...

Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

• ### c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...