(Circular Linked List) This chapter defined and indentified various operations o
ID: 3647776 • Letter: #
Question
(Circular Linked List) This chapter defined and indentified various operations on a circular linked list.a. Write the definitions of the class CircularLinkedList and its member functions. (You may assume that the elements of the circular linked list are in ascending order.)
b. Write a program to test various operations of the class defined in (a).
Explanation / Answer
#include #include using namespace std; struct node { int data; node * next; }; class LinkedList { public: LinkedList(); void add(int A); bool empty(); // return true if the list is empty, otherwise return false void InsertInOrder(ElementType x);//insert a value x in numerical order in the list void Delete(int x); //if value x is in the list, remove x void Display();// Display the data values in the list private: node * first;//pointer to the first node in the list }; LinkedList::LinkedList() { first=NULL; } void LinkedList::add(int A) { if (first==NULL) { first=new node; first->data=A; first->next=first; } else if (first->next==first) { node *curr=first->next; curr=new node; curr->data=A; curr->next=first; first->next=curr; } else { node *curr=first->next; node *prev=NULL; while (curr!=first) { prev=curr; curr=curr->next; } curr=new node; curr->data=A; curr->next=first; prev->next=curr; } } bool LinkedList::empty() { if (first==NULL) return true; else return false; } void LinkedList::Delete(int A) { node *curr=first->next; node *prev=NULL; bool flag=true; if (first->data==A) { first=first->next; delete first; flag=false; } else { while (flag && curr!=first) { if (curr->data==A) { prev->next=curr->next; delete curr; flag=false; } prev=curr; curr=curr->next; } if (flag==true) coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.