数据实验结构(二)——栈和队列的设计与实现
来源:互联网 发布:怎样在淘宝客服发图片 编辑:程序博客网 时间:2024/05/02 02:45
栈和队列的设计与实现
1.栈的设计与实现
#include<stdio.h>#include<malloc.h>#define MaxSize 50typedef char ElemType;typedef struct Linknode{ElemType data;struct Linknode *next;}listack; //定义栈void InStack(listack *&s){s=(listack *)malloc(sizeof(listack));s->next=NULL;}//初始化栈void DestoryStack(listack *&s){listack *p=s,*q=s->next;while(q!=NULL){free(p);p=q;q=p->next;}free(p);}//销毁栈bool EmptyStack(listack *s){ return (s->next==NULL);}//判断栈是否为空void PushStack(listack *&s,ElemType e){listack *p;p=(listack *)malloc(sizeof(listack));p->data=e;p->next=s->next;s->next=p;}//进栈bool PutStack(listack *&s,ElemType e){listack *p;if(s->next==NULL)return false;p=s->next;e=p->data;s->next=p->next;free(p);return true;}//出栈bool Gettop(listack *&s){ElemType e;if(s->next==NULL)return false;e=s->next->data;printf("%c\n",e);return true;}//取栈顶void StackLength(listack *s){int length=0;while(s->next!=NULL){length++;s->next=s->next->next;}printf("%d\n",length);}//栈的长度void DiStack(listack *s) //输出sq{if(s->next==NULL){printf("这是一个空栈!");printf("\n");}else{do{char ss=s->next->data;printf("%c",ss);s->next=s->next->next;}while(s->next!=NULL);}}void main(){listack *s;char a1='a',b1='b',c1='c',d1='d',e1='e';InStack(s);//1 EmptyStack(s);//2PushStack(s,a1);PushStack(s,b1);PushStack(s,c1);PushStack(s,d1);PushStack(s,e1);//3EmptyStack(s);//4Gettop(s);//e PutStack(s,a1);Gettop(s);//d PutStack(s,b1);Gettop(s);//c PutStack(s,c1);Gettop(s);//b PutStack(s,d1);Gettop(s);//6StackLength(s);//5DiStack(s);EmptyStack(s);//8DestoryStack(s);//9}
2.对列的设计与实现
/****************************顺序环形队列*****************************/ #include<stdio.h>#include<malloc.h>#include <stdlib.h> #include <string.h> #define MaxSize 50typedef char ElemType; typedef struct {ElemType data[MaxSize];int front,rear; } SqQueue; //建立队列 void CreateQueue(SqQueue* &q,ElemType a[],int n) { int i;q->front=0;for(i=1;i<=n;i++)q->data[i]=a[i-1]; q->rear=n; } //初始化队列 void InitQueue(SqQueue* &q) {q=(SqQueue *)malloc(sizeof(SqQueue)); q->front=q->rear=0; } //销毁队列 void ClearQueue(SqQueue* &q) { free(q); } //求队列的长度 void QueueLength(SqQueue *q) {printf("%d\n", q->rear-q->front); } //判断队列是否为空 int QueueEmpty(SqQueue *q) { return(q->front==q->rear); } //元素入队列 int EnQueue(SqQueue* &q,ElemType e) {if((q->rear+1)%MaxSize==q->front) return 0; q->rear=(q->rear+1)%MaxSize;q->data[q->rear]=e;return 1; } //元素出队列 int DeQueue(SqQueue* &q) { ElemType e; if(q->front==q->rear) return 0; q->front=(q->front+1)%MaxSize; e=q->data[q->front]; printf("%c\n",e); return 1; } //显示队列中元素 void DispQueue(SqQueue *q) { int i; for(i=q->front+1;i<=q->rear;i++) printf("%c",q->data[i]); printf("\n"); } //主函数 int main() { SqQueue* q; ElemType a[3]={'a','b','c'};InitQueue(q); QueueEmpty(q);CreateQueue(q, a,3);DeQueue(q);QueueLength(q);ElemType b[3]={'d','e','f'};for(int i=0;i<3;i++){EnQueue(q,b[i]);}QueueLength(q);DispQueue(q); ClearQueue(q); return 0; }
0 0
- 数据实验结构(二)——栈和队列的设计与实现
- 第四次实验——栈和队列(二)
- 实验二 栈和队列的应用
- 实验4:栈和队列的基本操作实现及其应用——循环队列
- 实验4:栈和队列的基本操作实现及其应用——链队列
- 数据结构实验二——栈和队列
- 实验3:栈和队列的基本操作实现及其应用——顺序队列和链队列
- 实验四 栈和队列的定义与实现(未完成)
- 一种日志结构文件系统的设计与实现(二)
- 数据结构实验(一)——顺序表和单链表的设计与实现
- 把我的 C 作业贴出来 实验二 栈和队列 ——2
- 栈和队列——设计一个有getMin功能的栈(二)
- 实验二 栈和队列——顺序栈、顺序队列
- 【数据结构】实验二:栈与队列的应用---表达式求值
- android实验二 图片查看器的设计与实现
- 实验二 预测分析算法的设计与实现
- 实验4:栈和队列的基本操作实现及其应用——进制转换以及实验总结
- 实验三:栈和队列的基本操作实现及其应用——顺序栈
- Android资源篇
- 钢筋连接套筒厂家
- 返回键的监听
- chkconfig命令
- UVA11645:Bits
- 数据实验结构(二)——栈和队列的设计与实现
- java 慎用ArrayList的contains方法,使用HashSet的contains方法代替
- Openfire + Smack 4.1.1 搭建Android聊天平台
- 多继承原理抛砖 - 虚继承的应用场景 - 虚继承能解决的问题有限
- 如果像招聘程序员那样招聘木匠
- Android ViewDragHelper 实现 QQ5.0 侧滑
- androidの监听EditView中的文本TextWatcher
- 位运算
- GRE填空词汇——简单类