数据结构(C语言版)里面的应用是平台VC++,对没有c++基础的人。很难去理解参数传递中使用引用的微妙之处。写程序来也是各种困难。在这个建议所有学习c/c++但是还没有对c++很熟练的学弟学妹们可以考虑下载WIN TC http://www.xdowns.com/soft/38/39/2006/soft_16136.html使用。
下面是基于c些的一个链表程序,基本功能都有。但是没有时间去调试,可能会有bug。见谅拉!
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define MAXSIZE 100
typedef struct info{
long int num;
char name[15];
char gender[5];
int score;
int length;
struct info *pnext;
}NODE,*linklist; /*指定一个结构体变量和指向结构体的指针变量*/
void show() /*界面设计*/
{
textbackground(BLUE);
clrscr();
window(15,5,64,24);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
gotoxy(16,4);
cputs("Students' Info of class 1");
gotoxy(16,8);
cputs("Press and key to continus");
gotoxy(39,18);
cputs("11.05.24");
getch();
window(1,1,80,25);
textbackground(YELLOW);
textcolor(7);
clrscr();
}
void menu_select()
{
printf("********************************************************************************\n");
printf(" please select one of the functions \n");
printf(" 1:insert info \n");
printf(" 2:delete info \n");
printf(" 3:find info \n");
printf(" 4:save info \n");
printf(" 5:exit \n");
printf(" 0:see \n");
printf("********************************************************************************\n");
}
void insertlist(linklist phead) /*添加数据*/
{
int i;
char c1;
NODE *p=phead,*s,*p1;
for(i=0;;i++)
{
while(p->pnext) p=p->pnext;
s=(NODE*)malloc(sizeof(NODE));
printf("please input the info\n");
printf("input number:");
scanf("%ld",&s->num);
printf("input name:");
scanf("%s",s->name);
printf("input gender:");
scanf("%s",s->gender);
printf("input score:");
scanf("%d",&s->score);
s->pnext=NULL;
p->pnext=s;
s->length=1;
printf("\n\t\tcontinue?y/n\n");
if('n'==getche())break;
}
}
void deletelist(linklist phead) /*删除数据*/
{
long int delorder;
NODE *p1=phead,*p2=phead;
printf("\t\twhich student you want to delete?\n");
printf("\t\tplease input the number:\n");
scanf("%ld",&delorder);
while(p2!=NULL)
{
if(p2->num==delorder)
{
if(p2==phead) phead=p2->pnext;
else
p1->pnext=p2->pnext;
printf("\t\tyou have delete the info\n");
free(p2);
getch();
break;
}
p1=p2;
p2=p2->pnext;
}
printf("\t\tthere is no any info\n");
}
void findlist(linklist phead) /*查询数据*/
{
long int st;
NODE *p=phead;
printf("\t\tplease input the student number which you want to find:\n");
scanf("%ld",&st);
while(p->pnext)
{
while(p->num!=st)
{ if(p->pnext==NULL) printf("\t\tyou have input the wrong number\n");
p=p->pnext;
if(p->num==st)
{
printf("\t\there is the info \n");
printf("\t\tthe number is %ld\n",p->num);
printf("\t\tthe name is %s\n",p->name);
printf("\t\tthe gender is %s\n",p->gender);
printf("\t\tthe score is %d\n",p->score);
getch();
break;
}
}
}
}
void listexit() /*退出系统*/
{
printf("good bye\n");
getch();
exit(0);
}
void savelist(linklist phead) /*将数据以文本形式存储*/
{
FILE *fp;
NODE *p1=phead->pnext;
fp=fopen("student.txt","w");
if(fp==NULL)
{
printf("\n\t\tcan not open this file\n");
getch();
exit(0);
}
while(p1)
{
if(p1==NULL) break;
printf("\nnumber=%ld\tname=%s\tgender=%s\tscore=%d\n",p1->num,p1->name,p1->gender,p1->score);
fprintf(fp,"\nnumber=%ld\tname=%s\tgender=%s\tscore=%d\n",p1->num,p1->name,p1->gender,p1->score);
p1=p1->pnext;
}
printf("\nsave off\n");
fclose(fp);
getch();
}
void see(linklist phead) /*查看全部数据*/
{ linklist p1;
for(p1=phead->pnext;;p1=p1->pnext)
{
printf("\nnumber=%ld\tname=%s\tgender=%s\tscore=%d\n",p1->num,p1->name,p1->gender,p1->score);
if(p1->pnext==NULL) break;
}
getch();
}
void main(void)
{ int select;
NODE *phead;
NODE stu[MAXSIZE];
phead=(linklist)malloc(sizeof(NODE));
if(!phead) exit(0);
phead->pnext=NULL;
show();
while(1)
{
menu_select(phead);
again: scanf("%d",&select);
switch(select)
{
case 1:
insertlist(phead);
break;
case 2:
deletelist(phead);
break;
case 3:
findlist(phead);
break;
case 4:
savelist(phead);
break;
case 5:
listexit();
break;
case 0:
see(phead);
break;
default:
printf("\t\tYou have input the wrong number\n");
printf(" \t\tinput the number again \n");
getch();
goto again;
}
}
}
浙公网安备 33010602011771号