#include "stdio.h"
#include "stdlib.h"
struct stack_node
{
int num;
struct stack_node *next;
};
typedef struct stack_node stack_list;
typedef stack_list *link;
link stack1 = NULL;
link stack2 = NULL;
/*--------------------------*/
/*栈数据的存入 */
/*--------------------------*/
link push(link stack,int value)
{
link new_node;
new_node = (link)malloc(sizeof(stack_list));
if(!new_node)
{
printf("内存分配失败!\n");
return NULL;
}
new_node->num = value;
new_node->next = stack;
stack = new_node;
return stack;
}
/*--------------------------*/
/*栈数据的取出 */
/*--------------------------*/
link pop(link stack,int *value)
{
link top;
if (stack != NULL)
{
top = stack;
stack = stack->next;
*value = top->num;
free(top);
return stack;
}
else
*value = -1;
}
int empty(link stack)
{
if (stack == NULL)
return 1;
else
return 0;
}
void main()
{
int list1[6] = {1,2,3,4,5,6};
int list2[6] = {6,5,4,3,2,1};
int i,temp;
for(i=0;i<6;i++)
{
stack1 = push(stack1,list1[i]);
stack2 = push(stack2,list2[i]);
}
printf("原来的数组顺序(1):");
for (i=0;i<6;i++)
printf("[%d]",list1[i]);
printf("\n");
printf("取出的数组顺序(1):");
while(!empty(stack1))
{
stack1 = pop(stack1,&temp);
printf("[%d]",temp);
}
printf("\n");
printf("原来的数组顺序(2):");
for (i=0;i<6;i++)
printf("[%d]",list2[i]);
printf("\n");
printf("取出的数组顺序(2):");
while(!empty(stack2))
{
stack2 = pop(stack2,&temp);
printf("[%d]",temp);
}
printf("\n");
}
#include "stdlib.h"
struct stack_node
{
int num;
struct stack_node *next;
};
typedef struct stack_node stack_list;
typedef stack_list *link;
link stack1 = NULL;
link stack2 = NULL;
/*--------------------------*/
/*栈数据的存入 */
/*--------------------------*/
link push(link stack,int value)
{
link new_node;
new_node = (link)malloc(sizeof(stack_list));
if(!new_node)
{
printf("内存分配失败!\n");
return NULL;
}
new_node->num = value;
new_node->next = stack;
stack = new_node;
return stack;
}
/*--------------------------*/
/*栈数据的取出 */
/*--------------------------*/
link pop(link stack,int *value)
{
link top;
if (stack != NULL)
{
top = stack;
stack = stack->next;
*value = top->num;
free(top);
return stack;
}
else
*value = -1;
}
int empty(link stack)
{
if (stack == NULL)
return 1;
else
return 0;
}
void main()
{
int list1[6] = {1,2,3,4,5,6};
int list2[6] = {6,5,4,3,2,1};
int i,temp;
for(i=0;i<6;i++)
{
stack1 = push(stack1,list1[i]);
stack2 = push(stack2,list2[i]);
}
printf("原来的数组顺序(1):");
for (i=0;i<6;i++)
printf("[%d]",list1[i]);
printf("\n");
printf("取出的数组顺序(1):");
while(!empty(stack1))
{
stack1 = pop(stack1,&temp);
printf("[%d]",temp);
}
printf("\n");
printf("原来的数组顺序(2):");
for (i=0;i<6;i++)
printf("[%d]",list2[i]);
printf("\n");
printf("取出的数组顺序(2):");
while(!empty(stack2))
{
stack2 = pop(stack2,&temp);
printf("[%d]",temp);
}
printf("\n");
}