链栈和数组队列

链栈

main.cpp:

#include <stdio.h>
#include <stdlib.h>
typedef int TYPE;
struct node
{
	TYPE data;
	node* next;
};
node* node_create()
{
	node* temp = (node*)malloc(sizeof(node));
	temp->data = 0;
	temp->next = NULL;
	return temp;
}
bool isEmpty(node* head)
{
	return head== NULL ? true:false;
}
TYPE front(node* nn)
{
	return nn->data;
}
void push(node* &head,TYPE val) {
	node* temp = node_create();
	temp->data = val;
	temp->next = head;
	head = temp;
}
TYPE pop(node* &head)
{
	if (isEmpty(head)) return 0;
	node* temp = head;
	head = head->next;
	TYPE tt = temp->data;
	free(temp);
	return tt;
}
void show(node* head)
{
	node* temp = head;
	while (temp != NULL)
	{
		printf("%d  ", temp->data);
		temp = temp->next;
	}
	printf("\n");
}
int main()
{
	node* head = node_create();

	push(head, 1);
	push(head, 2);
	push(head, 3);
	show(head);
	printf("pop出来的是%d\n", pop(head));
	push(head, 4);
	push(head, 5);
	printf("pop出来的是%d\n", pop(head));
	show(head);
	free(head);
	return 0;
}

结果:

在这里插入图片描述

数组队列

#include <stdio.h>
int arr[4] = {0};
int size = 0;
bool full()
{
	return size == 4 ? true : false;
}
bool empty()
{
	return size == 0 ? true : false;
}
void push(int val) {
	if (full()) return;
	arr[size] = val;
	size++;
}
int pop()
{
	if (empty()) return 0;
	int temp = arr[0];
	for (int i = 0; i < 3; i++)
	{
		arr[i] = arr[i + 1];
	}
	size--;
	return temp;
}
int main()
{
	push(1);
	push(2);
	push(3);
	printf("是否满%d\n", full()); //判断是否满
	printf("返回的是%d\n", pop()); //返回1
	printf("返回的是%d\n", pop()); //返回2
	printf("返回的是%d\n", pop());//返回3
	printf("是否空%d\n", empty());	//判断是否空,返回true
	push(10);
	push(20);
	printf("返回的是%d\n", pop());	//返回10
	printf("返回的是%d\n", pop());	//返回20
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值