Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Sorted Linked with infile

$
0
0
I am trying to figure out how to sort this list of char item[21] by alphabetical order. Any Input is appreciated.

due soon... :sweatdrop:

//shopping.cpp Implementation file for the Shopping class 
#include "shopping.h" 
#include <iostream> 
#include <string> 
using namespace std;
Shopping * Shopping::tail = NULL;
Shopping::Shopping(int v, char *s) 
{ 
	QTY = v; 
	strcpy(item, s); 
	next = NULL; 
} // constructor
void Shopping::addItem(Shopping *& h) 
{ 
	if (h == NULL) // the list is empty 
	{ 
		h = this; 
		tail = this; 
	} 
	else 
	{ 
		tail->next = this; 
		tail = this; 
	} 
} // addItem
void Shopping::show() 
{ 
	cout << QTY << " " << item << endl; 
	if (next) 
	next->show(); // recursive call to the neighbor 
} // show
void Shopping::clean() 
{ 
	Shopping *t = next; 
	delete this; 
	if (t) 
	t->clean(); 
} // clean

void Shopping::sort()
{
	Shopping* current=top;
	Shopping* min=NULL;
	Shopping* temp=NULL;

	char tempitem;
	int tempQTY;

	///don't know where to go from here.

	
}



// shopping.h declares the Shopping class 
#ifndef SHOPPING 
#define SHOPPING 
class Shopping 
{ 
	protected: 
	static Shopping *tail; // class attribute 
	int QTY; 
	char item[21]; 
	Shopping *next;

	Shopping *top;// ????
	public: 
	Shopping(int, char *); // constructor 
	void addItem(Shopping *&); 
	void show(); 
	void clean(); 
	void sort();
}; // Shopping 
#endif


// The driver for Shopping example (linked List). 
#include <fstream> 
#include <iostream> 
#include "shopping.h" 
using namespace std;
int main() 
{ 
	Shopping *head = NULL; 
	void buildList(ifstream &, Shopping *&); 
	ifstream myfile("shoplist.txt"); 
	if (myfile.fail()) { 
	cerr << "Cannot open input file\n"; 
	return 1; 
	} 
	buildList(myfile, head); 
        if (head) head->sort();///Correct?
	if (head) head->show(); 
	if (head) head->clean(); 
        
	myfile.close(); 
	return 0; 
} // main

void buildList(ifstream &infile, Shopping *&h) 
{ 
	char s[21]; 
	int k; 
	Shopping *temp; 
	if (infile.eof()) return; // stop recursive call 
	infile >> k >> s; 
	temp = new Shopping(k, s); 
	temp->addItem(h); 
	buildList(infile, h); // recursive call 
} // buildList

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>