linked list - ordening strings (is it possible?)

Started by
4 comments, last by Fruny 19 years, 4 months ago
hello i wonder if it is possible to read a file with records like "name = value" and put these records in alphabetically ordered using a linked list i have already achieved it with numbers... but strings i tried everything i could with no success... For example, i want to take these three records from a file size = 20 witdh = 30 height = 10 and compare each other to put it alphabetically ordered... i don't have any idea on how to compare strings to achieve this... my list will hold the hole line on each node (eg. "size = 20") i tried even a way to compare only the first character Then, after all have been ordered, i have to put it in another file... can someone help me?
Advertisement
What programming language? And are you using an existing linked list implementation or rolling your own?
sorry, forgot to say.. i'm using C++ and the list i'm using one i got from http://www.gametutorials.com/ a long time ago...

You can use strcmp to compare strings.
-Mike
#include <algorithm>#include <fstream>#include <iterator>#include <list>#include <string>int main(){	std::ifstream reader("infile.txt");	std::list<std::string> records;	std::string record;	while (std::getline(reader, record))	{		records.push_back(record);	}	records.sort();	std::ofstream writer("outfile.txt");	std::copy(records.begin(), records.end(), std::ostream_iterator<std::string>(writer, "\n"));}


Enigma
Quote:Original post by Silent Angel
sorry, forgot to say.. i'm using C++ and the list i'm using one i got from http://www.gametutorials.com/ a long time ago...


You really should use the C++ standard container classes, you know.

#include <list>#include <string>using namespace std;list<string> mylist;mylist.push_back("size = 20");mylist.push_back("witdh = 30");mylist.push_back("height = 10");mylist.sort();


Though, since you say you want to read from and write to a file:

#include <list>#include <string>#include <fstream>using namespace std;int main(){  list<string> mylist;  ifstream input("foo.txt");  string line;  while(!input.eof())  {     getline(input, line);     mylist.push_back(line);  }  mylist.sort();  ofstream output("bar.txt");  list<string>::iterator itor;  for(itor = mylist.begin(); itor != mylist.end(); ++itor)     ouput << *itor << endl;}


Though of course, there are more compact versions using STL algorithms.
Furthermore, at some point or other, you might find beneficial to actually parse the file into an associative array:

#include <map>#include <fstream>#include <sstream>#include <string>using namespace std;int main(){   map<string,int> mymap;   ifstream input("foo.txt");   string line;   while(!input.eof())   {      getline(input, line);      istringstream iss(line);            string key;      int value;            iss >> key;      iss >> ws;    // eat whitespace      iss.ignore(); // the '=' sign      iss >> value;      mymap[key] = value;   }   // can do lookups by key as necessary.   int size = mymap["size"];  // will create a new entry if 'size' not in map.                              // use mymap.find() to avoid that.   // and the keys are in order:   ofstream output("bar.txt");   map<string,int>::iterator itor;   for(itor = mymap.begin(); itor != mymap.end(); ++itor)     output << itor->first << " = " << itor->second << endl;}


Again, proper code would be cleaner and incorporate some decent amount of error-checking. Check your favourite C++ library reference manual.

[disclaimer: I've typed this in a browser without testing the code.]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement