Implement a class names CircularLinkedList. The data structure backing up this class is a double-linked list that loops, the data stored in each node will be an integer.
QUESTION
Implement a class names CircularLinkedList. The data structure backing up this class is a double-linked list that loops, the data stored in each node will be an integer. Implement the following methods: {enum ClockDirection {ClockWise, CounterClockWise}};
default constructor
destructor
void push(int i, ClockDirection d): this will insert a new node containing the integer provided in the d direction from the head
int pop(ClockDirection d): removes the head node returns its vale and sets the head in the direction specified in d
int peek(): this method will return the integer in the head of the circular list
void rotate(unsigned int n, ClockDirection d) will rotate the head pointer n spaces in the direction specified in d
implement << for your class that pretty prints your circular list in a clockwise direction starting at the head: { head-value, next-value…, last value }
The above program needs to be written in C++. Separate the code for header file(.h) and implementation file(.cc)
Input is up to the user.
SUMMARY
Each node in the circular linked list is used as a doubly-linked list node. This is how the circular linked list is implemented. There are methods used on these nodes which are: push, pop, peek and rotate. As the names themselves say, they are used to push a node into the list, pop the head node, return the head value and rotate the list by ‘n’ nodes respectively. To display the list contents the ‘<<’ operator is overloaded
EXPLANATION
A node class (which consists of basic node structures) that has data and two self-reference pointers which point to the previous and next nodes is implemented. The class names CircularLinkedList has the head node as its member and has certain methods which are used to modify the data in the list.
push() – takes in an integer, makes a node, and adds the node at the end of the list.
pop() – returns and deletes the head node and makes the next node or the previous node as head, based on the given direction.
peek() – returns the data in the head node.
rotate() – rotates the list by ‘n’ nodes in a clockwise direction.
CODE
#include <iostream> using namespace std; enum ClockDirection {ClockWise, CounterClockWise}; class node { public: int data; node *next; node *prev; node() { data=0; next=NULL; prev=NULL; } }; //class names CircularLinkedList class CircularLinkedList { public: /* member */ node *head; CircularLinkedList() { head=NULL; } /* to push a node in the circular linked list*/ void push(int i,ClockDirection dir) { node *newnode=new node; newnode->data=i; newnode->next=NULL; newnode->prev=NULL; if(head==NULL) head=newnode; else { if(dir==ClockWise) { /* if there is only one node */ if(head->prev==NULL) { head->next=newnode; newnode->prev=head; head->prev=newnode; newnode->next=head; } else { head->prev->next=newnode; newnode->prev=head->prev; newnode->next=head; head->prev=newnode; } } else if(dir==CounterClockWise) { /* if only head node */ if(head->next==NULL) { head->prev=newnode; newnode->next=head; head->next=newnode; newnode->prev=head; } /* if there is more than one node */ else { newnode->next=head->next; head->next->prev=newnode; head->next=newnode; newnode->prev=head; } } } } friend ostream& operator<<(ostream& os, const CircularLinkedList &c); /* to pop the head node and make the next or previous node as the head node */ int pop(ClockDirection dir) { int temp=head->data; head->prev->next=head->next; head->next->prev=head->prev; if(dir==ClockWise) head=head->next; else if(dir==CounterClockWise) head=head->prev; return temp; } /* to return the peek value of the list */ int peek() { return head->data; } /* to rotate the cicular linked list to n nodes */ void rotate(int n,ClockDirection dir) { if(dir==ClockWise) { while(n--) head=head->next; } else { while(n--) head=head->prev; } } }; /* to display list contents */ ostream& operator<<(ostream& os, const CircularLinkedList &c) { node *front=c.head; os<<"Displaying the list:\n"; os<<front->data<<" "; front=front->next; while(front!=c.head) { os<<front->data<<" "; front=front->next; } os<<"\n\n"; return os; } int main() { CircularLinkedList c; ClockDirection dir; dir=ClockWise; ClockDirection dir1; dir1=CounterClockWise; c.push(10,dir); c.push(20,dir); c.push(30,dir); c.push(40,dir); c.push(50,dir); cout<<c; c.push(60,dir1); c.push(70,dir1); cout<<c; cout<<"Popped out value: "<<c.pop(dir)<<endl<<endl; cout<<c; cout<<"Peek value: "<<c.peek()<<endl<<endl; c.rotate(3,ClockWise); cout<<"After rotating thrice:\n"; cout<<c; }
OUTPUT
Displaying the list :
20 40 60 80 100
Displaying the list :
20 50 70 40 60 80 100
Popped out value: 20
Displaying the list :
50 70 40 60 80 100
Peek value: 100
After rotating thrice :
Displaying the list :
60 80 100 50 70 40
Also, this the Top 10 Web Development Language Courses.