Modify the Class-Linked-List (You can use the copy paste fromExample 2.4) to a circular Class-Linked-List. #include #include
Question:
Modify the Class-Linked-List (You can use the copy paste fromExample 2.4) to a circular Class-Linked-List.
#include
#include
using namespace std;
class Node{
public:
int value;
Node *next;
Node(){
next =NULL;
}
};
class LinkedL{
private:
Node *head;
public:
voidinsertAtBeginning(int);
voidinsertAtEnd(int);
void insertAtPosition(int,int);
voiddeleteFromBeginning();
void deleteFromEnd();
voiddeleteFromPosition(int);
void print();
LinkedL(){
head =NULL;
}
};
void LinkedL::insertAtBeginning(int v){
Node *temp = new Node();
temp->value = v;
temp->next = head;
head = temp;
}
void LinkedL::insertAtEnd(int v){
Node *temp = new Node();
temp->value = v;
if (head == NULL){
head = temp;
}
else{
Node *ptr = head;
while (ptr->next !=NULL){
ptr =ptr->next;
}
ptr->next = temp;
}
}
void LinkedL::insertAtPosition(int v, int p){
Node *temp = new Node();
temp->value = v;
if (p == 0){
temp->next = head;
head = temp;
}
else{
Node *ptr = head;
while(p>1) {
ptr =ptr->next;
--p;
}
temp->next =ptr->next;
ptr->next = temp;
}
}
void LinkedL::deleteFromBeginning(){
if (head == NULL){
cout<<"List isEmpty"< }
else{
cout<<"Element Deleted:"<value< Node *temp = head;
head = head->next;
delete(temp);
}
}
void LinkedL::deleteFromEnd(){
if (head == NULL){
cout<<"List isEmpty"< }
else if (head->next == NULL){
cout<<"Element Deleted:"<value< delete(head);
head = NULL;
}
else{
Node *temp = head;
while (temp->next->next!= NULL) {
temp =temp->next;
}
cout<<"Element Deleted:"<next->value< delete(temp->next);
temp->next = NULL;
}
}
void LinkedL::deleteFromPosition(int p){
if (head == NULL){
cout<<"List isEmpty"< }
else{
Node *temp, *ptr;
if (p == 0) {
cout<<"Element Deleted:"<value< ptr =head;
head =head->next;
delete(ptr);
}
else{
temp = ptr= head;
while(p>0){
--p;
temp = ptr;
ptr = ptr->next;
}
cout<<"Element Deleted:"<value< temp->next = ptr->next;
// freepth node
free(ptr);
}
}
}
void LinkedL::print(){
if (head == NULL){
cout<<"List isempty"< }
else{
Node *temp = head;
cout<<"Linked List:";
while (temp != NULL){
cout<value<<"->";
temp =temp->next;
}
cout<<"NULL"< }
}
int main() {
cout<<"1. Insert at thebeginning";
cout<<"2. Insert at the end";
cout<<"3. Insert at midddle";
cout<<"4. Delete from beginning";
cout<<"5. Delete from the end";
cout<<"6. Delete from midddle";
cout<<"7. Display the list";
cout<<"0. Exit";
int choice,value,position;
LinkedL llist; //Define instance of a linkedlist class
do {
cout<<"Enter YourChoice: ";
cin>>choice;
switch (choice)
{
case1:
cout<<"Enter Element: ";
cin>>value;
llist.insertAtBeginning(value);
break;
case2:
cout<<"Enter Element: ";
cin>>value;
llist.insertAtEnd(value);
break;
case3:
cout<<"Enter Element: ";
cin>>value;
cout<<"Enter Position (start fromzero): ";
cin>>position;
llist.insertAtPosition(value,position);
break;
case4:
llist.deleteFromBeginning();
break;
case5:
llist.deleteFromEnd();
break;
case6:
cout<<"Enter Position (starting fromzero): ";
cin>>position;
llist.deleteFromPosition(position);
break;
case7:
llist.print();
break;
}
} while (choice != 0);
}