【c语言-数据结构】堆栈和队列基础操作的实现(初始化,非空否,入栈,出栈,取出栈顶元素;初始化,非空否,入队,出栈,取队头元素)


堆栈

顺序堆栈的操作实现(初始化,非空否,入栈,出栈,取出栈顶元素)

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define Datatype int
typedef struct {
	Datatype stack[MAXSIZE];
	int top;
}Sqstack;
//初始化
void Initiate(Sqstack *s)//(Sqstack &s)(c++ 引用传递
{
	s->top = 0;//(s.top=0;
}
//判断是否为空,空返回0,否则为1
int StackEmpty(Sqstack s)
{
	if (s.top <= 0) return 0;
	else return 1;
}
//入栈
int StackPush(Sqstack* s, Datatype x)
{
	//把x存入顺序栈中,成功返回1否则为0
	if (s->top >= MAXSIZE)
	{
		printf("堆栈已满,无法插入\n");
		return 0;
	}
	else
	{
		s->stack[s->top] = x;
		s->top++;
		return 1;
	}
}
//出栈
int StackPop(Sqstack* s, Datatype *d)
{
	//由d带回栈顶的值,成功返回1否则为0
	if (s->top <= 0)
	{
		printf("堆栈已空\n");
		return 0;
	}
	else
	{
		s->top--;
		*d = s->stack[s->top];
		return 1;
	
	}
}
//取栈顶元素
int StackTop(Sqstack s, Datatype* d)
{
	//取栈顶元素由d带回,成功1否则0
	if (s.top <= 0)
	{
		printf("堆栈已空\n");
		return 0;
	}
	else
	{
		*d = s.stack[s.top - 1];
		return 1;
	}
}
int main()
{
	Sqstack s ;
	Datatype d=0;
	Initiate(&s);//Initiate(s);
	for(int i = 0;i<5;i++) 
		StackPush(&s, i+1);//构建一个顺序栈
	StackTop(s, &d);//取栈顶元素
	printf("当前栈顶元素为:%d\n", d);
	printf("依次按顺序出栈:\n");
	while (StackEmpty(s))
	{
		StackPop(&s, &d);
		printf("%d\n", d);
	}
	return 0;
}

链式堆栈的操作实现(初始化,非空否,入栈,出栈,取出栈顶元素)

#include<stdio.h>
#include<stdlib.h>
#define Datatype int
typedef struct snode{
	Datatype data;
	struct snode* next;
}Snode;
//初始化
void Initiate(Snode **head)//(Sqstack &s)(c++ 引用传递
{
	*head = (Snode*)malloc(sizeof(Snode));
	if (*head == NULL)//检验是否申请成功
	{
		printf("error");
		return;
	}
	(*head)->next = NULL;
}

//判断是否为空,空返回0,否则为1
int StackEmpty(Snode *head)
{
	if (head->next==NULL) return 0;
	else return 1;
}
//入栈
void StackPush(Snode* head, Datatype x)
{
	//把x插入链式堆栈head的栈顶作为新的栈顶
	Snode* p = (Snode*)malloc(sizeof(Snode));
	if (p == NULL)
	{
		printf("error!");
		return;
	}
	p->data=x;
	p->next = head->next;
	head->next = p;
}
//出栈
int StackPop(Snode*head, Datatype *d)
{
	//由d带回栈顶的值,成功返回1否则为0
	Snode* p = head->next;
	if (p == NULL)
	{
		printf("堆栈已空,出栈错误!");
		return 0;
	}
	else
	{
		head->next = p->next;
		*d = p->data;
		free(p);
		return 1;
	}
}
//取栈顶元素
int StackTop(Snode*head, Datatype* d)
{
	//取栈顶元素由d带回,成功1否则0
	Snode* p = head->next;
	if (p==NULL)
	{
		printf("堆栈已空\n");
		return 0;
	}
	else
	{
		*d =p->data;
		return 1;
	}
}
//撤销动态申请空间
void Destory(Snode* head)
{
	Snode* p, * q;
	p = head;
	while (p != NULL)
	{
		q = p;
		p = p->next;
		free(q);
	}
}
int main()
{
	Snode* head ;
	Datatype d = 0;
	Initiate(&head);//Initiate(s);
	for(int i = 0;i<5;i++)
		StackPush(head, i+1);//构建一个顺序栈
	StackTop(head, &d);//取栈顶元素
	printf("当前栈顶元素为:%d\n", d);
	printf("依次按顺序出栈:\n");
	while (StackEmpty(head))
	{
		StackPop(head, &d);
		printf("%d\n", d);
	}
	Destory(head);
	return 0;
}

c++ stack

#include<stack>
stack<Datatype> s
s.push(a) 元素a入栈,增加元素 
s.pop()	移除栈顶元素 
s.top()	取得栈顶元素
s.empty() 检测是否为空,空为真 
s.size() 返回栈内元素的个数 

队列

顺序队列

循环顺序队列的操作实现(初始化,非空否,入队,出栈,取队头元素)

#include<stdio.h>
#define MAXSIZE 100
#define DataType int
typedef struct {
	DataType queue[MAXSIZE];
	int rear;
	int front;
	int count;
}SCQueue;

//初始化
void InitiateQ(SCQueue* s)
{
	s->rear = 0; //定义初始队尾指针下标值
	s->front = 0;//定义初始队头指针下标值
	s->count = 0;//定义初始计数器值
}
//非空否
int Empty(SCQueue Q)
{
	if (Q.count == 0)return 0;
	else return 1;
}
//入队
int Append(SCQueue* Q, DataType x)
{
	if (Q->count > 0 && Q->rear == Q->front)//队满判断,或:Q->count==MAXSIZE
	{
		printf("队列已满\n");
	}
	else
	{
		Q->queue[Q->rear] = x;//插入队尾
		Q->rear = (Q->rear + 1) % MAXSIZE;//队尾指针加一
		Q->count++;
		return 1;
	}
}
//出队
int Delete(SCQueue* Q, DataType* d)
{
	if (Q->count == 0)
	{
		printf("队列已空\n");
		return 0;
	}
	else
	{
		*d = Q->queue[Q->front];
		Q->front = (Q->front + 1) % MAXSIZE;
		Q->count--;
		return 1;
	}
}
//取队头元素
int Get(SCQueue Q, DataType* d)
{
	if (Q.count == 0)
	{
		printf("队列已空\n");
		return 0;
	}
	else
	{
		*d = Q.queue[Q.front];
		return 1;
	}
}

链式队列的操作实现(初始化,非空否,入队,出队,取队头元素)

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define DataType int

typedef struct qnode//结点的结构体定义
{
	DataType data;
	struct qnode* next;
}LQNode;

typedef struct {
	LQNode* rear;//队头指针
	LQNode* front;//尾
}LQueue;
//初始化
void InitiateQ(LQueue* Q)
{
	Q->front=NULL; //定义初始队尾指针下标值
	Q->rear = NULL;//定义初始队头指针下标值
	
}
//非空否
int Empty(LQueue Q)
{
	if (Q.front == NULL) return 0;
	else return 1;
}
//入队
void Append(LQueue* Q, DataType x)
{
	LQNode* p;
	p = (LQNode*)malloc(sizeof(LQNode));//LQNode* p = (LQNode*)malloc(sizeof(LQNode));
	p->data = x;
	p->next = NULL;
	if (Q->rear != NULL)
	{
		Q->rear->next = p;//队列原来非空:队尾加入新节点
	}
	Q->rear = p;//修改队尾指针
	if (Q->front == NULL)
	{
		Q->front = p;//队列原来为空:修改队头指针
	}
}
//出队
int Delete(LQueue* Q, DataType* d)
{
	LQNode* p;
	if (Q->front == NULL)
	{
		printf("队列已空\n");
		return 0;
	}
	else
	{
		*d = Q->front->data;
		p = Q->front;
		Q->front = Q->front->next;//出队列且脱链
		if (Q->front == NULL)
		{
			Q->rear = NULL;//删除最后一个结点后,要置队尾指针为空
		}
		free(p);
		return 1;
	}
}
//取队头元素
int Get(LQueue Q, DataType* d)
{
	if (Q.front == NULL)
	{
		printf("队列已空\n");
		return 0;
	}
	else
	{
		*d = Q.front->data;//(front为指针)
		return 1;
	}
}
//撤销动态申请空间
void Destroy(LQueue Q)
{
	LQNode* p, * q;
	p = Q.front;
	while (p != NULL)
	{
		q = p;
		p = p->next;
		free(q);
	}
}
  • 11
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值