C语言数据结构-栈stack

2021-05-02 22:29

阅读:571

标签:--   oid   index   ase   get   c语言   while   length   malloc   

栈stack - 是限定在表尾进行插入或删除的线性表

#ifndef __STACK_H__
#define __STACK_H__


#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLF -1
#define OVERFLOW -2

#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

typedef int Status;

typedef int SElemType;

typedef struct {
	SElemType *base;
	SElemType *top;
	int stacksize;
	int length;
}SqStack;

#endif

  

#include"stack.h"
#include
#include

Status InitStack(SqStack &S) {
	S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
	if(!S.base) {
		exit(OVERFLOW);
	}

	S.top = S.base;
	S.stacksize = STACK_INIT_SIZE;
	S.length = 0;
	return OK;
}

Status GetTop(SqStack S, SElemType &e) {
	if(S.top == S.base) {
		return ERROR;
	}

	e = *(S.top - 1);
	return OK;
}

Status Push(SqStack &S, SElemType e) {
	if(S.top - S.base >= S.stacksize) {
		S.base = (SElemType *)realloc(S.base,
			(S.stacksize + STACKINCREMENT) * sizeof(SElemType));
		if(!S.base) {
			exit(OVERFLOW);
		}
		S.top = S.base + S.stacksize;
		S.stacksize += STACKINCREMENT;
	}
	*S.top++ = e;
	S.length++;
	return OK;
}


void show(SqStack &S) {
	printf("stacksize = %d\n", S.stacksize);
	printf("length = %d\n", S.length);
	if(S.length == 0) {
		return;
	}

	for(int i = 0; i 

  

C语言数据结构-栈stack

标签:--   oid   index   ase   get   c语言   while   length   malloc   

原文地址:https://www.cnblogs.com/maduar/p/13200856.html


评论


亲,登录后才可以留言!