What exactly is a Circular Doubly Linked List? C++ - c++

I have do to an assignment where we are to do a doubly linked list with no head or tail. I want to find examples of this very thing so I can understand it better. Now a Circular Doubly Linked List C++ is just that is it not? When I look up this in google I get examples with head and/or tail. I just want clarification so I do not make a mistake and be way behind. I have asked the professor but I do not think he checks his emails as often as I would like.

Simple:
A "linked list" is when each node contains a pointer to the next.
A "doubly linked list" is where each node contains both a forward pointer (to the next element), and a backward pointer (to the previous element).
Finally, a "circular doubly linked list" has a finite length - the final element points forward to the first, in a "circle".

Every link list be composed of nodes.
Every node will be composed of tuples which are composed of (elements, links).
Every element is the very thing that is contained.
Every link is a pointer to another node.
Next circular linked list are those that have the link connected in such a way where when one transverse the links one can arrive to the start without any change in direction.
This is can be done singularly or through multiple directions.

Related

Deleting previous node in singly linked list if pointer to only that given current node is given

I have to delete the previous node. I searched web but couldnt find the exact sol cuz the list given is singly list and we are not given any other pointer except the current one. How to do this?
The list is neither circular nor doubly linked.
I won't write the code, but you can get the gist of the algorithm. Note that this is assuming that this is a single-linked list structure
Mark the current node of the list
Iterate through the list with a loop. For each node,
Check if the next node pointer is the marked node.
If it is, execute the delete operation for a singly linked list.
If it is not a marked node, move to the next node.

Can a linked list have no data elements in it and still be a linked list?

I read this while studying for interview questions:
"A linked list must have two things. It must have some data in each node, and it must have a reference to the next node. "
But can a linked list just have references to the next node?
No, it will not be a linked list (by definition).
a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.
Linked list - wiki
I think that you just asked a bad question.
What would be the meaning of linked list , with nodes of empty information? You can implement it, but there is no meaning.

Insertion/Deletion of Doubly Linked List in constant time O(1)

a) I have seen a lot of examples of this question in stackoverflow but I am still having problems understanding how does it know which node it is referring when call functions like insertAfter(Node n, Object o). If we say insert after node 2, how does linkedlist know which node is node 2?
b) in the previous posts in stackoverflow, it is said there is a pointer to a node that you want to insert after or before, that is why we get constant time operations. does that mean, just like we have to head and tail in linked list, we also have a pointer to each node?
would really appreciate help in understanding this.
If deletion is done using key then your point is valid because we dont know the position of the element to be deleted and thus finding key in the list makes its time linear in the length of the queue. But at places where it is written to be Constant time is deletion by address. So one may always move to that address and delete the node in constant time.
Note : This ain't possible with singly linked list.

Determine the first node in a Linked List

If the last node of a linked list is connected to the first node, it makes a ring.
Then how would you identify which of the nodes in the linked list is the first node and the last one?
You wouldn't. If it is a ring, then first and last are meaningless. Any node can be first or last.
If you define "first" as "created first", then you would probably want to add some sequencing information to the nodes to be able to know that.
Presumably there would be a pointer to the first node of a linked list (you need a way of entering the list). Additionally it is convenient in this case to maintain a pointer to the last node in the list.
If you are more specific about what you need to know I can be more helpful.
What you are describing is a circularly linked list. It is possible to know both the first and last item in the list based off just maintaining the last node. Logically that requires it's successor to be the first node.
wikipedia has a bit more about it: https://en.wikipedia.org/wiki/Linked_list#Circularly_linked_list
If you are trying to implement a circular list, have a look here http://en.wikipedia.org/wiki/Linked_list#Circularly_linked_list
When you create the Linkedlist, store the address of the first node in a variable(preferably a private variable) so that at any point of time , you can compare this address with the current node's address

Trying to jump directly to a node in a linked list

Hello I dont know if there is a command in C++ which I can use to jump directly to 5th node in a linked list? I know with : p->next i can try to go to the next node but what if I want to go to the 56th right awat is there a way ? like p->next(56) or something ? Thanks
If the linked list does not have a command like p->get(56) built in then you have to write your own function that uses a for loop. It takes the list and the number of the element you want and then calls next that number of times.
There is no such "command". A characteristic of a linked list is that it is slower to locate a particular node by position. Unless of course, you've already stored a pointer to that node.
If this is a problem, then a linked list is not the correct data structure for your purposes.
At least if you have an iterator of the category InputIterator (those of std::list are of this category) you can use std::advance. For example, if you want to get the iterator pointing to the fifth element from the beginning of the list:
std::list<int> l;
// ...
std::list<int>::iterator it = l.begin();
std::advance(it, 4);
// Now it points to the fifth element
But as the others already mentioned: A linked list isn't supposed to have random access. You always have to travel through it in order to get a certain entry. And thus std::advance will perform very poor for large lists.
C++ doesn't provide a linked list type that you can access at that level. It does have std::list<>, which provides encapsulation. You can not directly index into a linked list... though you can advance 56 steps from the first (or some other already-found) element, but each node must be traversed and this is relatively inefficient. If you need better performance, you should reconsider your choice of container: perhaps a vector or map would be more appropriate.
This is the nature of linked lists. You'll have to traverse all the way to the nth element.