1.栈的建立与入栈出栈操作的实现过程
#include <stdio.h>
#define MaxSize 50
//建立一个顺序栈
typedef int Elemtype;
typedef struct SqStack
{
Elemtype data[MaxSize];
int top;
}SqStack;//顺序栈的结构体声明
void InitSqStack(SqStack &S) {
S.top = -1;
}//初始化顺序栈
bool StackEmpty(SqStack S) {
if (S.top == -1) {
printf("栈不空");
return true;
}
else
{
printf("栈为空");
return false;
}
}//判断栈是否为空
bool Push(SqStack& S,Elemtype &x) {
if (S.top == MaxSize-1) {
printf("栈满");
return false;
}
else
{
S.data[++S.top] = x;
return true;
}
}
bool Pop(SqStack& S, Elemtype& x) {
if (S.top == -1) {
printf("栈为空");
return false;
}
x = S.data[S.top--];
return true;
}
bool Gettop(SqStack S) {
if (S.top == -1) {
printf("栈为空");
return false;
}
printf("栈顶元素为:%d", S.data[S.top]);
}
int main() {
int x;
SqStack S;
InitSqStack(S);
scanf_s("%d", &x);
while (x!=-1)
{
Push(S, x);
scanf_s("%d", &x);
}
Gettop(S);
}
2.队列的基本操作
-
循环队列的代码实现过程
#include "stdio.h"
#define MaxSize 10
typedef int ElemType;
typedef struct SqQueue {//建立一个循环队列
ElemType data[MaxSize];//存储MaxSize-1个元素
int front, rear;
};
void InitQueue(SqQueue &Q) {
Q.front = Q.rear = 0;
}//初始化队列
bool IsEmpty(SqQueue Q) {
return Q.rear == Q.front;
}
//循环队列入队
bool EnQueue(SqQueue& Q, ElemType &x) {
if ((Q.rear + 1) % MaxSize == Q.front) {
return false;
}//队满
Q.data[Q.rear] = x;//放入元素
Q.rear = (Q.rear + 1) % MaxSize;//改变队尾标记
return true;
}
bool DeQueue(SqQueue &Q, ElemType &x) {
if (Q.rear == Q.front) {//判断栈是否为空
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
printf("出队的元素为:%d\n", x);
return true;
}
int main() {
SqQueue Q;
InitQueue(Q);
bool ret;
int x;
ret = IsEmpty(Q);
if (ret) {
printf("SeQueue is Empty");
}
else
{
printf("SeQueue is not Empty");
}
while (scanf_s("%d",&x))
{
if (x==-1)
{
break;
}
EnQueue(Q, x);
}
if (ret) {
printf("EnQueue success\n");
}
else
{
printf("EnQueue failed\n");
}
ElemType n;
while (Q.rear!=Q.front)
{
DeQueue(Q, n);
}
ElemType element;//存储出队元素
DeQueue(Q, element);
return 0;
}
附运行结果图
-
链队列的代码实现
#include "stdio.h"
#include <stdlib.h>
#define MaxSize 10
typedef int ElemType;
typedef struct LinkNode {//建立一个链表队列
ElemType data;
struct LinkNode* next;
}LinkNode;
typedef struct {
LinkNode* rear, * front;//链表头链表尾
}LinkQueue;
void InitQueue(LinkQueue &Q) {
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));//动态为头节点尾节点分配空间
Q.front->next = NULL;//头节点的next指针为空
}//初始化队列
bool IsEmpty(LinkQueue Q) {
if (Q.front == Q.rear)
return true;
else
return false;
}
//链队列入队
bool EnQueue(LinkQueue& Q, ElemType &x) {
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = NULL;
Q.rear->next = s;
Q.rear = s;
return true;
}
bool DeQueue(LinkQueue &Q, ElemType &x) {
if (Q.front == Q.rear) {
return false;
}
LinkNode* p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (Q.rear == p)
Q.rear = Q.front;
printf("出队的元素为:%d\n", p->data);
free(p);
return true;
}
int main() {
LinkQueue Q;
InitQueue(Q);//初始化队列
bool ret;
int x;
ret = IsEmpty(Q);
if (ret) {
printf("SeQueue is Empty");
}
else
{
printf("SeQueue is not Empty");
}
while (scanf_s("%d",&x))
{
if (x==-1)
{
break;
}
EnQueue(Q, x);
}
if (ret) {
printf("EnQueue success\n");
}
else
{
printf("EnQueue failed\n");
}
ElemType n;
while (Q.rear!=Q.front)
{
DeQueue(Q, n);
}
ElemType element;//存储出队元素
DeQueue(Q, element);
return 0;
}
附运行结果图,如下所示: