我正在尝试读取一个文本文件,根据文本文件中的数字对每个用户进行排序,然后将其添加到链接列表中。然后再次按降序显示每个节点的用户编号。我也是尝试添加编辑功能,例如删除或更新内容。
txt文件就像
John Doe; 10
莎莉莉莉; 5 詹姆斯沃森; 12我所取得的成就是:
list.h:
#include<string>
#ifndef LIST_H
#define LIST_H
class list{
private:
struct node{
std::string data;
node* next;
};
typedef struct node* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
list();
void AddNode(std::string addData);
void DeleteNode(std::string delData);
void PrintList();
};
#endif
list.cpp
#include <cstdlib>
#include <iostream>
#include "list.h"
using namespace std;
list::list(){ //constructor
head = NULL;
curr= NULL;
temp=NULL;
}
void list::AddNode(string addData){
nodePtr n = new node; //nodePtr is node*
n->next=NULL; //find node n is pointing to, access its next element make it point to null
n->data= addData;
if(head != NULL) { // if we have at least 1 element in the list .
curr = head; // take the current pointer we are working with and make it same with head pointer pointing to.(current= front of list)
while(curr->next !=NULL){ // are we at the end of the list.
curr = curr->next;//we are not end of the list.curr pointer points next node.
}
curr->next = n;
}else{ //if we dont have at least 1 element in the list.
head =n;
}
}
void list::DeleteNode(string delData){
nodePtr delPtr = NULL;
temp = head;
curr = head;
while(curr != NULL && curr->data != delData){ // look for data user wants to delete
temp = curr;
curr = curr->next;
}
if(curr == NULL){ // we cant find we are looking for.
cout << delData << "not in list"<<endl;
delete delPtr;
}else{ // we found it
delPtr = curr;
curr = curr->next; // with those 2 lines we are patching the hole in the list.
temp->next = curr;
if(delPtr == head){
head = head->next;
temp = NULL;
}
delete delPtr;
cout<<delData<<"deleted"<<endl;
}
}
void list::PrintList(){
curr = head;
while(curr !=NULL){
cout<<curr->data<<endl;
curr = curr->next;
}
}
int main(){
list mylist;
mylist.AddNode("hello");
mylist.AddNode("how u doin");
mylist.AddNode("good");
mylist.PrintList();
return 0;
}
阅读txt文件后,我应该将它们放入一个向量(或直接列表)然后排序然后放到链表?我想它错了我应该直接把它们排序然后在屏幕上打印之前排序。我也不知道如何通过用户输入功能更新节点。
更新: 我实现了从txt文件向链表添加内容。
string file;
ifstream filein;
cout << "Enter file name:"<<endl;
cin >> file;
filein.open(file);
for(;filein.fail();)
{
cout << "Cannot open the file"<<endl;
cout << "Enter file name:"<<endl;
cin >> file;
filein.open(file);
}
string cline;
string cname;
string csurname;
int money;
string smoney;
string lastdata;
char delimiter=';';
while (std::getline(filein, cline)){
std::istringstream iss(cline);
while(iss >> cname >> csurname >> delimiter >> money){
ostringstream temp; // int to string
temp<<money;
smoney=temp.str();
lastdata = cname+" "+csurname+" "+smoney;
mylist.AddNode(lastdata);
}
}
mylist.PrintList();
现在它增加了喜欢 约翰多伊10 莎莉莉莉5 问题是我将如何达到这个10并在屏幕上打印时对它们进行排序
答案 0 :(得分:0)
解决问题的一种方法是创建两种方法,一种处理比较,另一种处理链表。
以下方法涉及比较。在这种情况下,它比较字符串的长度,但它可以是任何东西。
bool list::IsGreater(nodePtr a, nodePtr b)
{
if (a->data.length() > b->data.length())
return true;
return false;
}
第二种方法对链表进行排序。即使存在排序方法,最简单的方法是在方法AddNode期间对列表进行排序。
void list::SortList() {
for (nodePtr i = head; i != NULL; i = i->next) {
nodePtr prev = NULL;
for (nodePtr j = head; j != NULL && j->next != NULL; j = j->next) {
if (IsGreater(j, j->next)) {
if (prev)
prev->next = j->next;
else
head = j->next;
nodePtr tmp = j->next;
j->next = j->next->next;
tmp->next = j;
j = tmp;
}
if (prev == NULL)
prev = head;
else
prev = j;
}
prev = i;
}
}