This commit is contained in:
hairrrrr
2020-04-21 01:07:35 +08:00
parent 68b7b6f07a
commit 88c8eb06ca
10 changed files with 385 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
#define STACK_SIZE 100
static int contents[STACK_SIZE];
static int top = 0;
static void terminate(const char* message) {
printf("%s\n", message);
exit(EXIT_FAILURE);
}
void make_empty() {
top = 0;
}
bool is_empty() {
return top == 0;
}
bool is_full() {
return top == STACK_SIZE;
}
void push(int i) {
if (is_full())
terminate("Error in push: stack is full\n");
contents[top++] = i;
}
int pop() {
if (is_empty())
printf("Error in pop: stack is empty\n");
return contents[--top];
}

View File

@@ -0,0 +1,57 @@
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"stack.h"
typedef struct node {
int data;
struct node* next;
}node;
static node* top = NULL;
static void terminate(char* message) {
printf("%s\n", message);
exit(EXIT_FAILURE);
}
void make_empty() {
while (!is_empty())
pop();
}
bool is_empty() {
return top == NULL;
}
bool is_full() {
return false;
}
void push(int i) {
node* new_node = (node*)malloc(sizeof(node));
if (new_node == NULL) {
terminate("Error in push: stack is full.\n");
exit(EXIT_FAILURE);
}
new_node->data = i;
new_node->next = top;
top = new_node;
}
int pop() {
if (is_empty())
terminate("Error in pop: stack is empty.\n");
int data = top->data;
node* del = top;
top = top->next;
free(del);
return data;
}

View File

@@ -0,0 +1,12 @@
#ifndef STACK_H
#define STACK_H
#include<stdbool.h> //C99 only
void make_empty();
bool is_empty();
bool is_full();
void push(int i);
int pop();
#endif

View File

@@ -0,0 +1,71 @@
//#define _CRT_SECURE_NO_WARNINGS 1
//
//#include<stdio.h>
//#include<stdlib.h>
//#include"stackADT.h"
//
//#define STACK_SIZE 100
//
//typedef struct stack_type {
// int contents[STACK_SIZE];
// int top;
//}stack_type;
//
//
//
//static void terminate(char* message) {
// printf("%s\n", message);
// exit(EXIT_FAILURE);
//}
//
//Stack create() {
//
// Stack s = (Stack)malloc(sizeof(stack_type));
// if (s == NULL) {
// terminate("Error in create: stack could not be created.\n");
// exit(EXIT_FAILURE);
// }
// s->top = 0;
//
// return s;
//}
//
//
//void destory(Stack s) {
//
// free(s);
//}
//
//
//void make_empty(Stack s) {
//
// s->top = 0;
//}
//
//bool is_empty(Stack s) {
// return s->top == 0;
//}
//
//bool is_full(Stack s) {
// return s->top == STACK_SIZE;
//}
//
//void push(Stack s, int i) {
//
// if (is_full(s)) {
// terminate("Error in push: stack is full.\n");
// exit(EXIT_FAILURE);
// }
//
// s->contents[s->top++] = i;
//}
//
//int pop(Stack s) {
//
// if (is_empty(s)) {
// terminate("Error in pop: stack is empty.\n");
// exit(EXIT_FAILURE);
// }
//
// return s->contents[--s->top];
//}

View File

@@ -0,0 +1,16 @@
#ifndef STACKADT_H
#define STACKADT_H
#include<stdbool.h>
typedef struct stack_type* Stack;
Stack create();
void destory(Stack s);
void make_empty(Stack s);
bool is_empty(const Stack s);
bool is_full(const Stack s);
void push(Stack s, int i);
int pop(Stack s);
#endif

View File

@@ -0,0 +1,73 @@
//#define _CRT_SECURE_NO_WARNINGS 1
//
//#include<stdio.h>
//#include<stdlib.h>
//#include"stackADT3.h"
//
//
//typedef struct stack_type {
// int top;
// int size;
// Item contents[]; // 柔性数组
//}stack_type;
//
//
//
//static void terminate(char* message) {
// printf("%s\n", message);
// exit(EXIT_FAILURE);
//}
//
//Stack create(int size) {
//
// // sizeof(stack_type) 的大小不含有柔性数组
// Stack s = (Stack)malloc(sizeof(stack_type) + sizeof(Item) * size);
// if (s == NULL) {
// terminate("Error in create: stack could not be created.\n");
// exit(EXIT_FAILURE);
// }
// s->top = 0;
// s->size = size;
//
// return s;
//}
//
//
//void destory(Stack s) {
//
// free(s); // 柔性数组只需要释放一次
//}
//
//
//void make_empty(Stack s) {
//
// s->top = 0;
//}
//
//bool is_empty(Stack s) {
// return s->top == 0;
//}
//
//bool is_full(Stack s) {
// return s->top == s->size;
//}
//
//void push(Stack s, Item i) {
//
// if (is_full(s)) {
// terminate("Error in push: stack is full.\n");
// exit(EXIT_FAILURE);
// }
//
// s->contents[s->top++] = i;
//}
//
//Item pop(Stack s) {
//
// if (is_empty(s)) {
// terminate("Error in pop: stack is empty.\n");
// exit(EXIT_FAILURE);
// }
//
// return s->contents[--s->top];
//}

View File

@@ -0,0 +1,18 @@
#ifndef STACKADT3_H
#define STACKADT3_H
#include<stdbool.h>
typedef int Item;
typedef struct stack_type* Stack;
Stack create(int size);
void destory(Stack s);
void make_empty(Stack s);
bool is_empty(const Stack s);
bool is_full(const Stack s);
void push(Stack s, Item i);
Item pop(Stack s);
#endif

View File

@@ -0,0 +1,79 @@
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"stackADT4.h"
typedef struct node{
Item data;
struct node* next;
}node;
typedef struct stack_type {
node* top;
}stack_type;
static void terminate(char* message) {
printf("%s\n", message);
exit(EXIT_FAILURE);
}
Stack create() {
Stack s = (Stack)malloc(sizeof(stack_type));
if (s == NULL) {
terminate("Error in create: stack could not be created.\n");
exit(EXIT_FAILURE);
}
s->top = NULL;
return s;
}
void destory(Stack s) {
make_empty(s);
free(s);
}
void make_empty(Stack s) {
while (!is_empty(s))
pop(s);
}
bool is_empty(Stack s) {
return s->top == NULL;
}
bool is_full(Stack s) {
return false;
}
void push(Stack s, Item i) {
node* new_node = (node*)malloc(sizeof(node));
if (new_node == NULL) {
terminate("Error in push: stack is full.\n");
exit(EXIT_FAILURE);
}
new_node->data = i;
new_node->next = s->top;
s->top = new_node;
}
Item pop(Stack s) {
if (is_empty(s))
terminate("Error in pop: stack is empty.\n");
int data = s->top->data;
node* del = s->top;
s->top = s->top->next;
free(del);
return data;
}

View File

@@ -0,0 +1,18 @@
#ifndef STACKADT4_H
#define STACKADT4_H
#include<stdbool.h>
typedef int Item;
typedef struct stack_type* Stack;
Stack create();
void destory(Stack s);
void make_empty(Stack s);
bool is_empty(const Stack s);
bool is_full(const Stack s);
void push(Stack s, Item i);
Item pop(Stack s);
#endif

View File

@@ -0,0 +1 @@
不同的实现方式中 接口头文件和客户包含main函数的文件改变的很少