近日已經(jīng)臨近考試了,想起啊我還有一個(gè)數(shù)據(jù)結(jié)構(gòu)關(guān)于鏈表的實(shí)驗(yàn)未作,昨天折騰了一晚上,但是有個(gè)問(wèn)題還是未解決。
其問(wèn)題就是:
一個(gè)學(xué)生的信息:
1、姓名
2、學(xué)號(hào)
3、性別
4、年齡
用一個(gè)鏈表將其連接起來(lái),從外界輸入一個(gè)年齡,將與該年齡一樣的學(xué)生全從鏈表中刪除
分析:對(duì)于該實(shí)驗(yàn)給出一個(gè)
ADT student
操作對(duì)象:其基本信息(私有成員變量)
基本操作:
student();//構(gòu)造默認(rèn)參數(shù)學(xué)生
student(char *name,char *sex,int age);//構(gòu)造指定參數(shù)的學(xué)生
~student()://刪除學(xué)生
display();//顯示學(xué)生信息
student &operator=(student &s);//重載=用于后面鏈表的賦值
對(duì)于鏈表的結(jié)構(gòu)
ADT Link
操作對(duì)象:學(xué)生Student
Link()//構(gòu)造空表
Delete()//刪除一個(gè)元素
Add(Student &s)//向鏈表中添加s
Display()//顯示鏈表
~Link();//釋放鏈表
其代碼如下:
1
#include<iostream>
2
#include<string>
3
using namespace std;
4
class Link;
5
/**//*
6
**對(duì)于學(xué)號(hào)的問(wèn)題還在研究中,由于拷貝函數(shù)、構(gòu)造函數(shù)用的比較雜,沒(méi)有實(shí)現(xiàn)每次加1,但是鏈表功能還是實(shí)現(xiàn)了
7
*/
8
class Student{
9
friend class Link;
10
public:
11
Student();
12
Student(Student &);
13
Student(char *name,char* sex,int age);
14
void display();
15
Student &operator=(Student &s);
16
~Student();
17
private:
18
char *Name;
19
int age;
20
char *Sex;
21
int no;
22
Student *next;
23
int static Stu_no;
24
};
25
int Student::Stu_no=2009000;
26
Student::Student(){
27
no=Stu_no++;
28
Name=new char[2];
29
strcpy(Name,"X");
30
Sex=new char[4];
31
strcpy(Sex,"Boy");
32
age=20;
33
}
34
Student::Student(char *name,char* sex,int age){
35
no=Stu_no++;
36
this->age=age;
37
Name=new char[strlen(name)+1];
38
strcpy(Name,name);
39
Sex=new char[strlen(sex)+1];
40
strcpy(Sex,sex);
41
}
42
Student::Student(Student &s){
43
no=Stu_no++;
44
this->age=s.age;
45
Name=new char[strlen(s.Name)+1];
46
strcpy(Name,s.Name);
47
Sex=new char[strlen(s.Sex)+1];
48
strcpy(Sex,s.Sex);
49
next=new Student;
50
}
51
Student &Student::operator =(Student &s){
52
this->age=s.age;
53
Name=new char[strlen(s.Name)+1];
54
strcpy(Name,s.Name);
55
Sex=new char[strlen(s.Sex)+1];
56
strcpy(Sex,s.Sex);
57
return *this;
58
}
59
Student::~Student(){
60
delete []Name;
61
delete []Sex;
62
Stu_no--;
63
}
64
void Student::display(){
65
cout<<Name<<" "<<no<<" "<<Sex<<" "<<age<<endl;
66
}
67
class Link{
68
public:
69
Link();
70
void Delete(int);
71
void Add(Student& s);
72
void Display();
73
~Link();
74
private:
75
Student *pHead;
76
Student *pTail;
77
Student *pivot;
78
};
79
Link::Link(){//構(gòu)造空鏈表
80
pHead=NULL;;
81
pTail=NULL;
82
pivot=NULL;
83
}
84
Link::~Link(){//釋放內(nèi)存
85
pivot=pHead;
86
Student *p;
87
while(pivot){
88
p=pivot;
89
pivot=pivot->next;
90
delete p;
91
}
92
}
93
void Link::Add(Student &s){//向鏈表中加如學(xué)生s
94
if(pHead==NULL){
95
pHead = new Student(s);
96
pTail=pHead;
97
pTail->next=NULL;
98
}
99
else{
100
Student *st=new Student(s);
101
pTail->next=st;
102
pTail=st;
103
pTail->next=NULL;
104
}
105
106
}
107
void Link::Display(){//顯示鏈表中學(xué)生信息
108
pivot=pHead;
109
while(pivot){
110
pivot->display();
111
pivot=pivot->next;
112
}
113
if(pHead)//非空,每次顯示一條鏈表畫(huà)下劃線(xiàn)
114
cout<<"-------------------"<<endl;
115
}
116
void Link::Delete(int age){//刪除鏈表中所有年齡為age的學(xué)生
117
int yes=0;//記錄是否有age的學(xué)生
118
Student *p=pHead,*q;
119
if(p&&p->age==age){//如果鏈表首為age刪除
120
do{
121
cout<<"刪去了";
122
pHead->display();
123
yes=1;
124
pHead=p->next;
125
cout<<"-------------------"<<endl;
126
delete p;
127
p=pHead;
128
}while(p&&p->age==age);
129
}
130
while(p){//其他地方
131
q=p->next;//q為刪除的元素,p記錄其上一個(gè)元素
132
if(q&&q->age==age){
133
p->next=q->next;
134
cout<<"刪去了";
135
q->display();
136
yes=1;
137
delete q;
138
cout<<"-------------------"<<endl;
139
}
140
else if(!q&&yes==0){
141
cout<<"沒(méi)有"<<age<<"歲的學(xué)生"<<endl;
142
cout<<"-------------------"<<endl;
143
return;
144
}
145
else
146
p=p->next;
147
}
148
}
149
void main(){
150
Student s1("X","Boy",22);
151
Student s2("Y","Boy",20);
152
Student s3("Z","Boy",21);
153
Student s4("U","Girl",22);
154
Link l;
155
l.Add(s1);
156
l.Add(s2);
157
l.Add(s3);
158
l.Add(s4);
159
l.Display();
160
l.Delete(21);
161
l.Display();
162
}
上述代碼已經(jīng)實(shí)現(xiàn)了基本功能,只是學(xué)號(hào)問(wèn)題還未解決,由于在鏈表中對(duì)Student進(jìn)行操作,要構(gòu)造臨時(shí)學(xué)生,但是怎么就沒(méi)有及時(shí)釋放,導(dǎo)致學(xué)號(hào)問(wèn)題
posted on 2011-01-08 15:12
あ維wêiセ 閱讀(1962)
評(píng)論(5) 編輯 收藏 引用 所屬分類(lèi):
C++