其他分享
首页 > 其他分享> > (20220220)链栈

(20220220)链栈

作者:互联网

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define SIZE 10

enum ret_val
{
    MALLOC_OK = 100,
    MALLOC_NO,
    CREATE_OK,
    CREATE_NO,
    TAKE_OK,
    TAKE_NO,
    EMPTY_OK,
    EMPTY_NO,
    PUSH_OK,
    PUSH_NO,
    POP_OK,
    POP_NO,
};

typedef struct stack_node
{
    int num;
    char name[SIZE];

    struct stack_node *next;
}*Stack;

typedef struct node
{
    int num;
    char name[SIZE];
}Node;

int is_malloc_ok(Stack stack)   //判断内存分配
{
    if(NULL == stack)
    {
	return MALLOC_NO;
    }
    return MALLOC_OK;
}


int create_node(Stack *new_node)   //创建节点
{
    *new_node = (Stack)malloc(sizeof(struct stack_node));

    if(MALLOC_OK == is_malloc_ok(*new_node))
    {
	return CREATE_OK;
    }
    return CREATE_NO;
}

int create_link_stack(Stack *stack)  //创建链表
{
    if(CREATE_OK == create_node(stack))
    {
	(*stack)->next = NULL;
	return CREATE_OK;
    }    
    return CREATE_NO;
}

int push_link_stack(Stack stack, Node node)  //进栈
{
    Stack new_node = NULL;

    if(CREATE_NO == create_node(&new_node))
    {
	return PUSH_NO;
    }
     
    new_node->num = node.num;
    strcpy(new_node->name, node.name);

    new_node->next = stack->next;
    stack->next = new_node;
    return PUSH_OK;

}

void make_empty(Stack stack)     //置空
{
    Stack p = NULL;
    p = stack->next;

    while(p != NULL)
    {
	stack->next = p->next;
	free(p);
	p = stack->next;
    }  

}

int is_empty(Stack stack)   //判断是否为空
{
    if(NULL == stack->next)
    {
	printf("stack is NULL!\n");
	return EMPTY_OK;
    }
    return EMPTY_NO;
}

int pop_link_stack(Stack stack, Node *node)  //出栈
{
    Stack p = NULL;
    p = stack->next;
    
    if(EMPTY_OK == is_empty(stack))
    {
	return POP_NO;
    }

    (*node).num = p->num;
    strcpy((*node).name, p->name);

    stack->next = p->next;
    free(p);

    return POP_OK;

}

int take_top(Stack stack, Node *node)  //取栈顶
{
    Stack p = NULL;
    p = stack->next;
    
    if(EMPTY_OK == is_empty(stack))
    {
	return TAKE_NO;
    }

    (*node).num = p->num;
    strcpy((*node).name, p->name);

    return TAKE_OK;
    
}
void release_stack(Stack *stack)  //释放
{
    make_empty(*stack);
    free(*stack);
    *stack = NULL;
}

void display(Stack stack)   //遍历
{
    Stack p = NULL;
    
    if(stack == NULL)
    {
	printf("stack is no exit!\n");
    }
    else if(stack->next == NULL)
    {
	printf("stack is NULL!\n");
    }
    else
    {
	p = stack->next;
	while(p != NULL)
	{
            printf("%d\n", p->num);
	    p = p->next;
	}
    }
}

int main()
{
    Stack stack = NULL;
    Node node;
    int i;

    if(CREATE_NO == create_link_stack(&stack)) //创建链表
    {
	return 0;
    }
    
    printf("please input name:\n");
    for(i = 0; i < 5; i++)
    {
	node.num = i + 1;
        scanf("%s", node.name);	
        if(PUSH_OK == push_link_stack(stack, node))  //进栈
	{
	    printf("push stack is success!\n");
	}
	else
	{
	    printf("push stack is fail!\n");
	}
    }

   // make_empty(stack);     //置空
    
#if 0    
    printf("stack pop is:\n");
    for(i = 0; i < 5; i++)
    {
        if(POP_OK == pop_link_stack(stack, &node))  //出栈
	{
	    printf("%3d", node.num);
	    printf("%3s\n", node.name);
	}
	else
	{
	    printf("pop stack is fail!\n");
	}
    }

#endif

#if 1
    printf("stack pop is:\n");
    if(TAKE_OK == take_top(stack, &node))  //取栈顶元素
    {
        printf("%3d", node.num);
	printf("%3s\n", node.name);
    }
    else
    {
	printf("pop stack is fail!\n");
    }

#endif


    release_stack(&stack);  //释放
    display(stack);   //遍历

    return 0;
}

标签:node,NULL,OK,20220220,Stack,链栈,next,stack
来源: https://blog.csdn.net/weixin_53812805/article/details/123032805