Modify the code style of C (#299)

* style(my_list): modify c codes.

* style(stack): modify c codes.

* style(hash): modify c codes.
This commit is contained in:
Reanon
2023-01-29 00:08:12 +08:00
committed by GitHub
parent 3a2a9a996c
commit da405b579d
9 changed files with 1393 additions and 178 deletions

View File

@@ -6,118 +6,98 @@
#include "../include/include.h"
#define MAX_SIZE 5000
/* 基于数组实现的栈 */
struct ArrayStack {
int *stackTop;
struct arrayStack {
int *data;
int size;
int capacity;
};
typedef struct ArrayStack ArrayStack;
typedef struct arrayStack arrayStack;
/* 内部调用 */
/* 获取栈容量 */
static int capacity(ArrayStack *stk) {
return stk->capacity;
}
/* 栈自动扩容 */
static void extendCapacity(ArrayStack *stk) {
// 先分配空间
int newCapacity = capacity(stk) * 2;
int *extend = (int *)malloc(sizeof(int) * newCapacity);
int *temp = stk->stackTop;
// 拷贝旧数据到新数据
for(int i=0; i<stk->size; i++)
extend[i] = temp[i];
// 释放旧数据
free(temp);
// 更新新数据
stk->stackTop = extend;
stk->capacity = newCapacity;
}
/* 构造函数 */
void newArrayStack(ArrayStack *stk) {
stk->capacity = 10;
stk->size = 0;
stk->stackTop = (int *)malloc(sizeof(int) * stk->capacity);
}
/* 析构函数 */
void delArrayStack(ArrayStack *stk) {
stk->capacity = 0;
stk->size = 0;
free(stk->stackTop);
arrayStack *newArrayStack() {
arrayStack *s = malloc(sizeof(arrayStack));
// 初始化一个大容量,避免扩容
s->data = malloc(sizeof(int) * MAX_SIZE);
s->size = 0;
return s;
}
/* 获取栈的长度 */
int size(ArrayStack *stk) {
return stk->size;
int size(arrayStack *s) {
return s->size;
}
/* 判断栈是否为空 */
bool empty(ArrayStack *stk) {
return size(stk) == 0;
}
/* 访问栈顶元素 */
int top(ArrayStack *stk) {
return stk->stackTop[size(stk) - 1];
bool isEmpty(arrayStack *s) {
return s->size == 0;
}
/* 入栈 */
void push(ArrayStack *stk, int num) {
if (size(stk) == capacity(stk))
extendCapacity(stk); // 需要扩容
void push(arrayStack *s, int num) {
if (s->size == MAX_SIZE) {
printf("stack is full.\n");
return;
}
s->data[s->size] = num;
s->size++;
}
stk->stackTop[size(stk)] = num;
stk->size++;
/* 访问栈顶元素 */
int peek(arrayStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
return s->data[s->size - 1];
}
/* 出栈 */
void pop(ArrayStack *stk) {
int num = stk->stackTop[size(stk) - 1];
stk->size--;
int pop(arrayStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
int val = peek(s);
s->size--;
return val;
}
/* Driver Code */
int main() {
/* 初始化栈 */
ArrayStack stack;
newArrayStack(&stack);
arrayStack * stack = newArrayStack();
/* 元素入栈 */
push(&stack, 1);
push(&stack, 3);
push(&stack, 2);
push(&stack, 5);
push(&stack, 4);
push(stack, 1);
push(stack, 3);
push(stack, 2);
push(stack, 5);
push(stack, 4);
printf("栈 stack = ");
printArray(stack.stackTop, size(&stack));
printArray(stack->data, stack->size);
/* 访问栈顶元素 */
int stackTop = top(&stack);
printf("栈顶元素 top = %d\r\n", stackTop);
int val = peek(stack);
printf("栈顶元素 top = %d\n", val);
/* 元素出栈 */
pop(&stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop);
printArray(stack.stackTop, size(&stack));
val = pop(stack);
printf("出栈元素 pop = %d出栈后 stack = ", val);
printArray(stack->data, stack->size);
/* 获取栈的长度 */
int stackSize = size(&stack);
printf("栈的长度 size = %d\r\n", stackSize);
int size = stack->size;
printf("栈的长度 size = %d\n", size);
/* 判断是否为空 */
bool isEmpty = empty(&stack);
printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no");
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");
// 释放内存
free(stack->data);
free(stack);
/* 析构函数 */
delArrayStack(&stack);
return 0;
}

View File

@@ -7,104 +7,109 @@
#include "../include/include.h"
/* 基于链表实现的栈 */
struct LinkedListStack {
ListNode* stackTop; // 将头结点作为栈顶
struct linkedListStack {
ListNode *top; // 将头结点作为栈顶
int size; // 栈的长度
};
typedef struct LinkedListStack LinkedListStack;
typedef struct linkedListStack linkedListStack;
/* 构造函数 */
void newLinkedListStack(LinkedListStack* stk) {
stk->stackTop = NULL;
stk->size = 0;
linkedListStack *newLinkedListStack() {
linkedListStack *s = malloc(sizeof(linkedListStack));
s->top = NULL;
s->size = 0;
return s;
}
/* 析构函数 */
void delLinkedListStack(LinkedListStack* stk) {
while(stk->stackTop) {
ListNode *n = stk->stackTop->next;
free(stk->stackTop);
stk->stackTop = n;
void delLinkedListStack(linkedListStack *s) {
while (s->top) {
ListNode *n = s->top->next;
free(s->top);
s->top = n;
}
stk->size = 0;
free(s);
}
/* 获取栈的长度 */
int size(LinkedListStack* stk) {
assert(stk);
return stk->size;
int size(linkedListStack *s) {
assert(s);
return s->size;
}
/* 判断栈是否为空 */
bool empty(LinkedListStack* stk) {
assert(stk);
return size(stk) == 0;
bool isEmpty(linkedListStack *s) {
assert(s);
return size(s) == 0;
}
/* 访问栈顶元素 */
int top(LinkedListStack* stk) {
assert(stk);
assert(size(stk) != 0);
return stk->stackTop->val;
int peek(linkedListStack *s) {
assert(s);
assert(size(s) != 0);
return s->top->val;
}
/* 入栈 */
void push(LinkedListStack* stk, int num) {
assert(stk);
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
node->next = stk->stackTop; // 更新新加结点指针域
node->val = num; // 更新新加结点数据域
stk->stackTop = node; // 更新栈顶
stk->size++; // 更新栈大小
void push(linkedListStack *s, int num) {
assert(s);
ListNode *node = (ListNode *) malloc(sizeof(ListNode));
node->next = s->top; // 更新新加结点指针域
node->val = num; // 更新新加结点数据域
s->top = node; // 更新栈顶
s->size++; // 更新栈大小
}
/* 出栈 */
void pop(LinkedListStack* stk) {
assert(stk);
int num = top(stk);
ListNode *tmp = stk->stackTop;
stk->stackTop = stk->stackTop->next;
int pop(linkedListStack *s) {
if (s->size == 0) {
printf("stack is empty.\n");
return NIL;
}
assert(s);
int val = peek(s);
ListNode *tmp = s->top;
s->top = s->top->next;
// 释放内存
free(tmp);
stk->size--;
s->size--;
return val;
}
/* Driver Code */
int main() {
/* 初始化栈 */
LinkedListStack stack;
/* 构造函数 */
newLinkedListStack(&stack);
// 构造函数
linkedListStack *stack = newLinkedListStack();
/* 元素入栈 */
push(&stack, 1);
push(&stack, 3);
push(&stack, 2);
push(&stack, 5);
push(&stack, 4);
push(stack, 1);
push(stack, 3);
push(stack, 2);
push(stack, 5);
push(stack, 4);
printf("栈 stack = ");
printLinkedList(stack.stackTop);
printLinkedList(stack->top);
/* 访问栈顶元素 */
int stackTop = top(&stack);
printf("栈顶元素 top = %d\r\n", stackTop);
int val = peek(stack);
printf("栈顶元素 top = %d\r\n", val);
/* 元素出栈 */
pop(&stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", stackTop);
printLinkedList(stack.stackTop);
val = pop(stack);
printf("出栈元素 pop = %d, 出栈后 stack = ", val);
printLinkedList(stack->top);
/* 获取栈的长度 */
int stackSize = size(&stack);
printf("栈的长度 size = %d\r\n", stackSize);
printf("栈的长度 size = %d\n", size(stack));
/* 判断是否为空 */
bool isEmpty = empty(&stack);
printf("栈是否为空 = %s\r\n", isEmpty ? "yes" : "no");
bool empty = isEmpty(stack);
printf("栈是否为空 = %s\n", empty ? "true" : "false");
/* 析构函数 */
delLinkedListStack(&stack);
delLinkedListStack(stack);
return 0;
}