str.h file:
Code:
#ifndef STR_H
#define STR_H
#include <iostream.h>
#include <string.h>
class Str { //create class 'str'
char s[32]; //define array of 32 characters
public:
Str (char *p=""); //ctor (default)
int operator<(Str &str); //operator
int operator==(Str &str);
friend istream & operator>>(istream &in, Str &str);
friend ostream & operator<<(ostream &out, Str &str);
};
#endif
StudentLinkList.cpp:
Code:
#include "StudentLinkList.h"
#include <iomanip.h>
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include <iomanip.h>
StudentNode::StudentNode (char *p, int grade) :s(p,grade) { next=NULL;}
istream &operator >> (istream &in, StudentNode &s){
in >> s.s;
return in;
}
StudentLinkList::StudentLinkList(){
top=NULL;
}
StudentLinkList::~StudentLinkList(){
while (top!=NULL)
{
PStudentNode p=top;
top=top -> next;
delete p;
}
}
void StudentLinkList::List(){
PStudentNode curr=top;
while (curr!=NULL)
{
cout << curr->s << endl;
curr = curr->next;
}
}
void StudentLinkList::ReadFile(){
ifstream infile;
char fn[64];
cout <<"Enter the file name:";
infile.open (fn,ios::nocreate);
if (infile)
{
while (!infile.eof())
{
PStudentNode p=new StudentNode;
infile >> *p;
Insert (p);
}
infile.close();
}
}
void StudentLinkList::Insert (PStudentNode NewNode){
if (top==NULL) top=NewNode;
else if (NewNode->s.Name() < top->s.Name() )
{
NewNode ->next = top;
top = NewNode;
}
else
{
PStudentNode curr = top, prev;
while (curr !=NULL && curr->Name() < NewNode->s.Name() )
{
prev = curr;
curr = curr->next;
}
prev->next = NewNode;
NewNode -> next = curr;
}
}
void StudentLinkList::Delete (str name){
{
if (top->Name==name)
{
PStudentNode tmp=top;
top = top->next;
delete tmp;
}
else
{
PStudentNode curr=top,prev;
while(curr!=NULL && curr->name != name)
{
prev=curr;
curr=curr -> next;
}
if(curr !=NULL)
{
prev->next = curr->next;
delete curr;
}
}
}
}