#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stack_data{
int num;
char name[20];
};
struct stack_node{
int num;
char name[20];
struct stack_node *next;
};
typedef struct stack_node *Stack;
typedef struct stack_data Stack_data;
enum res_val{malloc_no=100,malloc_ok,create_stack_ok,create_stack_no,push_ok,push_no,pop_ok,pop_no,
empty_ok,empty_no
};
int create_node(Stack *new_node)
{
*new_node = (Stack)malloc(sizeof(struct stack_node));
if(*new_node == NULL)
{
return malloc_no;
}
return malloc_ok;
}
int create_stack(Stack *stack)
{
if(create_node(stack) == malloc_ok)
{
(*stack)->next = NULL;
return create_stack_ok;
}
else
{
return create_stack_no;
}
}
int push(Stack stack,Stack_data node_data)
{
Stack new_node = NULL;
if(create_node(&new_node) == malloc_ok)
{
new_node->num=node_data.num;
strcpy(new_node->name,node_data.name);
new_node->next=stack->next;
stack->next = new_node;
return push_ok;
}
else
{
return push_no;
}
}
int is_empty(Stack stack)
{
if(stack->next==NULL)
{
printf("the stack is empty!\n");
return empty_ok;
}
else
{
return empty_no;
}
}
int pop_stack(Stack stack,Stack_data node_data)
{
Stack p=stack->next;
if(is_empty(stack) == empty_ok)
{
return pop_no;
}
else
{
node_data.num=p->num;
strcpy(node_data.name,p->name);
stack->next=p->next;
free(p);
p=NULL;
return pop_ok;
}
}
int main()
{
Stack stack=NULL;
Stack_data node_data;
if(create_stack(&stack)==create_stack_ok)
{
printf("create stack success\n");
}
else
{
printf("create stack fail");
}
node_data.num=1;
strcpy(node_data.name,"werqwr");
push(stack,node_data);
return 0;
}#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stack_data{
int num;
char name[20];
};
struct stack_node{
int num;
char name[20];
struct stack_node *next;
};
typedef struct stack_node *Stack;
typedef struct stack_data Stack_data;
enum res_val{malloc_no=100,malloc_ok,create_stack_ok,create_stack_no,push_ok,push_no,pop_ok,pop_no,
empty_ok,empty_no
};
int create_node(Stack *new_node)
{
*new_node = (Stack)malloc(sizeof(struct stack_node));
if(*new_node == NULL)
{
return malloc_no;
}
return malloc_ok;
}
int create_stack(Stack *stack)
{
if(create_node(stack) == malloc_ok)
{
(*stack)->next = NULL;
return create_stack_ok;
}
else
{
return create_stack_no;
}
}
int push(Stack stack,Stack_data node_data)
{
Stack new_node = NULL;
if(create_node(&new_node) == malloc_ok)
{
new_node->num=node_data.num;
strcpy(new_node->name,node_data.name);
new_node->next=stack->next;
stack->next = new_node;
return push_ok;
}
else
{
return push_no;
}
}
int is_empty(Stack stack)
{
if(stack->next==NULL)
{
printf("the stack is empty!\n");
return empty_ok;
}
else
{
return empty_no;
}
}
int pop_stack(Stack stack,Stack_data node_data)
{
Stack p=stack->next;
if(is_empty(stack) == empty_ok)
{
return pop_no;
}
else
{
node_data.num=p->num;
strcpy(node_data.name,p->name);
stack->next=p->next;
free(p);
p=NULL;
return pop_ok;
}
}
int main()
{
Stack stack=NULL;
Stack_data node_data;
if(create_stack(&stack)==create_stack_ok)
{
printf("create stack success\n");
}
else
{
printf("create stack fail");
}
node_data.num=1;
strcpy(node_data.name,"werqwr");
push(stack,node_data);
return 0;
}
(C)链栈的基本操作
最新推荐文章于 2024-07-22 20:42:27 发布