数据结构实验课程----实验二(利用链表实现学生健康系统)

题目:

实现学生健康情况管理的几个操作功能(新建、插入、删除、从文件读取、写入文件和查询、屏幕输出等功能)。健康表中学生的信息有学号、姓名、出生日期、性别、身体状况等。

实验内容:

必做内容:

1.利用单链表来实现(PS:我用了双链表)

2.系统的菜单功能项如下: 1------新建学生健康表 2------向学生健康表插入学生信息 3------在健康表删除学生信息 4------从文件中读取健康表信息 5------向文件写入学生健康表信息 6------在健康表中查询学生信息(按学生学号来进行查找) 7------在屏幕中输出全部学生信息 8-----退出

PS:日后会修改的····

主要参考了《数据结构与算法分析》中STL的代码实现:http://www.cnblogs.com/alan-forever/archive/2012/09/16/2687480.html

  1 #include<iostream>
  2 #include<string>
  3 #include<fstream>
  4 using namespace std;
  5 
  6 struct Student
  7 {
  8     string number;
  9     string name;
 10     string dob;
 11     string sex;
 12     string condition;
 13 
 14     bool operator == ( const Student & x )
 15     {
 16         return number == x.number;
 17     }
 18     //==操作符重载
 19 };
 20 
 21 ostream & operator << ( ostream & os, const Student & x )
 22 {
 23     os << "学号:" << x.number << endl;
 24     os << "姓名: " << x.name  << endl;
 25     os << "出生日期: " << x.dob << endl;
 26     os << "性别:" << x.sex << endl;
 27     os << "身体状况:" << x.condition << endl;
 28     os << endl;
 29     return os;
 30 }
 31 /*重载输出“<<”符号,方便Student结构体的输出*/
 32 
 33 template <typename Object>
 34 class List
 35 {
 36 private:
 37     struct Node
 38     {
 39         Object data;
 40         Node * next;
 41         Node * prev;
 42 
 43         Node ( const Object & d = Object( ), Node *p = NULL, Node *n = NULL ) : data( d ), prev( p ), next( n ) { }
 44     };
 45     //私有内嵌结构体,包括数据,前指针,后指针。
 46 
 47 public:
 48     List( )
 49     {
 50         init( );
 51     }
 52                                                 //List构造函数,调用私有函数init生成一个默认的链表,有头结点,尾结点。
 53     ~List( )
 54     {
 55         clear( );
 56         delete head;
 57         delete tail;
 58     }
 59                                                 //析构函数
 60     void clear( )
 61     {
 62         Node * p = begin( );
 63         Node * q;
 64         while( p != end( ) )
 65         {
 66             q = p;
 67             p = p->next;
 68             delete q;
 69         }
 70         theSize = 0;
 71     }
 72                                                 //将链表里面的元素全部删除。
 73     int size( ) const
 74     {
 75         return theSize;
 76     }
 77                                                 //返回链表的长度。
 78     bool empty( )
 79     {
 80         return size( ) == 0;
 81     }
 82                                                 //判断链表是否为空。
 83     Node * locate( int i )
 84     {
 85         if( i < 0 || i > size( ))
 86             return NULL;
 87         else
 88         {
 89             Node * p = head;
 90             for( int j = 1; j <= i; ++j )
 91             {
 92                 p = p->next;
 93             }
 94             return p;
 95         }
 96     }
 97                                                 //返回第i个结点。
 98     Node * begin( )
 99     {
100         return head->next;
101     }
102                                                 //返回第一个结点。
103     Node * end( )
104     {
105         return tail;
106     }
107                                                 //返回尾结点。
108     Object getLocate( int i )
109     {
110         Node *p = locate( i );
111         return p->data;
112     }
113                                                 //返回第i个结点储存的元素。
114     void push_front( const Object & x )
115     {
116         insert( begin( ), x );
117     }
118                                                 //头插入。
119     void push_back( const Object & x )
120     {
121         insert( end( ), x );
122     }
123                                                 //尾插入。
124     void insert( Node * p, const Object & x )
125     {
126         p->prev = p->prev->next = new Node( x, p->prev, p );
127         theSize++;
128     }
129                                                 //在p结点前插入新的结点,其元素为x。
130     int search( const Object & x )
131     {
132         int i;
133         Node * p = begin( );
134         for( i = 1; i <= size( ); ++i )
135         {
136             if( p->data == x )
137             {
138                 break;
139             }
140             p = p->next;
141         }
142         /*if( i <= size( ))
143         {
144             cout << "该学号的学生的信息存在健康表!" << endl;
145         }
146         else
147             cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;*/
148         if( i <= size( ) )
149             return i;
150         else
151             return -1;
152     }
153                                                 //查找元素x在链表的位置,如果不存在,返回-1
154     void remove( int i )
155     {
156         Node * p = locate( i );
157         p->prev->next = p->next;
158         p->next->prev = p->prev;
159         delete p;
160         theSize--;
161     }
162                                                 //删除第i个结点。
163     void output( )
164     {
165         Node * p = begin( );
166         for( int i = 1; i <= size( ); ++i)
167         {
168             cout << p->data;
169             p = p->next;
170         }
171         cout << endl;
172     }
173                                                 //将链表依次输出。
174 private:
175     int theSize;                                //链表的长度。
176     Node * head;                                //头结点。
177     Node * tail;                                //尾结点。
178 
179     void init( )
180     {
181         theSize = 0;
182         head = new Node;
183         tail = new Node;
184         head->next = tail;
185         tail->prev = head;
186     }                                           //生成默认链表。
187 };
188 
189 int main( )
190 {
191     bool flag = false;
192     bool flag1 = false;
193     List<Student>  list;
194     cout << "---------------------------欢迎使用学生健康管理系统---------------------------" << endl;
195     cout << "健康表已经自动建立!" << endl;
196     int m, m1;
197     while( 1 )
198     {
199         cout << "请选择需要的操作!" << endl;
200         cout << "1:插入学生信息。 2:删除学生信息。 3:从文件读入信息。" << endl;
201         cout << "4:向文件写入信息。 5:查询。 6:输出全部信息。其他:退出。" << endl;
202         cout << endl;
203 
204         cin >> m;
205 
206         if( m == 1 )
207         {
208             int num;
209             Student x;
210             cout << "请选择学生信息插入的方式:" << endl;
211             cout << "1:头插入。 2:尾插入。" << endl;
212             cin >> m1;
213             cout << "请输入插入学生信息的数目:" << endl;
214             cin >> num;
215             if( m1 == 1 )
216             {
217                 for( int i = 1; i <= num; ++i)
218                 {
219                     cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
220                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
221                     list.push_front( x );
222                 }
223                 cout << "信息已经插入完毕!" << endl;
224             }
225             else
226             {
227                 for( int i = 1; i <= num; ++i)
228                 {
229                     cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
230                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
231                     list.push_back( x );
232                 }
233                 cout << "信息已经插入完毕!" << endl;
234             }
235             if( flag1 ==  false )
236             flag1 = true;
237             cout << endl;
238         }
239 
240         else if( m == 2 )
241         {
242             int i;
243             Student y;
244             cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
245             cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;
246             i = list.search( y );
247             if( i > -1 && i <= list.size( ) )
248             {
249                 list.remove( i );
250                 cout << "删除学生及其信息成功!" << endl;
251             }
252             else
253                 cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
254             cout << endl;
255         }
256 
257         else if( m == 3 )
258         {
259             if( !flag )
260                 cout << "你还没有向文件写入学生健康信息!请先写入学生健康信息后再读取" << endl;
261             else
262             {
263                 cout << "--------------------读取开始!--------------------" << endl;
264                 ifstream read( "list.txt" );
265                 char s;
266                 while( read.get( s ) )
267                     cout << s;
268                 cout << "--------------------读取结束!--------------------" << endl;
269                 read.close( );
270             }
271             cout << endl;
272         }
273 
274         else if( m == 4 )
275         {
276             string select;
277             cout << "是否已经将需要记录的学生全部记录?(该操作是依次将健康表里面的全部信息写入文件里)" << endl;
278             cout << "如果选择是就输入“Y”;否则输入“N”,继续将学生健康信息插入健康表里。" << endl;
279             cin >> select;
280 
281             if( select == "Y" )
282             {
283                 ofstream write( "list.txt" );
284                 for( int i = 1; i <= list.size( ); ++i )
285                 {
286                     write << list.getLocate( i );
287                 }
288                 flag = true;
289                 write.close( );
290             }
291             else
292             {
293                 cout << "请继续插入或删除操作!" << endl;
294             }
295             cout << endl;
296         }
297 
298         else if( m == 5 )
299         {
300             int i;
301             Student z;
302             string num;
303             bool flag2 = false;
304             cout << "请输入你想查询的学生的学号!" << endl;
305             cin >> num;
306             for( i = 1; i <= list.size( ); ++i)
307             {
308                 z = list.getLocate( i ) ;
309                 if( z.number == num )
310                 {
311                     flag2 = true;
312                     break;
313                 }
314             }
315             if( flag2 )
316                 cout << "该学号的学生的信息存在健康表!" << endl;
317             else
318                 cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
319 
320             cout << endl;
321         }
322 
323         else if( m == 6 )
324         {
325             if( flag1 )
326             {
327                 cout << "输出健康表中全部学生的信息!" << endl;
328                 list.output( );
329             }
330             else
331             {
332                 cout << "健康表为空,请先插入学生信息!" << endl;
333             }
334             cout << endl;
335         }
336 
337         else
338         {
339             cout << "---------------------------谢谢使用学生健康管理系统---------------------------" << endl;
340             break;
341         }
342     }
343 }
  1 #include<iostream>
  2 #include<string>
  3 #include<fstream>
  4 using namespace std;
  5 
  6 struct Student
  7 {
  8     string number;
  9     string name;
 10     string dob;
 11     string sex;
 12     string condition;
 13 
 14     bool operator == ( const Student & x )
 15     {
 16         return number == x.number;
 17     }
 18     //==操作符重载
 19 };
 20 
 21 ostream & operator << ( ostream & os, const Student & x )
 22 {
 23     os << "学号:" << x.number << endl;
 24     os << "姓名: " << x.name  << endl;
 25     os << "出生日期: " << x.dob << endl;
 26     os << "性别:" << x.sex << endl;
 27     os << "身体状况:" << x.condition << endl;
 28     os << endl;
 29     return os;
 30 }
 31 /*重载输出“<<”符号,方便Student结构体的输出*/
 32 
 33 template <typename Object>
 34 class List
 35 {
 36 private:
 37     struct Node
 38     {
 39         Object data;
 40         Node * next;
 41         Node * prev;
 42 
 43         Node ( const Object & d = Object( ), Node *p = NULL, Node *n = NULL ) : data( d ), prev( p ), next( n ) { }
 44     };
 45     //私有内嵌结构体,包括数据,前指针,后指针。
 46 
 47 public:
 48     List( )
 49     {
 50         init( );
 51     }
 52                                                 //List构造函数,调用私有函数init生成一个默认的链表,有头结点,尾结点。
 53     ~List( )
 54     {
 55         clear( );
 56         delete head;
 57         delete tail;
 58     }
 59                                                 //析构函数
 60     void clear( )
 61     {
 62         Node * p = begin( );
 63         Node * q;
 64         while( p != end( ) )
 65         {
 66             q = p;
 67             p = p->next;
 68             delete q;
 69         }
 70         theSize = 0;
 71     }
 72                                                 //将链表里面的元素全部删除。
 73     int size( ) const
 74     {
 75         return theSize;
 76     }
 77                                                 //返回链表的长度。
 78     bool empty( )
 79     {
 80         return size( ) == 0;
 81     }
 82                                                 //判断链表是否为空。
 83     Node * locate( int i )
 84     {
 85         if( i < 0 || i > size( ))
 86             return NULL;
 87         else
 88         {
 89             Node * p = head;
 90             for( int j = 1; j <= i; ++j )
 91             {
 92                 p = p->next;
 93             }
 94             return p;
 95         }
 96     }
 97                                                 //返回第i个结点。
 98     Node * begin( )
 99     {
100         return head->next;
101     }
102                                                 //返回第一个结点。
103     Node * end( )
104     {
105         return tail;
106     }
107                                                 //返回尾结点。
108     Object getLocate( int i )
109     {
110         Node *p = locate( i );
111         return p->data;
112     }
113                                                 //返回第i个结点储存的元素。
114     void push_front( const Object & x )
115     {
116         insert( begin( ), x );
117     }
118                                                 //头插入。
119     void push_back( const Object & x )
120     {
121         insert( end( ), x );
122     }
123                                                 //尾插入。
124     void insert( Node * p, const Object & x )
125     {
126         p->prev = p->prev->next = new Node( x, p->prev, p );
127         theSize++;
128     }
129                                                 //在p结点前插入新的结点,其元素为x。
130     int search( const Object & x )
131     {
132         int i;
133         Node * p = begin( );
134         for( i = 1; i <= size( ); ++i )
135         {
136             if( p->data == x )
137             {
138                 break;
139             }
140             p = p->next;
141         }
142         /*if( i <= size( ))
143         {
144             cout << "该学号的学生的信息存在健康表!" << endl;
145         }
146         else
147             cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;*/
148         if( i <= size( ) )
149             return i;
150         else
151             return -1;
152     }
153                                                 //查找元素x在链表的位置,如果不存在,返回-1
154     void remove( int i )
155     {
156         Node * p = locate( i );
157         p->prev->next = p->next;
158         p->next->prev = p->prev;
159         delete p;
160         theSize--;
161     }
162                                                 //删除第i个结点。
163     void output( )
164     {
165         Node * p = begin( );
166         for( int i = 1; i <= size( ); ++i)
167         {
168             cout << p->data;
169             p = p->next;
170         }
171         cout << endl;
172     }
173                                                 //将链表依次输出。
174 private:
175     int theSize;                                //链表的长度。
176     Node * head;                                //头结点。
177     Node * tail;                                //尾结点。
178 
179     void init( )
180     {
181         theSize = 0;
182         head = new Node;
183         tail = new Node;
184         head->next = tail;
185         tail->prev = head;
186     }                                           //生成默认链表。
187 };
188 
189 int main( )
190 {
191     bool flag = false;
192     bool flag1 = false;
193     List<Student>  list;
194     cout << "---------------------------欢迎使用学生健康管理系统---------------------------" << endl;
195     cout << "健康表已经自动建立!" << endl;
196     int m, m1;
197     while( 1 )
198     {
199         cout << "请选择需要的操作!" << endl;
200         cout << "1:插入学生信息。 2:删除学生信息。 3:从文件读入信息。" << endl;
201         cout << "4:向文件写入信息。 5:查询。 6:输出全部信息。其他:退出。" << endl;
202         cout << endl;
203 
204         cin >> m;
205 
206         if( m == 1 )
207         {
208             int num;
209             Student x;
210             cout << "请选择学生信息插入的方式:" << endl;
211             cout << "1:头插入。 2:尾插入。" << endl;
212             cin >> m1;
213             cout << "请输入插入学生信息的数目:" << endl;
214             cin >> num;
215             if( m1 == 1 )
216             {
217                 for( int i = 1; i <= num; ++i)
218                 {
219                     cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
220                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
221                     list.push_front( x );
222                 }
223                 cout << "信息已经插入完毕!" << endl;
224             }
225             else
226             {
227                 for( int i = 1; i <= num; ++i)
228                 {
229                     cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
230                     cin >> x.number >> x.name >> x.dob >> x.sex >> x.condition;
231                     list.push_back( x );
232                 }
233                 cout << "信息已经插入完毕!" << endl;
234             }
235             if( flag1 ==  false )
236             flag1 = true;
237             cout << endl;
238         }
239 
240         else if( m == 2 )
241         {
242             int i;
243             Student y;
244             cout << "请依次输入学生的学号、姓名、出生年月日、性别、身体情况!" << endl;
245             cin >> y.number >> y.name >> y.dob >> y.sex >> y.condition;
246             i = list.search( y );
247             if( i > -1 && i <= list.size( ) )
248             {
249                 list.remove( i );
250                 cout << "删除学生及其信息成功!" << endl;
251             }
252             else
253                 cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
254             cout << endl;
255         }
256 
257         else if( m == 3 )
258         {
259             if( !flag )
260                 cout << "你还没有向文件写入学生健康信息!请先写入学生健康信息后再读取" << endl;
261             else
262             {
263                 cout << "--------------------读取开始!--------------------" << endl;
264                 ifstream read( "list.txt" );
265                 char s;
266                 while( read.get( s ) )
267                     cout << s;
268                 cout << "--------------------读取结束!--------------------" << endl;
269                 read.close( );
270             }
271             cout << endl;
272         }
273 
274         else if( m == 4 )
275         {
276             string select;
277             cout << "是否已经将需要记录的学生全部记录?(该操作是依次将健康表里面的全部信息写入文件里)" << endl;
278             cout << "如果选择是就输入“Y”;否则输入“N”,继续将学生健康信息插入健康表里。" << endl;
279             cin >> select;
280 
281             if( select == "Y" )
282             {
283                 ofstream write( "list.txt" );
284                 for( int i = 1; i <= list.size( ); ++i )
285                 {
286                     write << list.getLocate( i );
287                 }
288                 flag = true;
289                 write.close( );
290             }
291             else
292             {
293                 cout << "请继续插入或删除操作!" << endl;
294             }
295             cout << endl;
296         }
297 
298         else if( m == 5 )
299         {
300             int i;
301             Student z;
302             string num;
303             bool flag2 = false;
304             cout << "请输入你想查询的学生的学号!" << endl;
305             cin >> num;
306             for( i = 1; i <= list.size( ); ++i)
307             {
308                 z = list.getLocate( i ) ;
309                 if( z.number == num )
310                 {
311                     flag2 = true;
312                     break;
313                 }
314             }
315             if( flag2 )
316                 cout << "该学号的学生的信息存在健康表!" << endl;
317             else
318                 cout << "对不起,这个健康表不存在你要查找的学生及其信息,请先查看全部学生的信息!谢谢!!" << endl;
319 
320             cout << endl;
321         }
322 
323         else if( m == 6 )
324         {
325             if( flag1 )
326             {
327                 cout << "输出健康表中全部学生的信息!" << endl;
328                 list.output( );
329             }
330             else
331             {
332                 cout << "健康表为空,请先插入学生信息!" << endl;
333             }
334             cout << endl;
335         }
336 
337         else
338         {
339             cout << "---------------------------谢谢使用学生健康管理系统---------------------------" << endl;
340             break;
341         }
342     }
343 }

转载于:https://www.cnblogs.com/alan-forever/archive/2012/10/22/2734587.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值