Double linked list c program code


















Traversal of a doubly linked list is similar to that of a singly linked list. We have to first check for a condition: whether the linked list is empty or not. This helps to set the start pointer at a proper location. After that we access each node till end. A new node can be inserted very easily in a doubly linked list. Insertion in a doubly linked list can be done in multiple ways:. Insertion in between the nodes.

Insertion at the beginning. New node 2. Set prev and next pointers of new node point next of newNode to the first node of the doubly linked list point prev to null Reorganize the pointers changes are denoted by purple arrows 3. Insertion in between two nodes Let's add a node with value 6 after node with value 1 in the doubly linked list. Set the next pointer of new node and previous node assign the value of next from previous node to the next of newNode assign the address of newNode to the next of previous node Reorganize the pointers 3.

Insertion at the End Let's add a node with value 6 at the end of the doubly linked list. Create a new node New node 2. Set prev and next pointers of new node and the previous node If the linked list is empty, make the newNode as the head node. Otherwise, traverse to the end of the doubly linked list and Reorganize the pointers. Each node consists of a data value, a pointer to the next node, and a pointer to the previous node.

Traversal can occur in one way only forward direction. Traversal can occur in both ways. It requires less space. It requires more space because of an extra pointer. It can be implemented on the stack.

It has multiple usages. It can be implemented on the stack, heap, and binary tree. Share on:. Did you find this article helpful? Sorry about that. Your code is extremely hard to read, which makes it hard to concentrate a review on the specific points you want reviewed although I think that the general structure of your linked list is actually alright.

I would probably put data and link together in one class, but other than that it seems fine. When writing code, try to think about the people who are going to read it this might include you in the future. I think you get the idea. It would be better if you would catch those exceptions, report the problem to the user, and ask for input again.



0コメント

  • 1000 / 1000