week 3 comment and destory function
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Author: Walter
|
||||||
|
* Student ID: 1930006025
|
||||||
|
* Week_3
|
||||||
|
* Stack
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -6,6 +13,7 @@
|
|||||||
|
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
|
/* test function */
|
||||||
int main() {
|
int main() {
|
||||||
Stack stack;
|
Stack stack;
|
||||||
double val;
|
double val;
|
||||||
@@ -31,13 +39,16 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool CreateStack(Stack *stack, int size) {
|
bool CreateStack(Stack *stack, int size) {
|
||||||
|
/* check */
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
|
|
||||||
|
/* memory allocate */
|
||||||
stack->data = (double *)malloc(sizeof(double) * size);
|
stack->data = (double *)malloc(sizeof(double) * size);
|
||||||
if (stack->data == NULL) {
|
if (stack->data == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* init attribute */
|
||||||
stack->top = -1;
|
stack->top = -1;
|
||||||
stack->size = size;
|
stack->size = size;
|
||||||
|
|
||||||
@@ -45,33 +56,46 @@ bool CreateStack(Stack *stack, int size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool IsEmpty(Stack *stack) {
|
bool IsEmpty(Stack *stack) {
|
||||||
|
/* if empty, stack->top = -1 */
|
||||||
return stack->top < 0;
|
return stack->top < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsFull(Stack *stack) {
|
bool IsFull(Stack *stack) {
|
||||||
|
/* if full, stack->top = stack->size */
|
||||||
return stack->top >= stack->size;
|
return stack->top >= stack->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Top(Stack *stack, double *x) {
|
bool Top(Stack *stack, double *x) {
|
||||||
|
/* check empty */
|
||||||
if (IsEmpty(stack)) {
|
if (IsEmpty(stack)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get top */
|
||||||
*x = stack->data[stack->top];
|
*x = stack->data[stack->top];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Push(Stack *stack, double x) {
|
bool Push(Stack *stack, double x) {
|
||||||
|
/* check */
|
||||||
if (IsFull(stack)) {
|
if (IsFull(stack)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* push */
|
||||||
stack->data[++stack->top] = x;
|
stack->data[++stack->top] = x;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Pop(Stack *stack, double *x) {
|
bool Pop(Stack *stack, double *x) {
|
||||||
|
/* check */
|
||||||
if (IsEmpty(stack)) {
|
if (IsEmpty(stack)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get x, and top - 1 */
|
||||||
*x = stack->data[stack->top--];
|
*x = stack->data[stack->top--];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -79,8 +103,10 @@ bool Pop(Stack *stack, double *x) {
|
|||||||
void DisplayStack(Stack *stack) {
|
void DisplayStack(Stack *stack) {
|
||||||
double *top_p = stack->data + stack->top;
|
double *top_p = stack->data + stack->top;
|
||||||
|
|
||||||
|
/* print the top pointer hint */
|
||||||
printf("top --> ");
|
printf("top --> ");
|
||||||
|
|
||||||
|
/* loop for stack data */
|
||||||
for (; top_p >= stack->data; top_p--) {
|
for (; top_p >= stack->data; top_p--) {
|
||||||
|
|
||||||
/* print top --> hint */
|
/* print top --> hint */
|
||||||
@@ -100,4 +126,7 @@ void DisplayStack(Stack *stack) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DestroyStack(Stack *stack) {
|
void DestroyStack(Stack *stack) {
|
||||||
|
free(stack->data);
|
||||||
|
stack->size = 0;
|
||||||
|
stack->top = -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user