From bb41921c7ca92abbe27d81df0c47e2b93c315149 Mon Sep 17 00:00:00 2001 From: sarth23 <60581001+sarth23@users.noreply.github.com> Date: Mon, 5 Oct 2020 08:39:58 +0530 Subject: [PATCH] Create Circular Linked List C++ --- .../Linked List/Circular Linked List C++ | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 C++/Data Structure/Linked List/Circular Linked List C++ diff --git a/C++/Data Structure/Linked List/Circular Linked List C++ b/C++/Data Structure/Linked List/Circular Linked List C++ new file mode 100644 index 00000000..363ac7ac --- /dev/null +++ b/C++/Data Structure/Linked List/Circular Linked List C++ @@ -0,0 +1,94 @@ +#include +using namespace std; + +class Node{ +public: + int data; + Node* next; +}; + +class CircularLinkedList{ +private: + Node* head; +public: + CircularLinkedList(int A[], int n); + void Display(); + void recursiveDisplay(Node* p); + Node* getHead(){ return head; } + ~CircularLinkedList(); + + +}; + +CircularLinkedList::CircularLinkedList(int *A, int n) { + + Node* t; + Node* tail; + + head = new Node; + + head->data = A[0]; + head->next = head; + tail = head; + + for (int i=1; idata = A[i]; + t->next = tail->next; + tail->next = t; + tail = t; + } +} + +void CircularLinkedList::Display() { + Node* p = head; + do { + cout << p->data << " -> " << flush; + p = p->next; + } while (p != head); + cout << endl; +} + +void CircularLinkedList::recursiveDisplay(Node *p) { + static int flag = 0; + if (p != head || flag == 0){ + flag = 1; + cout << p->data << " -> " << flush; + recursiveDisplay(p->next); + } + flag = 0; +} + +CircularLinkedList::~CircularLinkedList() { + Node* p = head; + while (p->next != head){ + p = p->next; + } + + while (p != head){ + p->next = head->next; + delete head; + head = p->next; + } + + if (p == head){ + delete head; + head = nullptr; + } + +} + + +int main() { + + int A[] = {1, 3, 5, 7, 9}; + + CircularLinkedList cl(A, sizeof(A)/sizeof(A[0])); + + cl.Display(); + + Node* h = cl.getHead(); + cl.recursiveDisplay(h); + + return 0; +}