Computer Science 261

Exercise set #4

Due:  Tuesday, Oct. 19

Important Note:  Although this exercise will not be picked up until after Oct. 19, it is intended in part as a review exercise for the hour exam scheduled Friday, Oct. 15. Please have this well underway by Thursday, Oct. 14, so that you can ask questions about it in class.

Consider the following code:

 

#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>


const int NumberLen = 6;
const int NameLen = 21;

class List{
public:
//	constructors
	List();
	~List();
	
//	accessors
	void PrintList();
	void PrintPart(char thePNo[]);
	void Insert(char thePNo[], char thePName[], int theAmt);
	void Delete(char thePno[]);
	
private:

	struct Node{
		char PNo[NumberLen];
		char PName[NameLen];
		int Amt;
		Node *link;
		};

	Node *head;
};
	

int main()
	{
	List MyList;
	ifstream infile;
	char aNumber[NumberLen];
	char aName[NameLen];
	int anAmt;

	infile.open("parts.dat");
	infile >> aNumber >> aName >> anAmt;

	while (strcmp(aNumber,"99999") != 0)
	{
		MyList.Insert(aNumber,aName,anAmt);
		infile >> aNumber >> aName >> anAmt;
	}

	MyList.PrintList();
	
	MyList.Delete("15285");
	MyList.Delete("01415");
	MyList.Delete("13384");
	MyList.Delete("15173");
	MyList.Delete("00034");

	MyList.PrintList();

	MyList.Insert("00001","NewPart1",10);
	MyList.Insert("05555","NewPart2",20);
	MyList.Insert("99998","NewPart3",30);

	MyList.PrintList();

	MyList.PrintPart("00001");
	MyList.PrintPart("05555");
	MyList.PrintPart("99998");


	return 0;
	}

 

Copy this code into a file (called LinkedList.cpp), and add the code for the promised member functions. Please be sure that your file is properly named. Note that I have changed the file by adding a sentinel record to it. This note will also (shortly) appear on the web page.

Please let me know of any problems. Many thanks!