栈 c实现

时间:2023-03-09 14:41:16
栈 c实现

栈的数组实现

stack.h

#ifndef _STACK_
#define _STACK_ #define SIZE 100 typedef int data_t; typedef struct head{
data_t data[SIZE];
int top;
}stack_t; stack_t *stack_create(); int stack_is_empty(stack_t *head);
int stack_is_full(stack_t *head);
void stack_clear(stack_t *head); int stack_push(stack_t *head, data_t data);
data_t stack_pop(stack_t *head);
data_t stack_get_top(stack_t *head); void stack_show(stack_t *head);
void stack_destory(stack_t **head); #endif

stack.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "sqstack.h" stack_t *stack_create()
{
stack_t *head = (stack_t *)malloc(sizeof(stack_t));
if (head == NULL) {
printf("malloc failed!\n");
return NULL;
}
bzero(head, sizeof(stack_t)); head->top = -; return head;
} int stack_is_empty(stack_t *head)
{
return head->top == -;
} int stack_is_full(stack_t *head)
{
return head->top == SIZE - ;
} void stack_clear(stack_t *head)
{
head->top = -;
} int stack_push(stack_t *head, data_t data)
{
if (stack_is_full(head)) {
printf("stack is full!\n");
return -;
} head->data[head->top+] = data;
head->top++; return ;
} data_t stack_pop(stack_t *head)
{
if (stack_is_empty(head)) {
printf("stack is empty!\n");
return -;
} data_t data = head->data[head->top];
head->top--; return data;
} data_t stack_get_top(stack_t *head)
{
if (stack_is_empty(head)) {
printf("stack is empty!\n");
return -;
} return head->data[head->top];
} void stack_show(stack_t *head)
{
int i;
for (i = head->top; i >= ; i--) {
printf("%d, ", head->data[i]);
}
printf("\n");
} void stack_destory(stack_t **head)
{
free(*head);
*head = NULL;
}

main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "sqstack.h" int main()
{
stack_t *head = stack_create(); int n = ;
while (n--) {
stack_push(head, n+);
}
stack_show(head); int i;
for (i = ; i < ; i++) {
printf("%d, ", stack_get_top(head));
printf("%d\n", stack_pop(head));
}
stack_show(head); stack_destory(&head); return ;
}

栈的链表实现

lstack.h

#ifndef _STACK_
#define _STACK_ typedef int data_t; typedef struct node{
data_t data;
struct node *next;
}NODE; NODE *stack_create(); int stack_is_empty(NODE *head);
int stack_is_full(NODE *head); int stack_length(NODE *head);
void stack_clear(NODE *head); int stack_push(NODE *head, data_t data);
data_t stack_pop(NODE *head);
data_t stack_get_top(NODE *head); void stack_show(NODE *head);
void stack_destory(NODE **head); #endif

lstack.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "lstack.h" NODE *stack_create()
{
NODE *head = (NODE *)malloc(sizeof(NODE));
if (NULL == head) {
printf("malloc failed!\n");
return NULL;
}
bzero(head, sizeof(NODE)); head->data = -;
head->next = NULL; return head;
} int stack_is_empty(NODE *head)
{
return head->next == NULL;
} int stack_is_full(NODE *head)
{
return ;
} int stack_length(NODE *head)
{
NODE *p = head->next;
int len = ; while (NULL != p) {
len++;
p = p->next;
} return len;
} void stack_clear(NODE *head)
{
NODE *p = head->next;
NODE *q = NULL; while (NULL != p) {
q = p->next;
free(p);
p = q;
} head->next = NULL;
} int stack_push(NODE *head, data_t data)
{
NODE *p = head;
NODE *q = (NODE *)malloc(sizeof(NODE));
if (NULL == q) {
printf("malloc failed!\n");
return -;
}
q->data = data;
q->next = NULL; q->next = p->next;
p->next = q; return ;
} data_t stack_pop(NODE *head)
{
if (stack_is_empty(head)) {
printf("list is empty!\n");
return -;
} data_t data = head->next->data; NODE *p = head;
NODE *q = head->next;
p->next = q->next;
free(q);
q = NULL; return data;
} data_t stack_get_top(NODE *head)
{
if (stack_is_empty(head)) {
printf("list is empty!\n");
return -;
} return head->next->data;
} void stack_show(NODE *head)
{
NODE *p = head->next; while (NULL != p) {
printf("%d, ", p->data);
p = p->next;
}
printf("\n");
} void stack_destory(NODE **head)
{
stack_clear(*head); free(*head);
*head = NULL;
}

main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h> #include "lstack.h" int main()
{
NODE *head = stack_create(); if (NULL == head) {
printf("create failed!\n");
return -;
} int n = ;
while (n--) {
if (- == stack_push(head, n+)) {
printf("insert failed!\n");
break;
}
} stack_show(head); int i;
for (i = ; i < ; i++) {
printf("%d, ", stack_get_top(head));
printf("%d\n", stack_pop(head));
} stack_destory(&head); return ;
}