Stack viết bởi C. Các bạn có thể thấy C không có class, object do đó các hàm tác động lên stack (ta phải truyền stack như là tham số vào), chứ không phải là method của Stack.
Khi khởi tạo stack cần truyền vào kích thước ban đầu của Stack
struct Stack* s = createStack(5);
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Stack {
int top;
int capacity;
int *array;
};
struct Stack* createStack(int capacity) {
struct Stack* s = malloc(sizeof(struct Stack));
if (!s) {
return NULL;
}
s->capacity = capacity;
s->top = -1;
s->array = (int*) malloc(s->capacity * sizeof(int));
if (!s->array) {
return NULL;
}
return s;
}
bool isStackEmpty(struct Stack* s) {
return (s->top == -1);
}
bool isStackFull(struct Stack* s) {
return (s->top == s->capacity -1);
}
bool push(struct Stack* s, int data) {
if (isStackFull(s)) {
printf("Stack is over flow\n");
return false;
} else {
s->array[++s->top] = data;
return true;
}
}
bool pop(struct Stack*s, int* data) {
if (isStackEmpty(s)) {
printf("Stack is empty");
return false;
} else {
*data = s->array[s->top--];
return true;
}
}
void deleteStack(struct Stack*s) {
if (s) {
if (s->array) {
free(s->array);
}
free(s);
}
}
int main() {
struct Stack* s = createStack(5);
push(s, 10);
push(s, 12);
push(s, 15);
push(s, 2);
push(s, 3);
push(s, 5);
while (!isStackEmpty(s)) {
int a;
if (pop(s, &a)) {
printf("%d\n", a);
}
}
deleteStack(s);
return 0;
}
Chuyển sang cách khai báo sử dụng typedef
chúng ta sẽ không phải viết lại từ khoá struct mỗi khi dùng
typedef struct {
int top;
int capacity;
int *array;
} Stack;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct {
int top;
int capacity;
int *array;
} Stack;
Stack* createStack(int capacity) {
Stack* s = malloc(sizeof(Stack));
if (!s) {
return NULL;
}
s->capacity = capacity;
s->top = -1;
s->array = (int*) malloc(s->capacity * sizeof(int));
if (!s->array) {
return NULL;
}
return s;
}
bool isStackEmpty(Stack* s) {
return (s->top == -1);
}
bool isStackFull(Stack* s) {
return (s->top == s->capacity -1);
}
bool push(Stack* s, int data) {
if (isStackFull(s)) {
printf("Stack is over flow\n");
return false;
} else {
s->array[++s->top] = data;
return true;
}
}
bool pop(Stack*s, int* data) {
if (isStackEmpty(s)) {
printf("Stack is empty");
return false;
} else {
*data = s->array[s->top--];
return true;
}
}
void deleteStack(Stack*s) {
if (s) {
if (s->array) {
free(s->array);
}
free(s);
}
}
int main() {
Stack* s = createStack(5);
return 0;
}
Chuyển sang khai báo Stack trong file .h, .c riêng
Stack.h
#ifndef BASICC_STACK_H
#define BASICC_STACK_H
#include <stdbool.h>
typedef struct {
int top;
int capacity;
int *array;
} Stack;
Stack *createStack(int capacity);
bool isStackEmpty(Stack *s);
bool isStackFull(Stack *s);
bool push(Stack *s, int data);
bool pop(Stack *s, int *data);
void deleteStack(Stack *s);
#endif //BASICC_STACK_H
Stack.c
#include <stdio.h>
#include <stdlib.h>
#include "Stack.h"
Stack *createStack(int capacity) {
Stack *s = malloc(sizeof(Stack));
if (!s) {
return NULL;
}
s->capacity = capacity;
s->top = -1;
s->array = (int *) malloc(s->capacity * sizeof(int));
if (!s->array) {
return NULL;
}
return s;
}
bool isStackEmpty(Stack *s) {
return (s->top == -1);
}
bool isStackFull(Stack *s) {
return (s->top == s->capacity - 1);
}
bool push(Stack *s, int data) {
if (isStackFull(s)) {
printf("Stack is over flow\n");
return false;
} else {
s->array[++s->top] = data;
return true;
}
}
bool pop(Stack *s, int *data) {
if (isStackEmpty(s)) {
printf("Stack is empty");
return false;
} else {
*data = s->array[s->top--];
return true;
}
}
void deleteStack(Stack *s) {
if (s) {
if (s->array) {
free(s->array);
}
free(s);
}
}
main.c
#include <stdio.h>
#include "Stack.h"
int main() {
Stack* s = createStack(40);
push(s, 10);
push(s, 12);
push(s, 15);
push(s, 2);
push(s, 3);
push(s, 5);
while (!isStackEmpty(s)) {
int a;
if (pop(s, &a)) {
printf("%d\n", a);
}
}
deleteStack(s);
return 0;
}