0614111、2班《C语言实验指导书》(7)

2020-05-24 10:45

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 #include #include void makeproblem(); int m,n,res; int main() {

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


0614111、2班《C语言实验指导书》(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:裁员减薪是很常用的降低人工本钱的方法

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: