C/C++ Doubly Linked List

This tutorial demonstrates the implementation of a doubly linked list and the removal of the prev pointer for a singly linked list.


struct Node{
public:
Member* info; //type is arbitrary, to the linked list implementation
Node* next; //used to point to next node
Node* prev; //used to point to previous node

Node():prev(NULL),next(NULL) {} //default constructor
Node(Node* n):prev(n),next(NULL) {} //prev known constructor
Node(Node* n, Node* m):prev(n),next(m) {} //prev and next known constructor
~Node() { delete info; delete next; } //destructor
};

This list would be held by a pointer:

Node* head;
head = &(new Node(&head);//create Node which knows prev(head) and next is NULL

Then each node is attached in a similar fashion.

To search such a list, use a while loop:

Node* n = head;
while(n->next){
//loop code
n = n->next;
}

Questions/Comments: [email protected]
William. § (marvin_gohan)

RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

Cleveland Furia on How to Clear Google Activity
Sid Wohlfarth on How to Clear Google Activity
Riley Magsayo on How to Clear Google Activity
สมัครรับเครดิตฟรี ทันที on How to improve Remote Desktop Protocol Performance
Neal Wangstad on How to Clear Google Activity
Slavcho Andov on How to install Ubuntu
Slavcho Andov on How to fix bad sectors
Sheraz Ali on How to Backup Android