[C语言]C语言实现栈(基于单向链表)

#include <stdio.h>
#include <stdlib.h>
typedef int DataType;

typedef struct node
{
    DataType data;
    struct node *next; 
}StackElement;

typedef struct Stack_t
{
    int size;
    StackElement *top; 
}Stack;

int stack_Init(Stack **);
int stack_Destory(Stack **); 
int stack_Pop(Stack*,DataType*);
int stack_Push(Stack*,DataType);
int stack_Size(Stack *);
// #define stack_Size(stack) ((stack)->size)

int main()
{
    Stack *mystack = NULL;
    DataType mydata;
    int i;
    printf("mystack is %x\n",mystack);
    printf("size is %d\n",stack_Size(mystack));
    stack_Init(&mystack);
    printf("mystack  Init!\n");
    printf("mystack is %x\n",mystack);
    printf("size is %d\n",stack_Size(mystack));
    for(i=0;i<10;i++)
    {
        stack_Push(mystack,i);
    }
    printf("size is %d\n",stack_Size(mystack));
    while(stack_Size(mystack) > 2)
    {
        stack_Pop(mystack,&mydata);
        printf("pop data is %d\n",mydata);
    }
    printf("mystack is %x\n",mystack);
    printf("size is %d\n",stack_Size(mystack));
    stack_Destory(&mystack);
    printf("mystack  Destory!\n");
    printf("mystack is %x\n",mystack);
    printf("size is %d\n",stack_Size(mystack));
    return 0;
}

/*************************************************
Function: 		stack_Init
Description: 	初始化栈
Input: 			stack:栈二级指针
Output: 		
Return:         成功0,失败-1
Other:          复杂度O(1)		
*************************************************/
int stack_Init(Stack **stack)
{
    Stack *new_stack = NULL;
    if((new_stack = (Stack*)malloc(sizeof(Stack))) == NULL)
    {
        return -1;
    }
    new_stack->top = NULL;
    new_stack->size = 0;
    *stack = new_stack;
    return 0;
}
/*************************************************
Function: 		stack_Destory
Description: 	销毁栈
Input: 			stack:栈二级指针
Output: 		
Return:         成功返回销毁结点数,失败-1
Other:          复杂度O(n)		
*************************************************/
int stack_Destory(Stack **stack)
{
    int count = 0;
    DataType data;
    if(*stack == NULL)
    {
        return -1;
    }
    count = (*stack)->size;
    while((*stack)->size > 0)
    {
        stack_Pop(*stack,&data);
        free((*stack)->top);
    }
    free(*stack);
    *stack = NULL;
    return count;
}
/*************************************************
Function: 		stack_Pop
Description: 	出栈
Input: 			stack:栈指针
                data:出栈结点的数据
Output: 		
Return:         成功0,失败-1
Other:          复杂度O(1)		
*************************************************/
int stack_Pop(Stack *stack,DataType *data)
{
    if(stack == NULL || stack->size == 0)
    {
        return -1;
    }
    stack->size--;
    *data = stack->top->data;
    stack->top = stack->top->next;
    return 0;
}
/*************************************************
Function: 		stack_Push
Description: 	入栈
Input: 			stack:栈指针
                data:入栈结点的数据
Output: 		
Return:         成功0,失败-1
Other:          复杂度O(1)		
*************************************************/
int stack_Push(Stack *stack,DataType data)
{
    StackElement *newElmt = NULL;
    if(stack == NULL || (newElmt = (StackElement*)malloc(sizeof(StackElement))) == NULL)
    {
        return -1;
    }
    newElmt->data = data;
    newElmt->next = stack->top;
    stack->size++;
    stack->top = newElmt;
    return 0;
}
/*************************************************
Function: 		stack_Size
Description: 	栈的
Input: 			stack:栈指针
                data:入栈结点的数据
Output: 		
Return:         成功0,失败-1
Other:          复杂度O(1)		
*************************************************/
int stack_Size(Stack *stack)
{
    if(stack == NULL)
    {
        return -1;
    }
    return stack->size;
}

运行结果

运行结果

猜你喜欢

转载自blog.csdn.net/weixin_43727672/article/details/104755888