栈和队列实现-c语言版

一、栈

1. 顺序栈

声明栈结构

#define StackMaxSize 16
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1

typedef struct Stack{
	int Data[StackMaxSize];
	int top;
}Stack;

初始化

void InitStack(Stack *S)
{
	if(S == NULL)
		return;

	S->top = -1;
}

判空

int isEmpty(Stack *S)
{
	if(S->top == -1)
	{
		return TRUE;
	}else
	{
		return FALSE;
	}
}

Push栈

int PushStack(Stack *S, int e)
{
	if(S->top >= (StackMaxSize - 1))
	{
		printf("Stack is full.\n");
		return ERROR;
	}

	S->Data[++(S->top)] = e;
	return OK;
}

Pop栈

int PopStack(Stack *S)
{
	int e = 0;
	if(isEmpty(S))
	{
		printf("Stack is empty.\n");
		return ERROR;
	}

	e = S->Data[(S->top)--];
	return e;
}

2.链栈

声明链栈结构

typedef struct Node{
	int data;
	struct Node *next;
}Node;

typedef struct LStack{
	Node *top;
}LStack;

初始化

void InitStack(LStack *S)
{
	S->top = (Node *)malloc(sizeof(Node));
	S->top->next = NULL;
}

判空

int isEmpty(LStack *S)
{
	if(S->top->next == NULL)
		return 1;
	else
		return 0;
}

Push栈

int PushStack(LStack *S, int e)
{
	Node *node_tmp;

	S->top->data = e;
	node_tmp = (Node *)malloc(sizeof(Node));
	if(!node_tmp)
		exit(-1);
	node_tmp->next = S->top;
	S->top = node_tmp;
}

Pop栈

int PopStack(LStack *S)
{
	Node *node_tmp;
	int e = 0;

	if(isEmpty(S))
	{
		printf("stack is empty.\n");
		return -1;
	}

	node_tmp = S->top->next;
	e = node_tmp->data;
	if(node_tmp->next != NULL)
	{
		S->top->next = node_tmp->next;
	}else
	{
		S->top->next = NULL;
	}
	free(node_tmp);

	return e;

}

二、队列

1. 顺序队列-环形结构

声明队列结构

#define QueueMaxSize 16
typedef struct Queue{
	int Data[QueueMaxSize];
	int front;
	int rear;
}Queue;

初始化

void InitQueue(Queue *q)
{
	q->front = 0;
	q->rear = 0;
}

判空

int isQueueEmpty(Queue *q)
{
	if (q->front == q->rear)
	{
		return TRUE;
	}else{
		return FALSE;
	}
}

判满

int isQueueFull(Queue *q)
{
	if ((q->rear + 1) % QueueMaxSize == q->front)
	{
		return TRUE;
	}else{
		return FALSE;
	}
}

Push队列

int PushQueue(Queue *q, int e)
{
	if (isQueueFull(q))
	{
		printf("Queue is full\n");
		return FALSE;
	}

	q->Data[q->rear] = e;
	q->rear = (q->rear + 1) % QueueMaxSize;
	return TRUE;
}

Pop队列

int PopQueue(Queue *q)
{
	int e = 0;

	if (isQueueEmpty(q))
	{
		printf("Queue is empty\n");
		return FALSE;
	}

	e = q->Data[q->front];
	q->front = (q->front + 1) % QueueMaxSize;
	return e;
}

2. 链队列

声明队列结构

typedef struct LNode{
	int Data;
	struct LNode *next;
}Node;

typedef struct LQueue{
	Node *front;
	Node *rear;
}LQueue;

初始化

void InitQueue(LQueue *q)
{
	q->front = q->rear = (Node *)malloc(sizeof(Node));
	if(q->front == NULL)
	{
		exit(-1);
	}
	q->front->next = NULL;
}

判空

int isEmpty(LQueue *q)
{
	if(q->front == q->rear)
		return TRUE;
	else
		return FALSE;
}

Push队列

int PushQueue(LQueue *q, int e)
{
	Node *q_tmp;
	q_tmp = (Node *)malloc(sizeof(Node));
	if(q_tmp == NULL)
	{
		printf("malloc error\n");
		return FALSE;
	}
	q_tmp->Data = e;
	q_tmp->next = NULL;
	q->rear->next = q_tmp;
	q->rear = q_tmp;

	return TRUE;
}

Pop队列

int PopQueue(LQueue *q)
{
	int e = 0;
	Node *q_tmp = NULL;
	if(isEmpty(q))
	{
		printf("Queue is empty!\n");
		return FALSE;
	}

	q_tmp = q->front->next;
	e = q_tmp->Data;
	q->front->next = q_tmp->next;
	if(q_tmp == q->rear)
		q->rear = q->front;

	free(q_tmp);
	return e;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值