1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// linked list
#include<string>
#include<iostream>
using namespace std;
class student{
private:
string name;
int grade;
student* next;
public:
//constructor and destructor and copy
student(){
name='\0';
grade=0;
next=NULL;
}
student(string name_, int grade_){
name=name_;
grade=grade_;
next=NULL;
}
//destruc
~student(){
name='\0';
grade=0;
next=NULL;
}
//copy and overloading
student(student& sv){
this->name= sv.name;
this->grade=sv.grade;
this->next=sv.next;//(*this)=sv;
}
student*& getnext(){
return next;
}
string& getname(){
return name;
}
int& getgrade(){
return grade;
}
student& operator = (student& sv2 );
friend bool operator ==(student& sv1, student& sv2);
friend double operator +(student& sv1, student& sv2);
friend istream& operator >>(istream& input, student& sv);
friend ostream& operator <<(ostream& output, student& sv);
};
bool operator ==(student& sv1, student& sv2){
if(sv1.grade!= sv2.grade) return false;
else return true;
}
double operator +(student& sv1, student& sv2){
double temp;
temp=sv1.grade+sv2.grade;
return temp;
}
istream& operator >>(istream& input, student& sv){
cout<<"name: ";
getline(input, sv.name);
cout<<"grade: ";
input>>sv.grade;
sv.next= NULL;
}
ostream& operator <<(ostream& output, student& sv){
cout<<"name: "<<sv.name<<endl<<"grade: "<<sv.grade<<endl;
}
class linklist{
private:
student *first, *last;
public:
student* getfirst(){
return first;
}
linklist();
~linklist();
void clear(){
student* temp=first;
while(temp!=NULL){
first=first->getnext();
delete temp;
temp=first;
//temp=first;
//first=first->getnext();
//delete temp;
}
if(first==NULL) cout<<"empty 1";
first=last=NULL;
}
void remove();
void sort();
void search();
//nhap du lieu, toan tu viet trong
friend istream& operator >>(istream& input, linklist& ll);
friend ostream& operator <<(ostream& output, linklist& ll);
};
linklist::linklist(){
first=last=NULL;
}
linklist::~linklist(){
clear();
}
void linklist::remove(){
string name;
cout<<"which's name you want to remove : ";
cin>>name;
if(name==this->first->getname()){
student* temp;
temp=this->first;
this->first=this->first->getnext();
delete temp;
}
else{
student *pre, *after, *cur;
for(pre=this->first;pre->getnext()!=NULL; pre=pre->getnext()){
cur=pre->getnext();
after=cur->getnext();
if(name==cur->getname()){
pre->getnext()=after;
delete cur;
}
if(pre->getnext()==NULL) break;
}
if(after==NULL){
this->last=pre;
}
}
}
istream& operator >>(istream& input, linklist& ll){
char yorn;
while(1){
cout<<"fill in the information(y/n): ";
input>>yorn;
input.ignore(1);
if(yorn!= 'y'&& yorn !='Y') break;
student *temp= new student;
input>>(*temp);
if(ll.first==NULL){
ll.first= ll.last= temp;
}
else {
ll.last->getnext()= temp;
ll.last=temp;
}
}
ll.last->getnext()=NULL;
return input;
}
ostream& operator <<(ostream& output, linklist& ll){
student *cur;
cur=ll.first;
if(ll.first==NULL) cout<<"empty 2";
else{
for(cur= ll.first; cur!=NULL; cur=cur->getnext()){
output<<cur->getname();
output<<" "<<cur->getgrade();
cout<<endl;
}
}
return output;
}
int main(){
linklist *ll= new linklist;
cin>>(*ll);
ll->remove();
cout<<(*ll);
cin>>(*ll);
cout<<(*ll);
delete ll;
if(ll->getfirst()==NULL) cout<<"empty 3";
cout<<(*ll);
return 0;
}
|