SLIDE

SLIDE

SLIDE 01: THE UNIVERSITY EXAMPLE, LINKED LIST

2. THE SOURCE PROGRAM IN C:

#include <stdio.h>
#include <string.h>

class univ
{
	friend class list;
protected:
	char LAST[15], FIRST[15];
	int AGE;
	long SS;
	univ *ptr;
	univ *next;
public:
	univ(char *last, char *first, int age, long ss)
	{
		strcpy(LAST,last);
		strcpy(FIRST,first);
		AGE=age; SS=ss; next=0;
	};

	univ()
	{
		LAST[0]='\0';
		FIRST[0]='\0';
		AGE=0; SS=0; next=0;
	};

	void ln(char *last)   {strcpy(LAST, last); };
	void fn(char *first)  {strcpy(FIRST,first);};
	void ag(int age)      {AGE=age;            };
	void ssno(long ss)    {SS=ss;              };
	void print()
	{
		printf("%s %s\nAGE: %d\nSS#: %ld\n",LAST,FIRST,AGE,SS);
	};
	virtual void insert() {};
};

class student: public univ
{
	friend class list;
private:
	float GPA;
	int LEVEL;
public:
	student(char *last,char *first,int age,long ss,float gpa,int level):
	univ(last,first,age,ss)
	{
		GPA=gpa; LEVEL=level;
	};
	student():univ() {GPA=0.0;LEVEL=0;};
	void av(float gpa) {GPA=gpa;};
	void lv(int level) {LEVEL=level;};
	void print()
	{
		univ::print();
		printf("GPA: %.2f LEVEL: %d\n",GPA,LEVEL);
	};
	void insert()
	{
		ptr=new student(LAST,FIRST,AGE,SS,GPA,LEVEL);
	};
};

class prof: public univ
{
	friend class list;
private:
	float SALARY;
public:
	prof(char *last,char *first,int age,long ss,float salary):
	univ(last,first,age,ss)
	{
		SALARY=salary;
	};
	prof():() {SALARY=0.0;};
	void sa(float salary) {SALARY=salary;};
	void print()
	{
		univ::print();
		printf("SALARY: $%.2f\n",SALARY);
	};
	void insert()
	{
		ptr=new prof(LAST,FIRST,AGE,SS,SALARY);
	};
};

class staff: public univ
{
	friend class list;
private:
	float WAGE;
public:
	staff(char *last,char *first,int age,long ss,float wage):
	univ(last,first,age,ss)
	{
		WAGE=wage;
	};
	staff():() {WAGE=0.0;};
	void wg(float wage) {WAGE=wage;};
	void print()
	{
		univ::print();
		printf("WAGE: $%.2f\n",WAGE);
	};
	void insert()
	{
		ptr=new staff(LAST,FIRST,AGE,SS,WAGE);
	};
};

class list
{
private:
	univ *root;
public:
	list() {root=0;};
	insert_person(univ *n)
	{
		char key[15];
		strcpy(key,n->LAST);
		univ *current=root;
		univ *previous=0;
		while (current!=0 && strcmp(current->LAST,key)<0)
		{
			previous=current;
			current=current->next;
		}
		n->insert();
		n->ptr->next=current;
		if (previous==0)
			root=n->ptr;
		else
			previous->next=n->ptr;
	};
	remove(char *last)
	{
		univ *current=root;
		univ *previous=0;
		while (current!=0 && strcmp(current->LAST,last)!=0)
		{
			previous=current;
			current=current->next;
		}
		if (current!=0 && previous==0)
		{
			root=current->next;
			delete current;
		}
		else if (current!=0 && previous!=0)
		{
			previous->next=current->next;
			delete current;
		}
	};
	void print_list()
	{
		univ *current=root;
		while (current!=0)
		{
			current->print();
			current=current->next;
		}
	};
};

main()
{
	list L;
	student S("Marshall","Law",27,111111111,1.36,4);
	staff F("Work","Do",26,222222222,8.24);
	prof P("Steele","Hard",66,999999999,44444);
	L.insert_person(&F);
	L.insert_person(&P);
	L.insert_person(&S);
	L.print_list();
	L.remove("Garbage");
	L.remove("Marshall");
	L.remove("Work");
	L.print_list();
}