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並進行排序,同時在屏幕上打印
我不確定我完全理解你在做什麼。你有沒有嘗試按排序順序添加節點?換句話說,不是走到列表末尾來添加節點,你是否嘗試着查看你在列表中傳遞的節點,以便知道何時插入新節點? –
即時計劃做從txt文件讀取數據後,例如,如果有5行調用adddata函數5次,並將它們添加到linkedlist.then當用戶想打印數據排序數據,並顯示在屏幕上。如果用戶想要添加數據控制檯再次調用adddata函數。或者在將txt文件加載到鏈接列表時直接加載它們,並在用戶想要添加數據時調用adddata函數 – rektandlove