标题:实现基于数组的栈和队列 |
分析:栈与队列有两种实现方式,一种是链表,一种是数组。下面就以数组的形式来实现栈和队列。
#define MAXSIZE 100 /*定义stacksize为常数100 */
int push(int stack[], int *ptop, int value)
{
int top;
top = *ptop;
if ( top == MAXSIZE)
{
printf("overflow\n");
return -1;
}
else
{
......;
}
}
int pop(int stack[], int *ptop, int *value)
{
int top;
top = *ptop;
if (top==0)
{
return -1;
}
else
{
......
return 0;
}
}
#define MAXSIZE 100
int EnQueue(int *Q, int *pf, int *pr, int value)
{
int front,
rear;
front =
*pf;
rear = *pr;
if (
(rear+1) % MAXSIZE == front )
return 0;
else
{
......
return 1;
}
}
int DeQueue(int *Q, int *pf, int *pr, int *value)
{
int front,
rear;
front =
*pf;
rear = *pr;
if (front
== rear)
return 0;
else
{
......
return 1;
}
}
Copyright 2011-2020 © MallocFree. All rights reserved.