1.栈和队列在物理结构上是顺序表
2.typedef关键字详解
此种情况不加typedef 名称写在上面SeqList代表类型名
此种情况不加typedef SeqList 代表全局结构体变量
结构体为无名结构体
上面两种情况的SeqList代表类型名
第一种写的时候直接SeqList XXX
第二种写的时候struct SeqList XXX
类型名相当于给struct Seqlist 起了一一个别名
4.栈顶指针指向的位置没有数据元素
5.卧槽 顺序栈终于整成功了 老子不想泡在bug里 明天好好看看realloc与 malloc的区别
#include"stdafx.h"
#include<assert.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#define doub 2
#define OK 1
#define NULLZHIZHEN -2
#define FullStack - 3
#define EmptyStack -4
#define n 10
using namespace std;
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *bottom;
SElemType *top;
int size;
}SeqStack;
bool Stack_Full(const SeqStack *s);
bool Stack_Empty(const SeqStack *s);
//增容
bool IncSize(SeqStack *s){
assert(s != NULL);
int total = doub*(s->size);
SElemType *newdata = (SElemType *)realloc(s->bottom ,sizeof(SElemType)*total);
if (newdata == NULL) return false;
else{
s->bottom = newdata;
s->top = s->bottom + s->size;
s->size = total;
return true;
}
}
//初始化
void Init_Stack(SeqStack *s){
assert(s != NULL);
s->bottom = (SElemType*)malloc(sizeof(SElemType)*n);
if (s->bottom == NULL) exit(1);
s->size = n;
s->top = s->bottom;
}
//摧毁栈
void Destory_Stack(SeqStack *s){
assert(s != NULL);
free(s->bottom);
s->top = NULL;
s->bottom = NULL;
}
//清空栈
void Clear_Stack(SeqStack* s){
assert(s != NULL);
s->top = s->bottom;
}
//入栈(先放元素 在将指针向上移一位)
Status Push_Stack(SeqStack *s,SElemType val){
assert(s != NULL);
if (Stack_Full(s)&&!IncSize(s)) return FullStack;
else{
*(s->top) = val;
s->top++;
return OK;
}
}
//获取栈顶元素 不删除栈顶元素
Status Get_Top(SeqStack *s, SElemType *p){
assert(s != NULL);
if (Stack_Empty(s)) return EmptyStack;
if (p == NULL) return NULLZHIZHEN;
*p = *(s->top-1);
return true;
}
//出栈
Status Pop(SeqStack *s, SElemType *p){
assert(s != NULL);
if (p == NULL) return NULLZHIZHEN;
if (Stack_Empty(s)) return EmptyStack;
s->top--;
*p = *(s->top);
return true;
}
//栈元素个数
int Stack_Lenth(const SeqStack *s){
assert(s != NULL);
SElemType i = s->top - s->bottom;
return i;
}
//栈判空
bool Stack_Empty(const SeqStack *s){
assert(s != NULL);
if (Stack_Lenth(s) ==0) return true;
else return false;
}
//栈判满
bool Stack_Full(const SeqStack *s){
assert(s != NULL);
if (s->size == Stack_Lenth(s)) return true;
else return false;
}
///
int main(){
SeqStack s;
Init_Stack(&s);
for (int i = 1; i < 15; i++){
Push_Stack(&s, i);
}
//Print_Stack(&s);
SElemType a = 9;
SElemType *p = &a;
cout << "以下为出栈" << endl;
while (!Stack_Empty(&s)){
Pop(&s, p);
cout << *p << endl;
}
//Destory_Stack(&s);
//Print_Stack(&s);
//Clear_Stack(&s);
/*
SElemType a = 9;
SElemType *p=&a;
Get_Top(&s, p);
cout << "栈顶元素为:" << endl;
cout << *p << endl;
*/
//Print_Stack(&s);
cout << "幸得识卿桃花面,从此阡陌多暖春。"<<endl;
}