队列与栈

1.队列

struct QueueRecord
{
	int Capacity;
	int Front;
	int Rear;
	int Size;
	int *Array;
};

typedef struct QueueRecord *Queue;

int IsEmpty(Queue Q)
{
	return Q->Size == 0;
}
void MakeEmpty(Queue Q)
{
	Q->Size = 0;
	Q->Front = 1;
	Q->Rear = 0;
}
static int Succ(int value, Queue Q)//入队检查大小合法
{
	if (++value == Q->Capacity)
	{
		value = 0;
	}
	return value;
}
void Enqueue(int X, Queue Q)
{
	if (IsFull(Q))
	{
	//err
	}
	else
	{
		Q->Size++;
		Q->Rear = Succ(Q->Rear, Q);
		Q->Array[Q->Rear] = X;
	}
}

2.栈


struct Node
{
	int Element;
	ptrToNode Next;
};

int IsEmpty(Stack S)
{
	return S->Next == NULL;
}

Stack CreateStack(void)
{
	Stack S;
	S = (Stack)(malloc(sizeof(struct Node)));
	if (S == NULL)
	{
		return NULL;
	}
	else
	{
		S->Next = NULL;
		MakeEmpty(S);
		return S;
	}
}
void MakeEmpty(Stack S)
{
	if (S == NULL)
	{
		//ERR
	}
	else
	{
		while (!IsEmpty(S))
		{
			Pop(S);
		}
	}
}
void Pop(Stack S)
{
	ptrToNode firstCell;
	if (IsEmpty(S))
	{
		//err
	}
	else
	{
		firstCell = S->Next;//删除顶端元素(链表第一个),pop出栈
		S->Next = S->Next->Next;
		free(firstCell);
	}
}
void Push(int X, Stack S)
{
	ptrToNode first;
	if (S == NULL)
	{
		//err
	}
	else
	{
		ptrToNode tmp = (ptrToNode)(malloc(sizeof (struct Node)));
		tmp->Element = X;
		first = S->Next;
		tmp->Next = first;
		S->Next = tmp;
	}
}
int Top(Stack S)
{
	int tmp;
	if (S->Next == NULL)
	{
		return -1;//err
	}
	else
	{
		return S->Next->Element;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值