From ee3168bab5706254d9b3b6004343e3a48fd1ef50 Mon Sep 17 00:00:00 2001 From: vanshammishra <72139336+vanshammishra@users.noreply.github.com> Date: Fri, 16 Oct 2020 13:03:30 +0530 Subject: [PATCH] Update and rename CircularLinkedList.cpp to CircularLinkedList.c --- .../Cpp/Linked List/CircularLinkedList.c | 53 +++++++++++ .../Cpp/Linked List/CircularLinkedList.cpp | 89 ------------------- 2 files changed, 53 insertions(+), 89 deletions(-) create mode 100644 data_structures/Cpp/Linked List/CircularLinkedList.c delete mode 100644 data_structures/Cpp/Linked List/CircularLinkedList.cpp diff --git a/data_structures/Cpp/Linked List/CircularLinkedList.c b/data_structures/Cpp/Linked List/CircularLinkedList.c new file mode 100644 index 00000000..6dbdd2a0 --- /dev/null +++ b/data_structures/Cpp/Linked List/CircularLinkedList.c @@ -0,0 +1,53 @@ +#include +#include +struct node +{ + int data; + struct node * next; +}; +void traverselist(struct node * head) +{ + struct node * p=head; + do + { + printf("%d\n",p->data); + p=p->next; + } while (p!=head); + +} +struct node * insertatfirst(struct node * head, int data) +{ + struct node * ptr=(struct node *)malloc(sizeof(struct node)); + ptr->data=data; + struct node * p=head; + while (p->next!=head) + { + p=p->next; + } + p->next=ptr; + ptr->next=head; + head=ptr; + return head; + +} +int main() +{ + struct node * head=(struct node *)malloc(sizeof(struct node)); + struct node * second=(struct node *)malloc(sizeof(struct node)); + struct node * third=(struct node *)malloc(sizeof(struct node)); + struct node * fourth=(struct node *)malloc(sizeof(struct node)); + head->data=4; + head->next=second; + second->data=5; + second->next=third; + third->data=6; + third->next=fourth; + fourth->data=7; + fourth->next=head; + printf("list before the insertion\n"); + traverselist(head); + head=insertatfirst(head,3); + printf("list after insertion is\n"); + traverselist(head); + return 0; +} diff --git a/data_structures/Cpp/Linked List/CircularLinkedList.cpp b/data_structures/Cpp/Linked List/CircularLinkedList.cpp deleted file mode 100644 index 3ba0efd4..00000000 --- a/data_structures/Cpp/Linked List/CircularLinkedList.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#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; -} \ No newline at end of file