C语言实现顺序栈 链栈 循环队列 链队列的基本操作

顺序栈

#include <stdio.h>
#include <stdlib.h>
#define StackInitSize 10
#define StackIncrement 5
typedef int SElemType;
typedef struct {
	SElemType *base;
	int top;
	int stacksize;
}SqStack;
void InitStack(SqStack &S) {
	S.base=(SElemType*)malloc(StackInitSize * sizeof(SElemType));
	if (!S.base)
		return;
	S.top = 0;
	S.stacksize = StackInitSize;
}
int StackEmpty(SqStack S) {
	if (S.top == 0)
		return 1;
	return 0;
}
int StackLength(SqStack S) {
	return S.top;
}
void Push(SqStack &S, SElemType e) {
	if (S.top >= StackInitSize) {
		S.base = (SElemType*)realloc(S.base,(StackInitSize + StackIncrement) * sizeof(SElemType));
		if (!S.base)
			return;
		S.stacksize = StackInitSize + StackIncrement;
	}
	S.base[S.top] = e;
	S.top++;
}
void pop(SqStack &S, SElemType e) {
	if (S.top == 0)
		return;
	S.top--;
	e = S.base[S.top];
}
void GetElem(SqStack S, SElemType &e){
	if (S.top == 0)
		return;
	e = S.base[S.top - 1];
}

链栈

#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode, *LinkList;
void InitStrack(LinkList &LS) {
	LS->next = NULL;
}
int StackEmpty(LinkList LS) {
	if (LS->next == NULL)
		return 1;
	return 0;

}
void push(LinkList &LS, ElemType e) {
	LinkList s= (LinkList)malloc(sizeof(LNode));
	s->data = e;
	s->next = LS;
	LS = s;
}
void pop(LinkList &LS, ElemType &e) {
	if (LS->next == NULL)
		return;
	e = LS->data;
	LinkList s = LS; LS = LS->next;
	free(s);

}
void GetElem(LinkList LS, ElemType e) {
	if (LS->next == NULL)
		return;
	e = LS->data;
	
}

循环队列

#include <stdio.h>
#include <stdlib.h>
#define QMAXSIZE 100
typedef int QElemType;
typedef struct {
	QElemType *base;
	int front, rear;
}CqQueue;
void InitQueue(CqQueue &Q) {
	Q.base = (QElemType*)malloc(QMAXSIZE*sizeof(QElemType));
	if (!Q.base)
		return;
	Q.front = Q.rear = 0;
}
int QueueEmpty(CqQueue Q) {
	if (Q.front == Q.rear)
		return 1;
	return 0;
}
int QueueLength(CqQueue Q) {
	return (Q.rear - Q.front + QMAXSIZE) % QMAXSIZE;
}
void EnQueue(CqQueue &Q, QElemType e) {
	if ((Q.rear + 1) % QMAXSIZE == Q.front)
		return;
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % QMAXSIZE;

}
void DeQueue(CqQueue &Q, QElemType &e) {
	if (Q.rear== Q.front)
		return;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % QMAXSIZE;
}

链队列

#include <stdio.h>
#include <stdlib.h>
typedef int QElemType;
typedef struct QNode {
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;
typedef struct {
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;
void InitQueue(LinkQueue &Q) {
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
	if (!Q.front)
		return;
	Q.front->next = NULL;

}
int QueueEmpty(LinkQueue Q) {
	if (Q.front = Q.rear)
		return 1;
	return 0;

}
void EnQueue(LinkQueue &Q, QElemType e) {
	QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
	s->data = e; s->next = NULL;
	Q.rear->next = s;
	Q.rear = s;

}
void DeQueue(LinkQueue &Q, QElemType e) {
	if (Q.front == Q.rear)
		return;
	QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
	s = Q.front->next; e = s->data;
	Q.front->next = S.next;  //处理头指针
	if (Q.rear == Q.front->next)	//处理尾指针
		Q.rear = Q.front;
	free(s);
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
顺序循环队列的获取队列长度的基本操作可以通过计算队列中元素的个数来实现。具体实现过程如下: 1. 定义队列结构体,包括队列数组、队头、队尾和队列最大长度等成员变量。 ``` #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int front; int rear; int length; } SqQueue; ``` 2. 初始化队列,将队头和队尾指针都指向数组的第一个元素,队列长度设置为0。 ``` void InitQueue(SqQueue *Q) { Q->front = Q->rear = 0; Q->length = 0; } ``` 3. 判断队列是否为空,根据队列长度是否为0来判断。 ``` int QueueEmpty(SqQueue *Q) { if (Q->length == 0) { return 1; } else { return 0; } } ``` 4. 获取队列长度,直接返回队列长度即可。 ``` int QueueLength(SqQueue *Q) { return Q->length; } ``` 5. 入队操作,先判断队列是否已满,若已满则返回错误,否则将元素插入到队尾,并更新队列长度。 ``` int EnQueue(SqQueue *Q, int x) { if ((Q->rear + 1) % MAXSIZE == Q->front) { return -1; // 队列已满 } Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % MAXSIZE; Q->length++; return 0; } ``` 6. 出队操作,先判断队列是否为空,若为空则返回错误,否则将队头元素删除,并更新队列长度。 ``` int DeQueue(SqQueue *Q, int *x) { if (Q->front == Q->rear) { return -1; // 队列为空 } *x = Q->data[Q->front]; Q->front = (Q->front + 1) % MAXSIZE; Q->length--; return 0; } ``` 通过以上操作,我们可以实现顺序循环队列的获取队列长度的基本操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值