-
-
-
- /*-------------------------------------
- 链队列
- -------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #define ElemType char
- #define MaxSize 100
- typedef struct node //定义节点类型
- {
- ElemType data; //数据域
- struct node *next; //指针域
- }SNode;
- typedef struct
- {
- SNode *front;
- SNode *rear;
- }LQueue;
- void InitQueue(LQueue *&qu)
- {
- qu=(LQueue *)malloc(sizeof(LQueue)); //建立链队头节点
- qu->front=qu->rear=NULL;
- }
- void EnQueue(LQueue *&qu,ElemType x)
- {
- SNode *s;
- s=(SNode *)malloc(sizeof(SNode));
- s->data=x;
- s->next=NULL;
- if(qu->rear==NULL) //原来为空队
- {
- qu->front=qu->rear=s;
- }
- else
- {
- qu->rear->next=s; //将*s插入队尾
- qu->rear=s; //由qu->rear指向
- }
- }
- int DeQueue(LQueue *&qu,ElemType &x)
- {
- SNode *p;
- if(qu->rear==NULL)
- return 0;
- p=qu->front;
- if(qu->front==qu->rear)
- qu->front=qu->rear=NULL;
- else
- qu->front=qu->front->next;
- x=p->data;
- free(p);
- return 1;
- }
- int QueueEmpty(LQueue *qu)
- {
- if(qu->front==NULL)
- return 1;
- else
- return 0;
- }
- void main()
- {
- LQueue *lq;
- InitQueue(lq);
- EnQueue(lq,'A');
- EnQueue(lq,'B');
- EnQueue(lq,'C');
- EnQueue(lq,'D');
- EnQueue(lq,'E');
- char Temp;
- Temp=NULL;
- DeQueue(lq,Temp);
- printf("出队元素为:%c/n",Temp);
- Temp=NULL;
- DeQueue(lq,Temp);
- printf("出队元素为:%c/n",Temp);
- Temp=NULL;
- DeQueue(lq,Temp);
- printf("出队元素为:%c/n",Temp);
- if(QueueEmpty(lq))
- printf("/n链队列为空/n");
- else
- printf("/n链队列中还有元素/n");
- }
- 链栈
- -------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #define ElemType char
- typedef struct node //定义节点类型
- {
- ElemType data; //数据域
- struct node *next; //指针域
- }SNode;
- void InitStack(SNode *&st) //置空栈
- {
- st=NULL;
- }
- void push(SNode *&st,ElemType x) //在栈顶指针是st的链栈中插入一个值为x的节点
- {
- SNode *p;
- p=(SNode *)malloc(sizeof(SNode)); //创建一个节点*p
- p->data=x;
- p->next=NULL;
- if(st==NULL) //原来为空栈
- st=p;
- else
- {
- p->next=st;
- st=p;
- }
- }
- int gettop(SNode *st,ElemType &x) //将栈st的栈顶节点的data域值赋给x
- {
- if(st==NULL) //向下溢出
- return 0; //返回失败
- else
- {
- x=st->data;
- return 1; //返回成功
- }
- }
- int pop(SNode *&st,ElemType &x) //将栈st的栈顶节点的data域值赋给x,然后删除栈顶节点
- {
- SNode *p=st;
- if(st==NULL)
- return 0;
- else
- {
- x=st->data;
- st=st->next;
- free(p);
- return 1;
- }
- }
- int StackEmpty(SNode *st)
- {
- if(st==NULL)
- return 1;
- else
- return 0;
- }
- void main()
- {
- SNode *ss;
- InitStack(ss);
- push(ss,'A');
- push(ss,'B');
- push(ss,'C');
- push(ss,'D');
- push(ss,'E');
- char Temp;
- gettop(ss,Temp);
- printf("栈顶元素:%c/n",Temp);
- Temp=NULL;
- pop(ss,Temp);
- printf("出栈元素:%c/n",Temp);
- gettop(ss,Temp);
- printf("栈顶元素:%c/n",Temp);
- if(StackEmpty(ss))
- printf("/n栈为空/n");
- else
- printf("/n栈中还有元素/n");
- }
-
- 顺序队列
- -----------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #define ElemType char
- #define MaxSize 50
- typedef struct
- {
- ElemType data[MaxSize]; //存放队列元素
- int front,rear; //定义队头指针和队尾指针
- }Queue;
- void InitQueue(Queue &qu)
- {
- qu.rear=qu.front=0;
- }
- int EnQueue(Queue &qu,ElemType x) //入队
- {
- if((qu.rear+1)%MaxSize==qu.front) //队满
- return 0; //报错
- else
- {
- qu.rear=(qu.rear+1)%MaxSize;
- qu.data[qu.rear]=x;
- return 1;
- }
- }
- int DnQueue(Queue &qu,ElemType x) //出队
- {
- if(qu.rear==qu.front) //队空
- return 0; //报错
- else
- {
- qu.front=(qu.front+1)%MaxSize;
- x=qu.data[qu.front];
- return 1;
- }
- }
- int QueueEmpty(Queue qu)
- {
- if(qu.rear==qu.front) //队空
- return 1;
- else
- return 0;
- }
- void main()
- {
- Queue qq;
- InitQueue(qq);
- EnQueue(qq,'A');
- EnQueue(qq,'B');
- EnQueue(qq,'C');
- EnQueue(qq,'D');
- EnQueue(qq,'E');
- if(QueueEmpty(qq))
- printf("/n队列为空/n");
- else
- printf("/n队列中还有元素/n");
- }
-
- 顺序栈
- 特别感谢 诛仙(QQ:16822065)
- ---------------------------------*/
- #include <stdio.h>
- #define MaxSize 100
- typedef char ElemType;
- typedef struct
- {
- ElemType s[MaxSize];
- int top;
- }Stack;
- void InitStack(Stack &st) //置栈空
- {
- st.top = -1;
- }
- int push(Stack &st,ElemType x) //将元素x压入到栈st中
- {
- if(st.top == MaxSize - 1) //栈上溢出
- return 0;
- else //否则栈指针增1,将x赋给栈顶元素
- {
- st.top++;
- st.s[st.top]=x;
- return 1;
- }
- }
- int gettop(Stack st,ElemType &x)//将栈st的栈顶元素赋给x,栈指针不变
- {
- if(st.top==-1) //栈下溢出
- return 0;
- else
- {
- x=st.s[st.top]; //否则将栈顶元素值赋给x
- return 1; //返回成功信息
- }
- }
- int pop(Stack &st,ElemType &x) //将栈st的栈顶元素赋给x,栈指针递减
- {
- if(st.top==-1) //栈下溢出
- return 0;
- else
- {
- x=st.s[st.top]; //否则把栈顶元素赋给x
- st.top--;
- return 1; //返回成功信息
- }
- }
- int StackEmpty(Stack st)
- {
- if(st.top==-1)
- return 1; //如果栈为空返回1
- else
- return 0; //否则返回0
- }
- void main()
- {
- Stack ss;
- InitStack(ss);
- push(ss,'A');
- push(ss,'B');
- push(ss,'C');
- push(ss,'D');
- char Temp;
- gettop(ss,Temp);
- printf("栈顶元素为:%c/n",Temp);
- Temp=NULL;
- pop(ss,Temp);
- printf("出栈元素为:%c/n",Temp);
- gettop(ss,Temp);
- printf("栈顶元素为:%c/n",Temp);
- if(StackEmpty(ss))
- printf("/n栈为空/n");
- else
- printf("/n栈中还有元素/n");
- }