数据结构学习 之 链表 练习 链表排序功能.(热身练习)

----非常火大.cao!去年的程序到今年竟然运行不成功 ,一个 排序问题搞到现在, ╮(╯▽╰)╭...可能不是当时的最终版本...

本来准备 结合 我的 上一次mysql 操作 练习 将数据 保存到 mysql 数据库 的; 没想到.... 

还没解决....

明天再说! 先洗澡睡觉...吃大亏了今天. ...

 


先介绍下基本功能:

 

      一个用于存储学生基本信息 的 链表,实现在任意位置 添加,删除,修改,的功能,以及排序功能'

学生有是个属性: 学号,姓名,语文分数,数学分数; 

 

     --------------------------------------------------

            1.按下'r'开始读取文件信息!

            2..按下's'开始对其进行排序!

            3.按下'a'开始添加信息!

    4.按下'd'删除当前信息!

    5.按下'e'修改当前信息!

    6.按下'n'显示下一条信息!

    7.按下'p'显示上一条信息!

    8.按下'v'显示全部信息!

    9.按下'w'开始写入所有信息!

     10.按下'q'退出程序!

--------------------------------------------------

printf("\n\twarning:排序`写入操作需要先读文件或是写!\n"); 

排序功能: 

---------------------------------------------

printf("\t\t1.按下数字0开始根据No进行排序!\n");

printf("\t\t2.按下数字1开始根据总分进行排序!\n");

printf("\t\t3.按下数字2开始根据数学进行排序!\n");

printf("\t\t4.按下数字3开始根据语文进行排序!\n");

printf("\t\t5.按下其他数字开始根据学号进行排序!\n");

printf("--\t---------------------------------------------\n"); 

 


文件结构:

 main.c:主函数所在文件

 menu.c:菜单显示功能文件

 Read.c: 读取文件中学生信息功能的文件

 Sort.c:排序功能文件

 Student.c:学生信息操作基本文件;包括链表 添加 . 删除 .修改. 和 基本信息 显示功能

 Write.c : 学生信息写操作

 head.h :头文件

贴下源码(VS C++ 编译器 版本-- linux 环境 版本就不贴了):

  


  1  // student.c 文件
  2 
  3  #include < stdio.h >
  4  #include " head.h "
  5  #include < malloc.h >
  6  #include < stdlib.h >
  7  #include < stdafx.h >
  8  #include  < iostream >
  9 
 10  void  SetTempValue( struct  Student  * s)
 11  {
 12      printf( " Input No:\t " );
 13      scanf( " %s " , & (s -> No));
 14 
 15      printf( " Input Name:\t " );
 16      scanf( " %s " , & (s -> Name));
 17 
 18      printf( " Input Maths:\t " );
 19      scanf( " %d " , & (s -> Maths));
 20 
 21      printf( " Input Chi:\t " );
 22      scanf( " %d " , & (s -> Chi));
 23      
 24  }
 25 
 26 
 27  struct  Student  *  StudentInit( struct  Student  * s)
 28  {
 29      strcpy(s -> No, "" );
 30      strcpy(s -> Name, "" );
 31      s -> Maths = 0 ;
 32      s -> Chi = 0 ;
 33      s -> Next = NULL;
 34      s -> Previous = NULL;
 35      s -> Next = s;
 36       return  s;
 37  }
 38 
 39 
 40 
 41  void   set ( struct  Student  * p, struct  Student  * s)
 42  {
 43      strcpy(p -> No,s -> No);
 44      strcpy(p -> Name,s -> Name);
 45      p -> Maths = s -> Maths;
 46      p -> Chi = s -> Chi;
 47  }
 48 
 49 
 50  void  Add( struct  Student  * s, struct  Student  * HL)
 51  {
 52       struct  Student  * newS  =  ( struct  Student  * )malloc( sizeof ( struct  Student));
 53       set (newS,s);
 54       // exchange shit!
 55      newS -> Next = HL -> Next;
 56      newS -> Previous  =  HL;
 57  // 注意
 58      HL -> Next -> Previous = newS;
 59      
 60      HL -> Next  =  newS;
 61      HL -> Previous = NULL;
 62  }
 63 
 64  void  DeleteThisOne( struct  Student  * s)
 65  {
 66       struct  Student  * L;
 67      L = s -> Previous;
 68      L -> Next = s -> Next;
 69      s -> Next -> Previous = L;
 70      free(s);    
 71  }
 72 
 73  void   Edit( struct  Student  * des, struct  Student  * src)
 74  {
 75      strcpy(des -> No,src -> No);
 76      strcpy(des -> Name,src -> Name);
 77      des -> Maths = src -> Maths;
 78      des -> Chi = src -> Chi;
 79      
 80  }
 81 
 82  void  DisplayThisOne( struct  Student  * s)
 83  {
 84      printf( " \tNo:     %s\n " ,s -> No);
 85      printf( " \tName: %s\n " ,s -> Name);
 86      printf( " \tMaths: %d\n " ,s -> Maths);
 87      printf( " \tChi:     %d\n " ,s -> Chi);
 88      printf( " \tChi:     %d\n " ,s -> Chi + s -> Maths);
 89  }
 90 
 91  void  DisplayAll( struct  Student  * head)
 92  {
 93      head = head -> Next;
 94       while (head -> Previous != NULL)
 95      {
 96          printf( " \tNo:     %s\n " ,head -> No);
 97          printf( " \tName :  %s\n " ,head -> Name);
 98          printf( " \tMaths:  %d\n " ,head -> Maths);
 99          printf( " \tChi:    %d\n " ,head -> Chi);
100          printf( " \tSum:    %d\n " ,head -> Chi + head -> Maths);
101          printf( " -------------------------------------------------------------\n " );
102          head = head -> Next;
103        }
104  }
105 
main.c:↓
ContractedBlock.gif ExpandedBlockStart.gif Code
  1 // main.c : Defines the entry point for the console application.
  2 //
  3 
  4 
  5 
  6 int main(int argc, _TCHAR* argv[])
  7 {
  8     char command;
  9     int i;
 10 
 11     struct Student * Head = NULL;
 12     i=(int)sizeof(struct Student);
 13     Head=(struct Student *)malloc(i);
 14     StudentInit(Head);
 15     
 16     struct Student *temp = NULL;
 17     temp=(struct Student *)malloc(i);
 18     StudentInit(temp);
 19     
 20     struct Student * Cur=NULL;
 21     Cur=Head;
 22 
 23     int Flag=0;
 24     ShowMenu();
 25     while(1){
 26         
 27 
 28         scanf("%c",&command);
 29 
 30         switch(command)
 31         {
 32             case 'r':    
 33                     Head->Next=Head;
 34                     Head->Previous=NULL;
 35                     Cur=Head;
 36                     Read(Head);
 37                     Flag=1;
 38                     ShowMenu();
 39                 break;
 40     
 41             case 'w':    
 42                     if(Flag>0)
 43                     {
 44                         Write(Head);
 45                         Flag=10;
 46                     }
 47                     else
 48                     {
 49                         ShowMenu();
 50                     }
 51                 break;
 52 
 53             case 's':
 54                     if(Flag>0)
 55                     {    
 56                         ShowSubMenu();
 57                         scanf("%d",&i);
 58                         Sort(i,Head);
 59                     }
 60                     else
 61                     {
 62                         ShowMenu();
 63                     }
 64                 break;
 65 
 66             case 'a':    
 67                     SetTempValue(temp);
 68                     Add(temp,Head);
 69                     Flag=2;
 70                 
 71                 break;
 72             
 73         
 74             case 'n':    
 75                     if(Flag>0)
 76                     {
 77                         if(Cur->Next->Previous!=NULL)
 78                         {
 79                             Cur=Cur->Next;
 80                             DisplayThisOne(Cur);
 81                         }
 82                         else
 83                         {
 84                             DisplayThisOne(Cur);
 85 
 86                             printf("Tip:the end\n");
 87                         }
 88                     }
 89                 break;
 90 
 91             case 'p':    
 92                     if(Flag>0)
 93                     {
 94                         if(Cur->Previous->Previous!=NULL)
 95                         {
 96                             Cur=Cur->Previous;
 97                             DisplayThisOne(Cur);
 98                             
 99                         }
100                         else
101                         {
102                             DisplayThisOne(Cur);
103                             printf("Tip:the head of \n");
104                         }
105                     }
106                 break;
107 
108                 case 'v':
109                     if(Flag>0)
110                     {
111                         DisplayAll(Head);
112                     }
113                     ShowMenu();
114                 break;
115             case 'd':
116                 if(Flag>0&&Cur!=Head)
117                 {
118                     DisplayThisOne(Cur);
119                     printf("\nTip:Delete this one /Are you sure?[0-Y  |  1-N]");
120                     //show messagebox "Sure ?" to confirm.
121                     scanf("%d",&i);
122                     if(i==0)
123                     {
124                         struct Student *t;
125                         t=Cur;
126                         Cur=Cur->Previous;
127                         DeleteThisOne(t);
128                         Flag=5;
129                         ShowMenu();
130                         printf("Tip:delete successfully !\n");
131                     }
132                     else
133                     {    ShowMenu();
134                         printf("Tip:delete operation cancelled ! 提示:删除操作已取消!\n");
135                         break;
136                     }
137                 }
138                 else
139                 {
140                     ShowMenu();
141                     printf("\n Tip:cann't access   the current place ! 当前位置不能编辑!\n");
142                 }
143                 break;
144         
145 
146             case 'e':
147                     if(Flag>0&&Cur!=Head)
148                     {
149                         DisplayThisOne(Cur);
150                         printf("\nTip: Edit  this one /Are you sure?[0-Y  |  1-N]");                    
151                         //show messagebox "Sure ?" to confirm.
152                         scanf("%d",&i);
153                         if(i==0)
154                         {
155                             SetTempValue(temp);
156                             DisplayThisOne(temp);
157                             Edit(Cur,temp);
158                             printf("\nTip: replaced by:  已被替换为:\n");
159                             DisplayThisOne(temp);
160                             Flag=3;
161                         }
162                     }
163                     else
164                     {
165                         
166                         ShowMenu();
167                         printf("\n Tip:cann't access the current place 当前位置不能编辑!\n");
168                     }
169                 break;
170             case 'q':
171                     if(Flag>1&&Flag!=10)
172                     {
173                         printf("Tip:Context has been changed ,Do you want to Save [ 0-Y  |  1-N ] ? 数据已经修改是否保存?[0-Y  |  1-N]");
174                         scanf("%d",&i);
175                         if(i==0)
176                         {
177                             Write(Head);
178                             printf("Tip: Saved successfully ! 数据已经保存!\n");
179                             exit(1);
180                         }
181                         else
182                         {
183                             
184                             printf("Tip:quit without Save !数据未保存!\n");
185                             exit(1);
186                         }
187                     }
188                     else
189                     {
190                         printf("正常退出!\n");
191                         exit(0);
192                     }
193                 break;
194         
195             default :
196                 break;
197 
198     }
199 }
200 return 0;
201 }
202 

sort.c:
ContractedBlock.gif ExpandedBlockStart.gif Code
  1 #include<stdio.h>
  2 #include "head.h"
  3 #include <malloc.h>
  4 #include<stdafx.h>
  5 #include <iostream>
  6 void exchange(struct Student *p,struct Student *s)
  7 {
  8     struct Student * temp;
  9     temp=(struct Student *)malloc(sizeof(struct Student));
 10     strcpy(temp->No,p->No);
 11     strcpy(p->No,s->No);
 12     strcpy(s->No,temp->No);
 13 
 14     strcpy(temp->Name,p->Name);
 15     strcpy(p->Name,s->Name);
 16     strcpy(s->Name,temp->Name);
 17 
 18     temp->Maths=p->Maths;
 19     p->Maths=s->Maths;
 20     s->Maths=temp->Maths;
 21     
 22     temp->Chi=p->Chi;
 23     p->Chi=s->Chi;
 24     s->Chi=temp->Chi;
 25 }
 26 
 27 
 28 struct Student* SortByNo(struct Student *s)
 29 {
 30 
 31     struct Student *p;
 32     struct Student *p2;
 33     
 34     for(p=s->Next;p->Next->Previous!=NULL;p=p->Next)
 35     {
 36         for(p2=p->Next;p2->Next!=NULL;p2=p2->Next)
 37         {
 38             if(strcmp(p->No,p2->No)>=0)
 39             {
 40                 exchange(p,p2);
 41             }
 42         }
 43     }
 44 
 45 
 46     return s;
 47 }
 48 
 49 struct Student* SortByMaths(struct Student *s)
 50 {
 51 
 52     struct Student *p;
 53     struct Student *p2;
 54     
 55     for(p=s->Next;p->Next->Previous!=NULL;p=p->Next)
 56     {
 57         for(p2=p->Next;p2->Previous!=NULL;p2=p2->Next)
 58         {
 59             if(p->Maths<p2->Maths)
 60             {
 61                 exchange(p,p2);
 62             }
 63         }
 64     }
 65 
 66     return s;
 67 }
 68 
 69 struct Student* SortByChi(struct Student *s)
 70 {
 71     struct Student *p;
 72     struct Student *p2;
 73     
 74     for(p=s->Next;p->Next->Previous!=NULL;p=p->Next)
 75     {
 76         for(p2=p->Next;p2->Previous!=NULL;p2=p2->Next)
 77         {
 78             if(p->Chi<p2->Chi)
 79             {
 80                 exchange(p,p2);
 81             }
 82         }
 83     }
 84 
 85     return s;
 86 }
 87 
 88 
 89 
 90 struct Student* SortBySum(struct Student *s)
 91 {
 92     struct Student *p;
 93     struct Student *p2;
 94     
 95     for(p=s->Next;p->Next->Previous!=NULL;p=p->Next)
 96     {
 97         for(p2=p->Next;p2->Previous!=NULL;p2=p2->Next)
 98         {
 99             if((p->Chi+p->Maths)<(p2->Chi+p2->Maths))
100             {
101                 exchange(p,p2);
102             }
103         }
104     }
105 
106     return s;
107 }
108 
109 
110 struct Student * Sort(int i,struct Student *s)
111 {
112     switch(i)
113         {
114     case 0: printf("Tip:Sort by NO!");    return SortByNo(s);
115                 break;
116             case 1:    return SortBySum(s);
117                 break;
118             case 2:    return SortByMaths(s);
119                 break;
120             case 3:    return SortByChi(s);
121                 break;
122             default:    return SortBySum(s);
123                 break;
124         }
125 }
126 

read.c

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 #include<stdio.h>
 2 #include "head.h"
 3 #include<stdafx.h>
 4 #include <malloc.h>
 5 
 6 void Read(struct Student *Head)
 7 {
 8     FILE * fp;
 9     int i=0;
10     
11     struct Student  * newStu;
12     newStu=(struct Student *)malloc(sizeof(struct Student));
13 
14     if((fp=fopen("StudentScore.txt","r"))==NULL)
15     {
16         printf("Can't open File!\n");
17         return  ;
18     }
19     else
20     {
21             i = sizeof(struct Student);
22             while(!feof(fp))
23             {
24                 fread(newStu,i,1,fp);
25                 Add(newStu,Head);
26                 newStu=(struct Student *)malloc(sizeof(struct Student));
27             }
28     }
29     fclose(fp);
30     return  ;
31 }
32 

 


write.c:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 #include<stdio.h>
 2 #include"head.h"
 3 #include "stdafx.h"
 4 #include <iostream>
 5 
 6 void Write(struct Student * s)
 7 {
 8     FILE * fp;
 9     int i=0;
10     if((fp=fopen("StudentScore.txt","w"))==NULL)
11     {
12         printf("Can't open File!\n");
13         return ;
14     }
15     else
16     {
17         i = sizeof(struct Student);
18         struct Student *p;
19         p=s->Next;
20         while(p->Previous!=NULL){            
21             fwrite(p,i,1,fp);
22             p=p->Next;
23             }
24     }
25     fclose(fp);
26     return ;
27 }
28 

 


menu.c

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 #include<stdio.h>
 2 #include<stdafx.h>
 3 
 4 
 5 void ShowMenu()
 6 {
 7     printf("--------------------------------------------------------------\n");
 8     printf("\t1.Press 'r'to get info from a file named StudentScore.txt !\n");
 9     printf("\t2.Press 's' to sort the info by what you like !\n");
10     printf("\t3.Press 'a' to add infomations !\n");
11     printf("\t4.Press 'd' to delete the stu info of the current pointer points to !\n");
12     printf("\t5.Press 'e'to edit the current info !\n");
13     printf("\t6.Press 'n' to show next infomation !\n");
14     printf("\t7.Press 'p' to show the previous info!\n");
15     printf("\t8.Press 'v' to show all the infomations !\n");
16     printf("\t9.Press 'w' to push all the info in the info to disk file @!\n");    
17     printf("\t10.Press 'q' to quit \n");    
18     printf("--------------------------------------------------------------\n");
19     printf("\n\twarning: File should be read or wriitten before sort !\n");
20 }
21 
22 
23 void ShowSubMenu()
24 {
25     printf("--\t--------------------------------------------------------\n");    
26     printf("\t\t1.Press  0  to order by student No !\n");
27     printf("\t\t2.Press  1  to order by student SUM of scores !\n");    
28     printf("\t\t3.Press     2  to order by student Maths score !\n");
29     printf("\t\t4.Press  3  to order by student Chinese score !\n");
30     printf("\t\t5.Press any other to order by student NO !\n");
31     printf("--\t--------------------------------------------------------\n");
32 

head.h

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #include<stdlib.h>
 4 #include <string.h>
 5 #include <sstream>
 6 
 7 
 8 #ifndef _head_h
 9 #define _head_h
10 
11 
12 void ShowMenu();
13 void ShowSubMenu();
14 struct Student{
15     char No[20];
16     char Name[8];
17     int Maths;
18     int Chi;
19     struct Student *Next;
20     struct Student *Previous;
21 };
22 void SetTempValue(struct Student *s);
23 struct Student * StudentInit(struct Student *s);
24 void set(struct Student *p,struct Student *s);
25 void Add(struct Student *s,struct Student *HL);
26 void DeleteThisOne(struct Student *s);
27 void Edit(struct Student *NewS,struct Student *S);
28 void DisplayThisOne(struct Student *s);
29 void DisplayAll(struct Student *head);
30 void exchange(struct Student *p,struct Student *s);
31 struct Student* SortByNo(struct Student *s);
32 struct Student* SortByMaths(struct Student *s);
33 struct Student* SortByChi(struct Student *s);
34 struct Student* SortBySum(struct Student *s);
35 struct Student * Sort(int i,struct Student *s);
36 struct Student * StudentInit(struct Student *s);
37 void Read(struct Student *h);
38 void Write(struct Student *h);
39 
40 
41 
42 

好了代码贴完 收工!

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值