C语言程序实例(编译通过含注释)

上传人:大米 文档编号:490045671 上传时间:2023-09-04 格式:DOCX 页数:29 大小:39.31KB
返回 下载 相关 举报
C语言程序实例(编译通过含注释)_第1页
第1页 / 共29页
C语言程序实例(编译通过含注释)_第2页
第2页 / 共29页
C语言程序实例(编译通过含注释)_第3页
第3页 / 共29页
C语言程序实例(编译通过含注释)_第4页
第4页 / 共29页
C语言程序实例(编译通过含注释)_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《C语言程序实例(编译通过含注释)》由会员分享,可在线阅读,更多相关《C语言程序实例(编译通过含注释)(29页珍藏版)》请在金锄头文库上搜索。

1、C语言程序实例-C语言存储汉字程序#include void main()char a2;printf (请先用ctrl+空格键切换成汉字输入法n);prin tf(请输入汉字n);scanf(%s,&a);printf(%s,a);getchar();getchar();(or gets puts C 中)二C语言延时程序#include #include #include void main()Sl eep (3000) ; /延时3秒printf(start!);getch();三动态链表的建立与输出假设链表不超过30个结点#include #include next=p;q=p;/p,

2、q 后移一位printf (请输入 key%d:,i+l);scanf(%d,&p-key);p-nex t=NULL;/最后这个结点为空p=head;/把链表的头指针赋给pwhile(p!=NULL)printf(%dn,p-key);p=p-next;/p 后移一位四. 链表、含4个数的单向静态链表的建立和输出#includestdio.h/含 4个数3,5, 8,11的简单单向静态链表的建立和输出 struct shulong num;struct shu *next; /next指向结构体shu的指针;void main()struct shu a,b,c,d,*head,*p;/把链

3、表中的所有整数都一一定义为结构体,以便使用-or.a. num=3;b. num=5;c. num=8;d. num=11;head=&a;a. next=&b;b. next=&c;c. next=&d;d. next=NULL;p=head;prin tf(该链表=);while(p!=NULL)printf(%d,p-num);p=p-next;if(p!=NULL)/为了最后一个数后面没逗号 printf(,);printf();五. 链队列程序#include stdio.h#include next;while(p!=NULL)printf(%cn,p-elem);p=p-next

4、;/ /*1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /break;case 2:/*进队列*prin tf(请输入要进入的元素:); p=(struct Lq*)malloc(LEN);getchar(); scanf(%c,&p-elem);system(cls);p-next=NULL;Qrear-next=p;Qrear=p;prin tf(进队列成功!);/ / *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1

5、* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /break;case 3: /*出队列*if(Qfront=Qrear)printf (error!队列为空!); p=Qfront-next; Qfront-next=p-next; if(Qrear=p) Qrear=Qfront;prin tf(出队列成功!);/ / *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /break;case 4: flag=0;break;d

6、efault:printf(error!n);printf(n);六. 栈和队列1、试编写算法,在顺序存储结构下实现堆栈的下列运算:(1) initstk(s)。初始化操作,建立一个空栈s;(2) emptystk(s)。判定栈是否为空;(3) pushstk(s)。如果栈s不满,在栈顶插入x;(4) popstk(s)。如果栈s不空,删除栈顶元素,并 返回该元素的值;(5) getstk(s)。如果栈s不空,返回栈顶元素。2、试编写算法,实现链队列的下列操作算法:(1) initlq(q)。初始化操作,建立一个空队列q;(2) emptylq(q)。判定队列q是否为空;(3) enterlq

7、(q)。进队列;(4) deletelq(q)。出队列。1. #include #include #define MAX 5 /定义堆栈最大容量void main()char elemMAX; /栈区int top=-1; /初始化栈顶指针为-1int flag=1;char ch;int i,j;/程序解说prin tf(本程序实现顺序结构的堆栈的操作.n);prin tf(可以进行入栈,出栈,取栈顶元素等操作.n);/while(flag)prin tf(请选择:n);printf(1.显示栈中所有元素n)printf(2.入栈n)printf(3.出栈n);printf(4.取栈顶元素n

8、);printf(5.退出程序n);scanf(%d,&j);system(cls);switch(j)case 1:/*显示所有元素*if(top=-1)prin tf(堆栈为空栈!n);elseprin tf(堆栈所有元素:n);for(i=0;i=top;i+)printf(%c,elemi);printf(n);/ /*1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /break;case 2:if( top=MAX-l)/判断是否栈满prin tf (error!栈已满!)

9、;/*入栈*elseprin tf(请输入要入栈的元素:);scanf( %c,&ch); /输入要入栈的元素system(cls);top+;/栈顶指针top加一elemtop=ch;/入栈prin tf(入栈成功!);/ / *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /break;case 3: /*出栈*if(top=-1)pri ntf (err or!栈已空!);elsetop-;/栈顶指针减一prin tf(出栈成功!);/ / / /break;case 4: /*取得栈顶元素*if(top=-1)prin tf (error!栈为空栈!);elseprin tf(栈顶元素为:c,elem top);/显示栈顶元素/ / *X* *X* *X* *X* *X* *X* *X* *X* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1* *1*/ /b

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号