PLS write program in c thanx Linked List Sorting and Reversi
–PLS write program in c++ thanx
Linked List Sorting and Reversing
Write a C++ using your own linked list class to manage a list of string elements. Besides the basic operations expected of a linked list (add an element, remove an element, check for empty list, report size (number) of elements in the list, and print out the content of the list), the linked list class also needs to have a method called sort() and a method called reverse() the manipulate the string elements. The sort method rearranges the elements in the list so they are sorted in alphabetical order. The reverse method reverses the order of the elements of the list. Do NOT use recursion to implement either of these operations. pls write program in c++ thanx
Solution
#include <iostream>
 #include <string>
 using namespace std;
class Node{ //class node
 public:
 string data;
 Node* next;
   
 void SetData(string aData) { data = aData; };
 void SetNext(Node* aNext) { next = aNext; };
 
 };
 class List{ //class list
 Node *head;
 public:
 List() { head = NULL; };
 void addElement(string data);
 void removeElement(string data);
 int isEmpty(Node* temp);
 int size();
 void print();
 void sort();
 void reverse();
 };
int List::isEmpty(Node* temp){ //method to check whether list is empty or not
 if(temp!=NULL)
 return 1;
 else
 return 0;
 }
void List::addElement(string data){ //method to add
 Node* newNode=new Node();
 newNode->SetData(data);
 newNode->SetNext(NULL);
 Node* temp=head;
 if(temp!=NULL)
 {
 while(temp->next!=NULL)
 {temp=temp->next;}
   
 temp->next=newNode;
 }
 else
 head=newNode;
   
 }
 void List::removeElement(string data){ //method for delete
 Node* temp=head;
 Node *prev=head;
 if(isEmpty(temp)==1)
 {//prev=temp;
 while(temp!=NULL)
 {
 if(temp->data==data)
 {prev->next=temp->next;
 temp->next=NULL;
 }
 else
 {prev=temp;
 temp=temp->next;
 }
 }
 
 }
 else
 cout<<\"Nothing to delete as linked list is empty \"<<endl;
 
 }
int List::size(){ //method for size
 int count=0;
 Node* temp=head;
 if(isEmpty(temp)==1)
 {while(temp!=NULL)
 {
 count++;
 temp=temp->next;
 }
 }
 return count;
 }
void List::print(){ //method for print
 Node* temp=head;
 if(isEmpty(temp)==1){
 while(temp!=NULL)
 {
 cout<<temp->data<<\" \";
 temp=temp->next;
 }
   
 }
 }
 int main()
 {
 List list;
 list.addElement(\"anshu\");
 list.addElement(\"anurag\");
 list.addElement(\"how\");
 list.addElement(\"are\");
 list.addElement(\"you?\");
 
 int count=list.size();
   
 cout<<\"size of the linked list= \"<<count<<endl;
 list.print();
   
 list.removeElement(\"anurag\");
 cout<<\"size of the linked list= \"<<count<<endl;
 list.print();
 
 return 0;
 }



