栈与队列 练习

###栈

#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
#define Stack_size 10
typedef struct Stack
{
	DataType* _array;//数组指针
	size_t	_top; //栈顶 
	size_t	_end;//最大容量
}Stack;
-----------------------------------------------------------
// 栈的实现接口 
void StackInit(Stack* s)//栈的初始化
{
	assert(s);
	s->_array = (DataType*)malloc(sizeof(DataType)*Stack_size);
	s->_end = Stack_size;
	s->_top = 0;
	
}

-----------------------------------------------------------

void StackPush(Stack* s, DataType x)//入栈
{
	assert(s);
	if (s->_end == s->_top)//栈已满
	{
		s->_end *= 2;
		s->_array = (DataType*)realloc(s->_array, sizeof(DataType)*(s->_end));
		s->_array[s->_top] = x;
		(s->_top)++;
	}
	else
	{
		s->_array[s->_top] = x;
		(s->_top)++;
	}
}

---------------------------------------------------------

void Stack_print(Stack *s)
{
	if (s == NULL)
	{
		return;
	}
	while ((s->_top)--)
	{
		printf("%d\t", s->_array[s->_top]);
	}
}

---------------------------------------------------------

void StackPop(Stack* s)//出栈
{
	assert(s);
	if (s->_top == 0)
	{
		printf("the stack is empty");
	}
	else
	{
		s->_top--;
	}
}

---------------------------------------------------------

DataType StackTop(Stack* s)//取栈顶元素
{
	assert(s);
	if (s->_top == 0)
	{
		printf("the stack is empty");
		return;
	}
	else
	{
		int num = s->_top;
		int i = s->_array[(--num)];
		return i;
	}
}

---------------------------------------------------------

size_t StackSize(Stack* s)//栈的长度
{
	assert(s);
	return s->_end;
}

---------------------------------------------------------

int StackEmpty(Stack* s)//判断栈是否为空
{
	if (s->_top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

###队列

#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef int DataType;
typedef struct QueueNode
{
	DataType _data;    //链式队列
	struct QueueNode* _next;
}QueueNode;

typedef struct Queue
{
	QueueNode* _head;//队头指针
	QueueNode* _tail;//队尾指针
}Queue;
------------------------------------------------
void QueueInit(Queue* q)//队列初始化
{
	q->_head = (QueueNode*)malloc(sizeof(QueueNode));
	assert(q->_head);
	q->_tail = q->_head;
	q->_tail->_next = NULL;
}

-------------------------------------------------

void QueuePush(Queue* q, DataType x)//入队
{
	assert(q);
	QueueNode *newnode = (QueueNode*)malloc(sizeof(QueueNode));
	assert(newnode);
	newnode->_data = x;
	newnode->_next = NULL;
	q->_tail->_next = newnode;
	q->_tail = newnode;
}

---------------------------------------------------

void QueuePop(Queue* q)//出队
{
	assert(q);
	if (q->_head == q->_tail)
	{
		printf("the queue is empty.");
		return;
	}
	QueueNode *p = q->_head->_next;
	q->_head = p;

}

---------------------------------------------------

DataType QueueFront(Queue* q)//取队头数据
{
	assert(q);
	if (q->_head == q->_tail)
	{
		printf("the queue is empty.");
		return 0;
	}
	return (q->_head->_next)->_data;
}

----------------------------------------------------

DataType QueueBack(Queue* q)//取队尾数据
{
	assert(q);
	if (q->_head == q->_tail)
	{
		printf("the queue is empty.");
		return 0;
	}
	QueueNode *p = q->_head->_next;
	while (p != q->_tail)
	{
		p = p->_next;
	}
	return p->_data;
}

-------------------------------------------------

size_t QueueSize(Queue* q)//计算队列长度
{
	assert(q);
	if (q->_head == q->_tail)
	{
		return 0;
	}
	QueueNode *flag = q->_head->_next;
	size_t i = 0;
	while (flag != q->_tail)
	{
		flag = flag->_next;
		i++;
	}
	return i+1 ;
}

-----------------------------------------------

int QueueEmpty(Queue* q)//判断队列是否为空
{
	assert(q);
	if (q->_head == q->_tail)
	{
		printf("the queue is empty.");
		return 0;
	}
	return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值