C++ 使用c+中的链表按字母顺序对字符串进行排序+;
我正在做一个字符串链表的赋值。在我进入分拣部分之前,一切似乎都很顺利。我也在使用课堂。我已经有两个函数了。第一个C++ 使用c+中的链表按字母顺序对字符串进行排序+;,c++,string,sorting,linked-list,C++,String,Sorting,Linked List,我正在做一个字符串链表的赋值。在我进入分拣部分之前,一切似乎都很顺利。我也在使用课堂。我已经有两个函数了。第一个最小值(string),返回按字母顺序排在第一位的字符串 这个函数运行良好。我还有一个函数remove(string),它从链表中删除包含给定字符串的节点。如果成功,则返回true,否则返回false(如果字符串不在列表中),在这种情况下,我必须删除minimum(string)函数返回的字符串。我遇到的函数是sort() 它希望我定义一个StringNode指针作为新列表(空列表)的
最小值(string)
,返回按字母顺序排在第一位的字符串
这个函数运行良好。我还有一个函数remove(string)
,它从链表中删除包含给定字符串的节点。如果成功,则返回true,否则返回false(如果字符串不在列表中),在这种情况下,我必须删除minimum(string)
函数返回的字符串。我遇到的函数是sort()
它希望我定义一个StringNode
指针作为新列表(空列表)的头,然后我需要一个循环,在这里我将调用minimum(string)
和remove(string)
的函数。在循环中,我还需要将此节点插入新列表中的正确位置(将其附加到末尾)。旧的头指针(现在为空)必须指向新列表
我对此感到非常困惑,到目前为止我有:
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;
}
}
我的理解是:我需要创建一个新节点,它将是一个空列表,意思是newList->next=NULL代码>然后在循环上,我需要创建另一个新节点,以及一个指向循环内新节点头部的指针。我需要将由最小值给出的值存储在指针p
和指向新节点的指针中
任何帮助都将不胜感激。谢谢
这是钻孔程序
//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.
}
#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();
}
#包括
#包括
#包括
#包括“StringList.h”
使用名称空间std;
//******************************************************************************
//StringList:创建一个空列表
//******************************************************************************
StringList::StringList()
{
head=NULL;
}
//******************************************************************************
//StringList:取消分配StringList中的所有节点
//******************************************************************************
StringList::~StringList()
{
StringNode*p;
StringNode*n;
p=水头;
while(p!=NULL)
{
n=p->next;
删除p;
p=n;
}
}
//******************************************************************************
//count:返回列表中的节点总数。
//******************************************************************************
int StringList::count()
{
整数计数=0;
StringNode*p;
p=水头;
while(p!=NULL)
{
计数++;
p=p->next;
}
返回计数;
}
//******************************************************************************
//添加:将新节点添加到列表的开头。
//******************************************************************************
void StringList::添加(字符串电影)
{
StringNode*newNode=新StringNode;
新建节点->数据=电影;
新建节点->下一步=头部;
头=新节点;
}
//******************************************************************************
//移除:从链接列表中移除包含字符串的节点
//如果字符串不在列表中,则返回true或false
//******************************************************************************
bool StringList::删除(字符串电影)
{
StringNode*p=头部;
StringNode*n=NULL;
while(p!=NULL&&p->data!=movie)
{
n=p;
p=p->next;
if(p==NULL)
{
返回false;
}
else if(p->data==电影)
{
n->next=p->next;
删除p;
返回true;
}
}
}
//******************************************************************************
//显示:显示列表中的字符串。
//******************************************************************************
void StringList::display()
{
StringNode*p;
p=水头;
while(p!=NULL)
{
下一步是收集数据;
}
cout数据;
while(p->next!=NULL)
{
p=p->next;
如果(最小值>p->数据)
{
最小值=p->数据;
}
}
返回最小值;
}
//******************************************************************************
//排序:将调用最小值函数和删除函数
//******************************************************************************
void StringList::sort()
{
StringNode*newhead;//创建一个新的head指针
迷你弦乐;
去除bool;
//将第一个节点添加到新列表中。
StringNode*newnode=新StringNode;
mini=minimum();//从现有链表中获取最小值
newnode->data=mini;
newnode->next=NULL;
newhead=newnode;//将最小节点添加到新列表中(使用newhead)
StringNode*p=newhead;
while(head!=NULL)//循环应一直运行,直到原始列表中没有节点为止
{
StringNode*newnode=新StringNode;
mini=minimum();//从现有链表中获取最小值
newnode->data=mini;
newnode->next=NULL;
p->next=newnode;//将最小节点添加到新列表中(使用newhead指针)
移除=移除(迷你);
p=p->next;
}
head=newhead;//最后更改head指针,使head现在指向已排序的列表。
}
//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
#如果包含NDEF字符串列表#
#定义包含的字符串列表
#包括
使用名称空间std;
类字符串列表
{
私人:
struct StringNode//链表的节点
{
字符串数据;//数据是字符串
StringNode*next;//指向列表中的下一个节点
};
StringNode*head;//头指针
公众:
StringList();
~StringList();
int count();
无效添加(字符串);
bool-remove(字符串);
v
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;
}
}
}
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.
}