C语言程序设计实验指导书
struct student
{int data; struct student *next; };/*结点结构*/ int n;
struct student *creat(void) /*单链表的建立*/
{ struct student *head; struct student *p1, *p2; n=0; p1=p2=(struct student *) malloc(LEN); scanf(\ head=NULL;
while(p1->data!=0) {n=n+1;
if(n==1)head=p1; else p2->next=p1;
p2=p1; p1=(struct student *)malloc (LEN); scanf(\}
p2->next=NULL; return(head); }
void print( struct student *head) /*输出链表*/ { struct student *p;
printf(\ p=head;
if(head!=NULL) do
{ printf(\ p=p->next; }while(p!=NULL); }
struct student *del(struct student *head,int num)/* 删除一个结点*/ { struct student *p1,*p2;
if(head==NULL) {printf(\ p1=head;
while(num!=p1->data && p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->data)
{ if(p1==head) head=p1->next; else p2->next=p1->next; printf(\ n=n-1; }
25
C语言程序设计实验指导书
else printf(\ end: return(head); }
struct student *insert(struct student *head, struct student *stud) /*结点的插入*/
{ struct student *p0, *p1, *p2;
p1=head; /*使p1指向第一个结点*/ p0=stud; /*p0指向要插入的结点*/ if(head==NULL) /*原来的链表是空表*/
{head=p0; p0->next=NULL; } /*使p0指向的结点作为头结点*/ else
{ while((p0->data>p1->data) && (p1->next!=NULL))
{ p2=p1; /*使p2指向刚才p1指向的结点*/ p1=p1->next; } /*p1后移一个结点*/ if(p0->data<=p1->data)
{ if(head==p1) head=p0; /*插到原来第一个结点之前 */ else p2->next=p0; /*插到p2指向的结点之后*/ p0->next = p1; }
else {p1->next=p0; p0->next=NULL; } /*插到最后的结点之后*/ }
n=n+1; /*结点数加1*/ return(head); }
void main( ) /*主函数*/
{ struct student *head, *stu; int del_num;
printf(\
head=creat(); /*建立链表,返回头指针*/ print(head); /*输出全部结点*/ printf(\
scanf(\输入要删除的学号*/ while(del_num !=0)
{ head=del(head,del_num); /*删除后链表的头地址*/ print(head); /*输出全部结点*/ printf(\
scanf(\输入要删除的学号*/ }
printf(\ stu=(struct student *) malloc(LEN);
26
C语言程序设计实验指导书
scanf(\输入要插入的结点*/ while(stu->data!=0)
{ head=insert(head,stu); /*插入新结点,返回地址*/ print(head); /*输出全部结点*/ printf(\ stu=(struct student *) malloc(LEN); scanf(\ } }
27
C语言程序设计实验指导书
小学生测验
#include
int num=10,trylimit=3; int score=0; int i;
for (i=0;i int ans; makeproblem(); while(trylimit>0) { scanf(\ if(ans==res) break; else { printf(\ } trylimit--; } if (trylimit==3) { score+=10; } else if(trylimit==2) 28 C语言程序设计实验指导书 { score+=7; } else if(trylimit==1) { score+=5; } trylimit=3; } if (score>=90) { printf(\ } else if (score>=80) { printf(\ } else if (score>=70) { printf(\ } else if (score>=60) { printf(\ } else { printf(\ } } void makeproblem() { int isAdd; srand(time(0)); isAdd=rand()%2; 29 C语言程序设计实验指导书 if (isAdd) { m=rand()Q; n=rand()%(51-m);; res=m+n; printf(\ } else { m=rand()Q; n=rand()%(m+1); res=m-n; printf(\ } } 30 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库0614111、2班《C语言实验指导书》(7)在线全文阅读。
相关推荐: