简体   繁体   中英

sort strings in alphabetical order with linked lists in c++

I am doing an assignment of linked list of strings. Everything seems to work fine until I get to the sorting part. I am using classes as well. I already have 2 functions. The first one minimum(string) where it returns the string that would come first in alphabetical order.

This functions works fine. I also have a function remove(string) where it removes a node containing the given string from the linked list. It returns true if successful, otherwise false (if string was not in the list), in this case I have to erase the string returned by the minimum(string) function. The function I am having trouble is sort() .

It wants me to define a StringNode pointer to be the head of a new list (the empty list) then I need a loop where I will call the functions of minimum(string) and remove(string) . Inside the loop I also need to insert this node into the proper position in the new list (append it to the end). The old head pointer (now empty) must point to the new list.

I am very confused about this, so far I have this:

void StringList::sort()
{
    StringNode *newList = new StringNode;
    newList = NULL;

    string mini;
    bool removed;

    while (newList->next != NULL)
    {
        StringNode *newList2 = new StringNode;
        StringNode *p = head;
        mini = minimum();
        p->data = mini;
        p->next = NULL;
        newList2 = newList2->next;
        newList2->next = p;
        removed = remove(mini);
        newList = newList2;
    }
}

How I understand is: I need to create a new node which will be an empty list meaning newList->next = NULL; then on the loop I need to create another new node, and a pointer that will point to the head of new node inside the loop. I need to make the value given by the minimum be stored in the pointer p and the pointer to point back to the new node.

Any help will be greatly appreciated. Thanks!

Here is the hole program.

// StringList.cpp

#include <iomanip>
#include <iostream>
#include <sstream>
#include "StringList.h"

using namespace std;

//******************************************************************************
// StringList: creates an empty list
//******************************************************************************

StringList::StringList()
{
    head = NULL;
}

//******************************************************************************
// StringList: deallocates all the nodes in StringList
//******************************************************************************

StringList::~StringList()
{
    StringNode *p;
    StringNode *n;

    p = head;

    while (p != NULL)
    {
        n = p->next;
        delete p;
        p = n;
    }

}

//******************************************************************************
// count: returns the total number of nodes in the list.
//******************************************************************************

int StringList::count()
{
    int count = 0;
    StringNode *p;
    p = head;
    while ( p != NULL )
    {
       count++;
       p = p->next;
    }
    return count;
}

//******************************************************************************
// add: adds a new node to the beginning of the list.
//******************************************************************************

void StringList::add(string movie)
{
    StringNode *newNode = new StringNode;
    newNode->data = movie;
    newNode->next = head;
    head = newNode;
}

//******************************************************************************
// remove: removes a node containing the string from linked list
// returns true if successful or false the string is not in the list
//******************************************************************************

bool StringList::remove(string movie)
{
    StringNode *p = head;
    StringNode *n = NULL;

    while (p != NULL && p->data != movie )
    {
        n = p;
        p = p->next;

      if (p == NULL )
      {
         return false;
      }
      else if (p->data == movie)
      {
         n->next = p->next;
         delete p;
         return true;
      }
    }
}

//******************************************************************************
//display: Displays the strings in the list.
//******************************************************************************

void StringList::display()
{
    StringNode *p;
    p = head;

    while (p != NULL)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}

//******************************************************************************
//minimum: return the string in alphabetical order
//******************************************************************************

string StringList::minimum()
{
    StringNode *p = head;

    string minimum = p->data;

    while (p->next != NULL)
    {
        p = p->next;
        if(minimum > p->data)
        {
             minimum = p->data;
        }
    }
    return minimum;

}

//******************************************************************************
//sort: will call the minimum function and remove function
//******************************************************************************

 void StringList::sort()
{
StringNode* newhead;  // create a new head pointer
string mini;
bool removed; 

//adding the first node to the new list.
StringNode *newnode = new StringNode;
mini = minimum();  // get the minimum from the existing linked list
newnode->data = mini; 
newnode->next = NULL;
newhead=newnode; //add the minimum node to the new list(with the newhead)
StringNode *p = newhead;

while (head != NULL) // loop should run until there's no node left in the original list
{
    StringNode *newnode = new StringNode;
    mini = minimum();  // get the minimum from the existing linked list
    newnode->data = mini; 
    newnode->next = NULL;
    p->next=newnode; //add the minimum node to the new list(with the newhead pointer)
    removed = remove(mini);
    p=p->next;  
}
 head=newhead; //finally change the head pointer, so that the head now points to sorted list.
}

// StringList.h

#ifndef STRINGLIST_H_INCLUDED
#define STRINGLIST_H_INCLUDED

#include <string>
using namespace std;

class StringList
{
  private:
    struct StringNode          // the Nodes of the linked list
    {
        string data;           // data is a string
        StringNode *next;      // points to next node in list
    };

    StringNode *head;           // the head pointer

  public:
    StringList();
    ~StringList();

    int count();
    void add(string);
    bool remove(string);
    void display();
    string minimum();
    void sort();
};



#endif // STRINGLIST_H_INCLUDED

//Driver.cpp

#include<iostream>
#include<iomanip>
using namespace std;

#include "StringList.h"

int main()
{
    //testing StringList
    StringList slist;

    string movie1 = "Star Wars";
    string movie2 = "Fargo";
    string movie3 = "Back to the Future";
    string movie4 = "Titanic";

    // Testing add/display/count
    cout << "Testing add/display/count: " << endl;
    cout << "count is: " << slist.count() << endl;

    slist.add(movie1);
    slist.display();
    cout << "count is: " << slist.count() << endl;

    slist.add(movie2);
    slist.display();
    cout << "count is: " << slist.count() << endl;

    slist.add(movie3);
    slist.add(movie4);
    slist.display();
    cout << "count is: " << slist.count() << endl;

    // Testing remove
    cout << endl;
    cout << "Testing remove: " << endl;
    bool delResult;
    delResult = slist.remove(movie4);
    cout << "remove result movie4 = " << boolalpha << delResult << endl;

    delResult = slist.remove(movie3);
    cout << "remove result movie3 = " << boolalpha << delResult << endl;

    delResult = slist.remove("Not There");
    cout << "remove result Not There = " << boolalpha << delResult << endl;

   cout << "display after remove: " << endl;
   slist.display();
   cout << "count is: " << slist.count() << endl;

   //Testing minimum
    cout << endl;
    cout << "Testing minimum: " << endl;

    cout << "Test minimum 1: " << endl;
    slist.display();
    cout << "minimum: " << boolalpha << slist.minimum() << endl;

    cout << "Test minimum 2: " << endl;
    slist.add(movie4);
    slist.display();
    cout << "minimum: " << boolalpha << slist.minimum() << endl;

    cout << "Test minimum 3: " << endl;
    slist.add(movie3);
    slist.display();
    cout << "minimum: " << boolalpha << slist.minimum() << endl;

    //Testing sort and display

    cout << endl;
    cout << "Testing sort/display: " << endl;
    slist.sort();
    slist.display();

    cout << endl;
    cout << "Testing sort/display after add: " << endl;
    slist.add("Jurassic Park");
    slist.display();
    cout << "now sorted: " << endl;
    slist.sort();
    slist.display();


}

your remove function has small fault. You didn't take care of the possibility of removing the first node.

bool StringList::remove(string movie)
{
StringNode *p = head;
StringNode *n = NULL;
if(p->data==movie)  //added this condition
{
    head=head->next;
    delete p;
    return true;
}
while (p != NULL && p->data != movie )
{
    n = p;
    p = p->next;

  if (p == NULL )
  {
     return false;
  }
  else if (p->data == movie)
  {
     n->next = p->next;
     delete p;
     return true;
  }
}
}

final sort function. The objective is to sort the existing list by using the minimum and remove functions and get the new sorted list. You need to get minimum from the existing list and keep adding them at the end of the new list. And finally change the head pointer to point it to the new list.

The while loop should run until the existing list becomes empty.

void StringList::sort()
{  
   string mini;
   bool removed;

  //adding the first node to the new list.
  StringNode *newnode1 = new StringNode;
  mini = minimum();  // get the minimum from the existing linked list
  newnode1->data = mini;
  newnode1->next = NULL;
  removed =remove(mini);
  StringNode *p = newnode1;

while (head != NULL) // the loop should run until the original list is empty
{
  StringNode *newnode = new StringNode;
  mini = minimum();  // get the minimum from the existing linked list
  newnode->data = mini;
  newnode->next = NULL;
  p->next=newnode; //add the minimum node to the new list
  removed = remove(mini);
  p=p->next;
}
   head=newnode1; //finally change the head pointer, so that the head now points to sorted list.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM