【问题标题】:Adding Data to Linked List after Sorting排序后将数据添加到链表
【发布时间】:2017-10-14 20:09:18
【问题描述】:

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

txt 文件是这样的

约翰·多伊; 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 个并在屏幕上打印时对它们进行排序

【问题讨论】:

  • 我不确定我是否完全理解您想要做什么。您是否尝试按排序顺序添加节点?换句话说,您是否尝试过查看您在列表中传递的节点以便知道何时插入新节点,而不是走到列表的末尾添加节点?
  • 我打算在从 txt 文件中读取数据后执行,例如,如果有 5 行调用 adddata 函数 5 次并将它们添加到linkedlist。然后当用户想要打印数据排序数据并将其显示在屏幕上时。如果用户想再次从控制台添加数据,请调用 adddata 函数。或者在将txt文件加载到链表时直接加载它们,当用户想要添加数据时调用adddata函数

标签: c++ sorting linked-list


【解决方案1】:

解决问题的一种方法是创建两种方法,一种处理比较,另一种对链表进行排序。

下面的方法处理比较。在这种情况下,它正在比较字符串的长度,但它可以是任何东西。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多