2017-10-14 17 views
0

我正在尝试读取文本文件,按照文本文件中的编号对每个用户进行排序,然后将其添加到链接列表中。然后再次显示它,以尊重每个节点的用户编号按降序排列。我试图添加编辑功能,如删除或更新内容。在排序后向链接列表添加数据

txt文件就像

John Doe; 10

Sally Tully; 5

James Watson; 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 问题是IM要去如何到达这个10并进行排序,同时在屏幕上打印

+0

我不确定我完全理解你在做什么。你有没有尝试按排序顺序添加节点?换句话说,不是走到列表末尾来添加节点,你是否尝试着查看你在列表中传递的节点,以便知道何时插入新节点? –

+0

即时计划做从txt文件读取数据后,例如,如果有5行调用adddata函数5次,并将它们添加到linkedlist.then当用户想打印数据排序数据,并显示在屏幕上。如果用户想要添加数据控制台再次调用adddata函数。或者在将txt文件加载到链接列表时直接加载它们,并在用户想要添加数据时调用adddata函数 – rektandlove

回答

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; 
    } 
}