栈结构
顺序栈
对于栈的使用常常使用数组结构进行实现,因为栈和顺序线性表很大区别就在于栈只用对顶部元素进行增删操作即可,和顺序线性表一旦增删就要改动很大的特点有了很大优化。
- 判空
int IsEmpty(stack s) {
return s->top == -1;
}
- 判满
int IsFull(stack s,int maxsize) {
return ++s->top == maxsize;
}
- 创立空栈
stack CreatStack(int maxsize) {
stack s;
s = (Stack*)malloc(sizeof(Stack));
if (s == NULL) {
cout << "out of space!" << endl;
}
else {
s->array = (int*)malloc(sizeof(int) * maxsize);
if (s->array == NULL) {
cout << "out of space!" << endl;
}
else {
s->capacity = maxsize;
MakeEmpty(s);
return s;
}
}
}
- 压栈
void Push(int x,stack s,int maxsize) {
if (!IsFull(s, maxsize)) {
s->array[++s->top] = x;
}
else {
cout << "error!" << endl;
}
}
- 出栈
void Pop(stack s) {
if (!IsEmpty(s)) {
s->top--;
cout << s->array[s->top] << endl;
}
else {
cout << "error!" << endl;
}
}
- 销毁
void DisposeStack(stack s) {
if (s != NULL) {
free(s->array);
free(s);
}
}
- 获得栈顶元素
int GetTop(stack s) {
if (!IsEmpty(s)) {
return s->array[s->top];
}
else {
cout << "error!" << endl;
return 0;
}
}
主程序
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct Stack {
int capacity;
int top;
int* array;
};
typedef struct Stack* stack;
stack CreatStack(int maxsize);
int IsEmpty(stack s);
int IsFull(stack s, int maxsize);
void MakeEmpty(stack s);
void Pop(stack s);
void Push(int x,stack s,int maxsize);
void DisposeStack(stack s);
int GetTop(stack s);
int TopAndPop(stack s);
int main(void) {
stack s =(Stack*)malloc(sizeof(Stack));
int x, y,z;
int maxsize;
cout << "请输入最大空间范围" << endl;
cin >> maxsize;
cout << "请输入压栈值" << endl;
cin >> x;
s=CreatStack(maxsize);
Push(x, s, maxsize);
Push(x, s, maxsize);
Pop(s);
y=GetTop(s);
cout << y << endl;
z=TopAndPop(s);
cout << z << endl;
DisposeStack(s);
cout << "销毁成功!" << endl;
return 0;
}
stack CreatStack(int maxsize) {
stack s;
s = (Stack*)malloc(sizeof(Stack));
if (s == NULL) {
cout << "out of space!" << endl;
}
else {
s->array = (int*)malloc(sizeof(int) * maxsize);
if (s->array == NULL) {
cout << "out of space!" << endl;
}
else {
s->capacity = maxsize;
MakeEmpty(s);
return s;
}
}
}
int IsEmpty(stack s) {
return s->top == -1;
}
int IsFull(stack s,int maxsize) {
return ++s->top == maxsize;
}
void MakeEmpty(stack s) {
s->top = -1;
}
void Pop(stack s) {
if (!IsEmpty(s)) {
s->top--;
}
else {
cout << "error!" << endl;
}
}
void Push(int x,stack s,int maxsize) {
if (!IsFull(s, maxsize)) {
s->array[++s->top] = x;
}
else {
cout << "error!" << endl;
}
}
void DisposeStack(stack s) {
if (s != NULL) {
free(s->array);
free(s);
}
}
int GetTop(stack s) {
if (!IsEmpty(s)) {
return s->array[s->top];
}
else {
cout << "error!" << endl;
return 0;
}
}
int TopAndPop(stack s) {
if (!IsEmpty(s)) {
return s->array[s->top--];
}
else {
cout << "error!" << endl;
return 0;
}
}
链式栈
- 创建空栈
struct STACK* CreatStack(void){
struct STACK* S=malloc(sizeof*S);
S->top=S->bottom=malloc(sizeof*S->top);
if(S->top==NULL){
printf("空间分配失败");
exit(-1);
}
else{
S->top->next=NULL;
return ;
}
}
- 判断空
int StackEmty(struct STACK*S){
if(S->top==S->bottom){
return 1;
}
else{
return 0;
}
}
- 压栈
struct NODE* Push(struct NODE* top) {
struct NODE* node = malloc(sizeof * node);
if (node == NULL) {
printf("空间分配失败");
exit(-1);
}
printf("请输入学生姓名,成绩:");
scanf("%s%f", node->name, &(node->score));
node->next = top;
top = node;
return top;
}
- 输出整个栈
void OutPutStack(struct STACK* S) {
struct NODE* move = S->top;
while (S->bottom != move) {
printf("[姓名:%s成绩:%f]->", move->name, move->score);
move = move->next;
}
printf("\n");
}
- 取栈顶顶点
void GetTop(struct NODE* top) {
printf("[姓名:%s成绩:%f]->", top->name, top->score);
}
- 出栈
struct NODE* Pop(struct NODE* top) {
struct NODE* buf;
buf = top;
top = top->next;
free(buf);
buf = NULL;
return top;
};
- 销毁栈
void DestroyStack(struct STACK* S) {
while (!(StackEmty(S))) {
S->top = Pop(S->top);
}
}
主程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct NODE {
char name[20];
float score;
struct NODE* next;
};
struct STACK {
struct NODE* top;
struct NODE* bottom;
};
struct STACK* CreatStack(void);
int StackEmty(struct STACK* S);
struct NODE* Push(struct NODE* top);
void OutPutStack(struct STACK*);
void GetTop(struct NODE* top);
struct NODE* Pop(struct NODE* S);
void DestroyStack(struct STACK*);
int main(void) {
int num, ret;
struct SATCK* S = (SATCK*)malloc(sizeof(SATCK*));
printf("是否创建空栈1/0:");
int check;
scanf("%d", &check);
if (check == 1) {
S = CreatStack();
}
else {
return 0;
}
printf("1、判断空\n2、压栈\n3、输出整个栈\n4、取栈顶顶点\n5、出栈\n6、销毁栈\n7、退出\n");
while (1) {
printf("请输入操作号:");
scanf("%d", &num);
switch (num) {
case 1:
{
ret = StackEmty(S);
if (ret == 1) {
puts("栈空");
}
else if (ret == 0) {
puts("栈非空");
}
break;
}
case 2:
{
S->top = Push(S->top);
break;
}
case 3:
{
OutputStack(S);
break;
}
case 4:
{
GetTop(S->top);
break;
}
case 5:
{
if (!(StackEmty(S))) {
S->top = Pop(S->top);
printf("出栈成功");
}
else {
puts("栈空,出栈失败");
}
break;
}
case 6:
{
DestoryStack(S);
puts("栈已销毁");
break;
}
case 7:
{
return 0;
break;
}
}
}
return 0;
}
struct STACK* CreatStack(void) {
struct STACK* S = malloc(sizeof * S);
S->top = S->bottom = malloc(sizeof * S->top);
if (S->top == NULL) {
printf("空间分配失败");
exit(-1);
}
else {
S->top->next = NULL;
return;
}
}
int StackEmty(struct STACK* S) {
if (S->top == S->bottom) {
return 1;
}
else {
return 0;
}
}
struct NODE* Push(struct NODE* top) {
struct NODE* node = malloc(sizeof * node);
if (node == NULL) {
printf("空间分配失败");
exit(-1);
}
printf("请输入学生姓名,成绩:");
scanf("%s%f", node->name, &(node->score));
node->next = top;
top = node;
return top;
}
void OutPutStack(struct STACK* S) {
struct NODE* move = S->top;
while (S->bottom != move) {
printf("[姓名:%成绩:%f]->", move->name, move->score);
move = move->next;
}
printf("\n");
}
void GetTop(struct NODE* top) {
printf("[姓名:%s成绩:%f]->", top->name, top->score);
}
struct NODE* Pop(struct NODE* top) {
struct NODE* buf;
buf = top;
top = top->next;
free(buf);
buf = NULL;
return top;
};
void DestroyStack(struct STACK* S) {
while (!(StackEmty(S))) {
S->top = Pop(S->top);
}
}