This is my whole code I made a function to add to head and a function to print the whole list and a function to sort the list in a descending order. There is no error whenever I run the code and the linked lists doesn't rearrange in a descending order it prints out the same order
#include <iostream>
using namespace std;
#include <string.h>
class Node{
public:
string data;
Node* next , *head;
int counter=0;
};
class tsortedList :public Node{
public:
void printlist(){
Node *t = head;
while (t != NULL){
cout<<t->data<<", ";
t = t->next;
}
}
void insert_at_beginning(string data){
Node *temp = new Node();
temp->data = data;
temp->next = head;
head = temp;
counter++;
}
void Sort(){
Node* temp;
temp = head ;
string info ;
for (int j = 0; j < counter; j++)
{
while ((temp->data) < (temp->next->data) )
{
info=temp->data;
temp->data = temp->next->data;
temp->next->data=info;
temp = temp->next ;
}
temp = temp->next ;
}
temp = head ;
}
};
int main(){
tsortedList obj;
obj.insert_at_beginning("Apple");
obj.insert_at_beginning("bannana");
obj.insert_at_beginning("Zebra");
obj.insert_at_beginning("Horse");
obj.printlist();
obj.Sort();
obj.printlist();
}
from Recent Questions - Stack Overflow https://ift.tt/3pps9Cb
https://ift.tt/eA8V8J
No comments:
Post a Comment