栈和队列的基本操作实现

《数据结构》(c语言版)——第三章、栈和队列


抽象数据类型——栈

顺序存储结构:

#include
#include
#include

#define ok 1
#define error 0
#define overflow -1

#define STACK_INIT_SIZE 100   //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间的分配增量
typedef int Status;
typedef int ElemType;

/*栈的顺序存储表示*/
typedef struct{
    ElemType *base;
    ElemType *top;  //栈顶指针
    int stacksize;  //当前已分配的存储空间,以元素为单位
}SqStack;

/*基本操作函数的原型说明*/
Status InitStack(SqStack &S);  //构造空栈S
Status GetTop(SqStack S,ElemType &e);   //返回栈顶元素
Status Push(SqStack &S,ElemType e); //插入e为新的栈顶元素
Status Pop(SqStack &S,ElemType &e); //删除栈顶元素用e返回


/*基本操作的算法实现*/
Status InitStack(SqStack &S)
{
    S.base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if(!S.base)
        exit(overflow); //存储分配失败
    S.top=S.base;
    S.stacksize=STACK_INIT_SIZE;
    return ok;
}

Status GetTop(SqStack S,ElemType &e)
{
    if(S.top==S.base)
        exit(overflow);
    e=*(S.top-1);
    return ok;
}

Status Push(SqStack &S,ElemType e)
{
    if(S.top-S.base>=S.stacksize)	//栈满,追加存储结构 
    {
		S.base=(ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
		if(!S.base)
			exit(overflow);		//存储分配失败 
		S.top=S.base+S.stacksize;
		S.stacksize+=STACKINCREMENT; 
    }
    *S.top++ =e;
    return ok;
}//push

Status Pop(SqStack &S,ElemType &e)
{
	if(S.top==S.base)
		return error;
	e= *--S.top;
	return ok;
}//Pop

/*简单的测试函数*/
int main()
{
	SqStack S;
	InitStack(S);
	int i,n;
	ElemType e;
	printf("Please input the SqStack's length: ");
	scanf("%d",&n);
	
	printf("\nThe sequence of pushstack is \n");
	for(i=0;i

抽象数据类型——队列

单链队列(链式存储)

#include
#include
#include

#define ok 1
#define error 0
#define overflow -1
typedef int Status;
typedef int ElemType;

/*单链队列的链式存储结构*/
typedef struct QNode
{
    ElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front; //队头指针
    QueuePtr rear;  //队尾指针
}LinkQueue;

/*基本操作函数的原型说明*/
Status InitQueue(LinkQueue &Q); //构造一个空队列
Status DestroyQueue(LinkQueue &Q);  //销毁队列Q
Status EnQueue(LinkQueue &Q,ElemType e);    //插入元素e作队尾
Status DeQueue(LinkQueue &Q,ElemType &e);   //若队列不空,删除队头元素,用e返回

/*基本操作函数的实现*/
Status InitQueue(LinkQueue &Q)
{
    Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
    if(!Q.front)
        exit(overflow); //存储分配失败
    Q.front->next=NULL;
    return ok;
}

Status DestroyQueue(LinkQueue &Q)
{
    while(Q.front)
    {
        Q.rear=Q.front->next;
        free(Q.front);
        Q.front=Q.rear;
    }
    return ok;
}

Status EnQueue(LinkQueue &Q,ElemType e)
{
    QueuePtr p;
    p=(QueuePtr)malloc(sizeof(QNode));
    if(!p)
        exit(overflow); //存储分配失败
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return ok;
}

Status DeQueue(LinkQueue &Q,ElemType &e)
{
    QueuePtr p;
    if(Q.front==Q.rear)
        return error;   //若队列为空,则返回错误
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
    return ok;
}

/*简单的测试函数*/
int main()
{
    LinkQueue Q;
    InitQueue(Q);

    int i,n;
    ElemType e;

    printf("please input the sequence's length :  ");
    scanf("%d",&n);
    printf("\nThe sequence of enqueue is \n");
    for(i=0;i

循环队列(顺序存储)

#include
#include
#include

#define ok 1
#define error 0
#define overflow -1
#define MAXQSIZE 100    //最大队列长度
typedef int Status;
typedef int ElemType;

/*循环队列的顺序存储结构*/
typedef struct{
    ElemType *base; //初始化的动态分配存储空间
    int front;  //头指针,若队列不空,指向队列的头元素
    int rear;   //尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;

/*循环队列的基本函数原型说明*/
Status InitQueue(SqQueue &Q);   //构造一个空队列
Status QueueLength(SqQueue Q);  //返回队列的长度
Status EnQueue(SqQueue &Q,ElemType e);  //插入元素e为新的队尾元素
Status DeQueue(SqQueue &Q,ElemType &e); //若队列不空,删除队头元素,用e返回

/*循环队列的基本操作算法实现*/
Status InitQueue(SqQueue &Q)
{
    Q.base=(ElemType *)malloc(MAXQSIZE * sizeof(ElemType));
    if(!Q.base)
        exit(overflow); //存储分配失败
    Q.front=Q.rear=0;
    return ok;
}

Status QueueLength(SqQueue Q)
{
    return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}

Status EnQueue(SqQueue &Q,ElemType e)
{
    if((Q.rear+1)%MAXQSIZE==Q.front)
        return error;   //队列满
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXQSIZE; //队尾指针加1
    return ok;
}

Status DeQueue(SqQueue &Q,ElemType &e)
{
    if(Q.front==Q.rear)
        return error;   //队空
    e=Q.base[Q.front];
    Q.front=(Q.front+1)%MAXQSIZE;   //队头指针加1
    return ok;
}

/*简单测试函数*/
int main()
{
    SqQueue Q;
    InitQueue(Q);

    int i,n;
    ElemType e;

    //printf("please input the sequence's length :  ");
    //scanf("%d",&n);
    printf("The sequence of enqueue is \n");
    for(i=0;i<10;i++)
    {
        EnQueue(Q,2*i);
        printf("%d ",2*i);
    }
    printf("\n\n");


    printf("The sequence's length: \n");
    n=QueueLength(Q);
    printf("%d\n\n",n);

    printf("The sequence of dequeue is \n");
    for(i=0;i<10;i++)
    {
        DeQueue(Q,e);
        printf("%d ",e);
    }
    printf("\n\n");

    printf("The sequence's length: \n");
    n=QueueLength(Q);
    printf("%d\n\n",n);

    return 0;
}


你可能感兴趣的:(ACM)