Stacks and queues

//============================================================
// 使用数组实现的栈 20140613
//============================================================
#include <stdio.h>
#include <stdlib.h>

typedef	int StackElem;
typedef struct stack {
	int top;  // 指向栈顶元素
	int size; // 栈大小capacity
	StackElem *arr;
} *STACK;

#ifndef ERROR
#define ERROR -1
#endif

//============================================================
// 初始化栈
//============================================================
STACK InitializeStack(STACK S, int n)
{
	S = (STACK)malloc(sizeof(struct stack));
	if (!S) {
		printf("s malloc error\n");
		return NULL;
	}
	S->arr = (StackElem *)malloc(sizeof(StackElem) * n);
	if (!S->arr) {
		free(S);
		printf("s->arr malloc error\n");
		return NULL;
	}
	S->size = n;
	S->top = -1; // careful
	return S;
}

//============================================================
// 判断栈是否为空
//============================================================
bool StackEmpty(STACK S)
{
	return S->top == -1; // careful
}

//============================================================
// 判断栈是否满
//============================================================
bool StackFull(STACK S)
{
	return S->top == S->size - 1;
}

//============================================================
// 进栈(top为最近插入元素的索引)
//============================================================
void Push(STACK S, StackElem x)
{
	if (StackFull(S)) {
		printf("overflow\n");
		return ;
	} else {
		// S->top = S->top + 1;
		// S->arr[S->top] = x;
		S->arr[++S->top] = x;
	}
}

//============================================================
// 出栈
//============================================================
StackElem Pop(STACK S)
{
	if (StackEmpty(S)) {
		printf("underflow\n");
		return ERROR;
	} else {
		// S->top = S->top - 1;
		// return S->arr[S->top + 1];
		return S->arr[S->top--];
	}
}

//============================================================
// 输出栈中元素
//============================================================
void StackPrint(STACK S)
{
	if (StackEmpty(S))
		return ;

	for (int i = 0; i <= S->top; i++)
		printf("%d\t", S->arr[i]);
	printf("\n");
}

int main()
{
	int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};
	int len = sizeof(arr) / sizeof(arr[0]);
	STACK S = NULL;

	S = InitializeStack(S, len - 1);
	for (int i = 0; i < len; i++) {
		Push(S, arr[i]);
		printf("%d\t", arr[i]);
	}
	printf("\n");

	for (int i = 0; i < len; i++) {
		StackElem val = Pop(S);
		if (val != ERROR)
			printf("%d\t", val);
	}
	printf("\n");

	system("pause");
	return 0;
}
//============================================================
// 使用链表实现的栈 20140613
//============================================================
#include <stdio.h>
#include <stdlib.h>

typedef	int StackElem;

typedef struct snode {
	StackElem elem;
	struct snode *next;
} SNode;

typedef struct stack {
	struct snode *top;
} *STACK;

#ifndef ERROR
#define ERROR -1
#endif

//============================================================
// 初始化栈
//============================================================
STACK InitializeStack(STACK S, int n)
{
	S = (STACK)malloc(sizeof(struct stack));
	if (!S) {
		printf("s malloc error\n");
		return NULL;
	}
	S->top = NULL;

	return S;
}

//============================================================
// 判断栈是否为空
//============================================================
bool StackEmpty(STACK S)
{
	return S->top == NULL;
}


//============================================================
// 进栈
//============================================================
void Push(STACK S, StackElem x)
{
	struct snode *p = (struct snode *)malloc(sizeof(struct snode));
	if (!p) {
		printf("p malloc error\n");
		return ;
	}
	p->elem = x;
	p->next = NULL;

	p->next = S->top; // 在S->top后插入
	S->top = p;
}

//============================================================
// 出栈
//============================================================
StackElem Pop(STACK S)
{
	if (StackEmpty(S)) {
		printf("underflow\n");
		return ERROR;
	} 

	StackElem x = S->top->elem;
	struct snode *p = S->top; // 临时保存待删除的结点
	S->top = S->top->next;
	free(p);

	return x;
}

//============================================================
// 输出栈中元素
//============================================================
void StackPrint(STACK S)
{
	struct snode *p = S->top;
	while (p) {
		printf("%d\t", p->elem);
		p = p->next;
	}
	printf("\n");
}

//============================================================
// 获得栈的大小
//============================================================
int GetStackSize(STACK S)
{
	int size = 0;
	struct snode *p = S->top;
	while (p) {
		size++;
		p = p->next;
	}

	return size;
}

int main()
{
	int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};
	int len = sizeof(arr) / sizeof(arr[0]);
	STACK S = NULL;

	S = InitializeStack(S, len - 1);
	for (int i = 0; i < len; i++) {
		Push(S, arr[i]);
		printf("size = %d\n", GetStackSize(S));
		StackPrint(S);
	}
	
	for (int i = 0; i < len; i++) {
		StackElem val = Pop(S);
		if (val != ERROR) {
			printf("size = %d\n", GetStackSize(S));
			StackPrint(S);
		}
	}

	system("pause");
	return 0;
}


//============================================================
// 使用数组实现的循环队列 20140613
//============================================================
#include <stdio.h>
#include <stdlib.h>

typedef	int QueueElem;
typedef struct queue {
	int head;  // 队列头
	int tail;  // 队列尾
	int size;  // 队列大小capacity
	QueueElem *arr;
} *QUEUE;

#ifndef ERROR
#define ERROR -1
#endif

//============================================================
// 初始化队列
//============================================================
QUEUE InitializeQueue(QUEUE Q, int n)
{
	Q = (QUEUE)malloc(sizeof(struct queue));
	if (!Q) {
		printf("Q malloc error\n");
		return NULL;
	}
	Q->arr = (QueueElem *)malloc(sizeof(QueueElem) * (n + 1));
	if (!Q->arr) {
		printf("Q->arr malloc error\n");
		return NULL;
	}
	Q->size = n;
	Q->head = Q->tail = 0;
	return Q;
}

//============================================================
// 判断队列是否为空
//============================================================
bool QueueEmpty(QUEUE Q)
{
	return Q->head == Q->tail;
}

//============================================================
// 判断对垒是否满(careful)
//============================================================
bool QueueFull(QUEUE Q)
{
	return Q->head == (Q->tail + 1) % Q->size;
}

//============================================================
// 进队列
//============================================================
void Enqueue(QUEUE Q, QueueElem x)
{
	if (QueueFull(Q)) {
		printf("overflow\n");
		return;
	}
	Q->arr[Q->tail] = x;
	if (Q->tail == Q->size) // 到数组的末端,卷绕
		Q->tail = 0;
	else
		Q->tail++;
}

//============================================================
// 出队列
//============================================================
QueueElem Dequeue(QUEUE Q)
{
	if (QueueEmpty(Q)) {
		printf("underflow\n");
		return ERROR;
	}
	int x = Q->arr[Q->head];
	if (Q->head == Q->size) // 到数组的末端,卷绕
		Q->head = 0;
	else
		Q->head++;
	return x;
}

int main()
{
	int arr[] = {45, 65, 56, 2, 542, 6, 89, 21, 97, 60};
	int len = sizeof(arr) / sizeof(arr[0]);
	QUEUE Q = NULL;

	Q = InitializeQueue(Q, len - 1);
	for (int i = 0; i < len; i++) {
		Enqueue(Q, arr[i]);
		printf("%d\t", arr[i]);
		printf("head:%d, tail:%d\n", Q->head, Q->tail);
	}

	for (int i = 0; i < len; i++) {
		QueueElem val = Dequeue(Q);
		if (val != ERROR)
			printf("%d\t", val);
		printf("head:%d, tail:%d\n", Q->head, Q->tail);
	}

	system("pause");
	return 0;
}
//============================================================
// 使用链表实现的循环队列 20140613
//============================================================
#include <stdio.h>
#include <stdlib.h>

typedef	int QueueElem;

typedef struct qnode {
	QueueElem elem;
	struct qnode *next;
} QNode;

typedef struct queue {
	struct qnode *front;
	struct qnode *rear;
} *QUEUE;

#ifndef ERROR
#define ERROR -1
#endif

//============================================================
// 初始化队列
//============================================================
QUEUE InitializeQueue(QUEUE Q)
{
	Q = (QUEUE)malloc(sizeof(struct queue));
	if (!Q) {
		printf("Q malloc error\n");
		return NULL;
	}
	Q->front = NULL;
	Q->rear = NULL;

	return Q;
}

//============================================================
// 判断队列是否为空
//============================================================
bool QueueEmpty(QUEUE Q)
{
	return Q->front == NULL;
}

//============================================================
// 进队列
//============================================================
void Enqueue(QUEUE Q, QueueElem x)
{
	struct qnode *p = (struct qnode *)malloc(sizeof(struct qnode));
	if (!p) {
		printf("q malloc error\n");
		return ;
	}
	p->elem = x;
	p->next = NULL;

	if (QueueEmpty(Q)) {
		Q->front = p;
		Q->rear = p;
	} else {
		Q->rear->next = p; // 将新元素插到链表尾部
		Q->rear = p;       // 更新Q->rear的位置
	}
}

//============================================================
// 出队列
//============================================================
QueueElem Dequeue(QUEUE Q)
{
	if (QueueEmpty(Q)) {
		printf("underflow\n");
		return ERROR;
	}

	int x = Q->front->elem;     // Q->front != NULL
	struct qnode *p = Q->front; // 临时保存待删除的结点
	Q->front = Q->front->next;  // if (Q->front == NULL) Q->rear = NULL;
	free(p);

	return x;
}

//============================================================
// 获得队列大小
//============================================================
int GetQueueSize(QUEUE Q)
{
	int size = 0;
	struct qnode *p = Q->front;
	while (p) {
		size++;
		p = p->next;
	}

	return size;
}

//============================================================
// 打印队列
//============================================================
void PrintQueue(QUEUE Q)
{
	struct qnode *p = Q->front;
	while (p) {
		printf("%d\t", p->elem);
		p = p->next;
	}
	printf("\n");
}

int main()
{
	int arr[] = {45, 12, 56, 3, 542, 6, 89, 21, 97, 60};
	int len = sizeof(arr) / sizeof(arr[0]);
	QUEUE Q = NULL;

	Q = InitializeQueue(Q);
	for (int i = 0; i < len; i++) {
		Enqueue(Q, arr[i]);
		printf("size = %d\n", GetQueueSize(Q));
		PrintQueue(Q);
	}

	for (int i = 0; i < len; i++) {
		QueueElem val = Dequeue(Q);
		if (val != ERROR) {
			printf("size = %d\n", GetQueueSize(Q));
			PrintQueue(Q);
		}
	}

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值