-
-
Save reterVision/402b746c933e913f5182 to your computer and use it in GitHub Desktop.
Circular Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| struct Node | |
| { | |
| int value; | |
| Node* next; | |
| }; | |
| class CircularLinkedList | |
| { | |
| public: | |
| CircularLinkedList(); | |
| ~CircularLinkedList(); | |
| void AddNode(Node* newNode); | |
| void DelNode(Node* delNode); | |
| void PrintNodes(); | |
| protected: | |
| Node* head; | |
| }; | |
| CircularLinkedList::CircularLinkedList() | |
| { | |
| this->head = new Node(); | |
| this->head->value = -1; | |
| this->head->next = this->head; | |
| } | |
| CircularLinkedList::~CircularLinkedList() | |
| { | |
| Node* curr = this->head->next; | |
| while (curr != this->head) | |
| { | |
| Node* temp = curr->next; | |
| this->DelNode(curr); | |
| curr = temp; | |
| } | |
| // Delete the head node. | |
| if (this->head != NULL) | |
| delete this->head; | |
| } | |
| void CircularLinkedList::AddNode(Node* newNode) | |
| { | |
| Node* temp = this->head; | |
| while (temp->next != this->head) | |
| temp = temp->next; | |
| Node* next = temp->next; | |
| newNode->next = temp->next; | |
| temp->next = newNode; | |
| } | |
| void CircularLinkedList::DelNode(Node* delNode) | |
| { | |
| Node* prev = this->head; | |
| Node* curr = prev->next; | |
| while (curr != this->head && curr != delNode) | |
| { | |
| prev = curr; | |
| curr = curr->next; | |
| } | |
| if (curr == this->head) | |
| { | |
| cout << "No such node in the list!" << endl; | |
| return; | |
| } | |
| prev->next = curr->next; | |
| curr->next = NULL; | |
| delete curr; | |
| } | |
| void CircularLinkedList::PrintNodes() | |
| { | |
| Node* temp = this->head->next; | |
| while (temp != this->head) | |
| { | |
| cout << temp->value << endl; | |
| temp = temp->next; | |
| } | |
| cout << temp->value << endl; | |
| } | |
| int main(int argc, char* argv[]) | |
| { | |
| CircularLinkedList circularLinkedList; | |
| Node* newNode_1 = new Node(); | |
| newNode_1->value = 1; | |
| newNode_1->next = NULL; | |
| Node* newNode_2 = new Node(); | |
| newNode_2->value = 2; | |
| newNode_2->next = NULL; | |
| Node* newNode_3 = new Node(); | |
| newNode_3->value = 3; | |
| newNode_3->next = NULL; | |
| circularLinkedList.AddNode(newNode_1); | |
| circularLinkedList.AddNode(newNode_2); | |
| circularLinkedList.AddNode(newNode_3); | |
| circularLinkedList.PrintNodes(); | |
| cout << "After remove 2" << endl; | |
| circularLinkedList.DelNode(newNode_2); | |
| circularLinkedList.PrintNodes(); | |
| cout << "After remove 1" << endl; | |
| circularLinkedList.DelNode(newNode_1); | |
| circularLinkedList.PrintNodes(); | |
| getchar(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment