Professional Documents
Culture Documents
A doubly linked list is a type of linked list where each node has pointers to both the next and the
previous nodes.
2. Display:
Traverse the list from the "head" node to the "tail" node, printing each node's data.
C++ code:
#include <iostream>
class Node {
public:
int data;
Node(int value) {
data = value;
next = nullptr;
prev = nullptr;
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() {
head = nullptr;
tail = nullptr;
if (!head) {
head = newNode;
tail = newNode;
return;
newNode->prev = tail;
tail->next = newNode;
tail = newNode;
void display() {
while (current) {
current = current->next;
}
};
int main() {
DoublyLinkedList list;
list.append(10);
list.append(20);
list.append(30);
list.display();
return 0;
===================================================================================
A circular linked list is a type of linked list where the last node's "next" pointer points back to the first
node, forming a loop.
2. Display:
o Traverse the list starting from the "head" node until you reach the "head" node again,
printing each node's data.
C++ code:
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
};
class CircularLinkedList {
private:
Node* head;
Node* tail;
public:
CircularLinkedList() {
head = nullptr;
tail = nullptr;
if (!head) {
head = newNode;
tail = newNode;
newNode->next = head;
return;
newNode->next = head;
tail->next = newNode;
tail = newNode;
void display() {
if (!head) {
return;
do {
current = current->next;
};
int main() {
CircularLinkedList list;
list.append(10);
list.append(20);
list.append(30);
list.display();
return 0;