Print the doubly linked list in recursive order.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Node{
public:
int data;
Node *next, *prev;
Node();
Node(int);
};
Node::Node(){
next = NULL;
prev = NULL;
}
Node::Node(int data){
this->data = data;
next = NULL;
prev = NULL;
}
class DoublyLinkedList{
Node *head, *tail;
int size;
public:
DoublyLinkedList();
void insert(int);
void printRecursive(Node* c = NULL, Node* b = NULL);
};
DoublyLinkedList::DoublyLinkedList(){
head = NULL;
tail = NULL;
size = 0;
}
void DoublyLinkedList::insert(int data){
if(head == NULL){
head = new Node(data);
tail = head;
size++;
}
else{
tail->next = new Node(data);
tail->next->prev = tail;
tail = tail->next;
size++;
}
}
void DoublyLinkedList::printRecursive(Node* curr, Node* b){
if(curr == NULL){
curr = head;
}
if(curr == b){
cout<<"\n";
return;
}
if(curr != NULL){
cout<<curr->data<<" ";
}
if(b == NULL){
b = head;
}
printRecursive(curr->next, b);
}
int main(){
DoublyLinkedList list;
for(int i = 1; i <= 10; i++){
list.insert(i);
}
list.printRecursive();
return 0;
}
Comments
Leave a comment