近日已經臨近考試了,想起啊我還有一個數據結構關于鏈表的實驗未作,昨天折騰了一晚上,但是有個問題還是未解決。
其問題就是:
一個學生的信息:
1、姓名
2、學號
3、性別
4、年齡
用一個鏈表將其連接起來,從外界輸入一個年齡,將與該年齡一樣的學生全從鏈表中刪除
分析:對于該實驗給出一個
ADT student
操作對象:其基本信息(私有成員變量)
基本操作:
student();//構造默認參數學生
student(char *name,char *sex,int age);//構造指定參數的學生
~student()://刪除學生
display();//顯示學生信息
student &operator=(student &s);//重載=用于后面鏈表的賦值
對于鏈表的結構
ADT Link
操作對象:學生Student
Link()//構造空表
Delete()//刪除一個元素
Add(Student &s)//向鏈表中添加s
Display()//顯示鏈表
~Link();//釋放鏈表
其代碼如下:
1
#include<iostream>
2
#include<string>
3
using namespace std;
4
class Link;
5
/**//*
6
**對于學號的問題還在研究中,由于拷貝函數、構造函數用的比較雜,沒有實現每次加1,但是鏈表功能還是實現了
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(){//構造空鏈表
80
pHead=NULL;;
81
pTail=NULL;
82
pivot=NULL;
83
}
84
Link::~Link(){//釋放內存
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){//向鏈表中加如學生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(){//顯示鏈表中學生信息
108
pivot=pHead;
109
while(pivot){
110
pivot->display();
111
pivot=pivot->next;
112
}
113
if(pHead)//非空,每次顯示一條鏈表畫下劃線
114
cout<<"-------------------"<<endl;
115
}
116
void Link::Delete(int age){//刪除鏈表中所有年齡為age的學生
117
int yes=0;//記錄是否有age的學生
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記錄其上一個元素
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<<"沒有"<<age<<"歲的學生"<<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
}
上述代碼已經實現了基本功能,只是學號問題還未解決,由于在鏈表中對Student進行操作,要構造臨時學生,但是怎么就沒有及時釋放,導致學號問題
posted on 2011-01-08 15:12
あ維wêiセ 閱讀(1953)
評論(5) 編輯 收藏 引用 所屬分類:
C++