C源碼
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct _NODE
{
int data;
struct _NODE *next;
}Stack_node;
typedef struct _STACK
{
Stack_node *top;
}Link_stack;
#define SIZE_OF_NODE sizeof(struct _NODE)
#define SIZE_OF_STACK sizeof(struct _STACK)
void Init_stack(Link_stack **LS);
int Is_empty(Link_stack *LS);
void Push_stack(Link_stack *LS, int key);
int Pop_stack(Link_stack *LS);
void Print_stack(Link_stack *LS);
int Check_success(int array_in[], int array_out[], int size);
int Check_servive(Link_stack *LS, int key, int current);
int main()
{
Link_stack *LS = NULL;
int array[] = {1, 2, 3, 4, 5};
//int check[] = {1, 4, 5, 3, 2};
//int check[] = {4, 3, 5, 1, 2};
int check[] = {3, 5, 4, 2, 1 };
int len = 0;
int i = 0;
int Is_success = 0;
len = sizeof(array) / sizeof(int);
Is_success = Check_success(array, check, len);
if(1 == Is_success )
{
printf("The check array is the pop squence!!/n");
}
else
{
printf("The check array is not the pop squence!!/n");
}
return 0;
}
void Init_stack(Link_stack **LS)
{
(*LS) = (Link_stack *) malloc(SIZE_OF_STACK);
if((*LS) == NULL)
{
printf("Memory assign failure!!/n");
exit(1);
}
(*LS)->top = NULL;
}
int Is_empty(Link_stack *LS)
{
if(LS->top == NULL)
{
return 1;
}
else
{
return 0;
}
}
void Push_stack(Link_stack *LS, int key)
{
Stack_node *node = NULL;
node = (Stack_node *)malloc(SIZE_OF_NODE);
if(node == NULL)
{
printf("Memory assign failure!!/n");
exit(1);
}
else
{
node->data = key;
node->next = LS->top;
LS->top = node;
LS->top->data = node->data;
}
}
int Pop_stack(Link_stack *LS)
{
Stack_node *node = NULL;
int key = 0;
if( ! Is_empty(LS))
{
node = LS->top;
LS->top = LS->top->next;
key = node->data;
free(node);
node = NULL;
return key;
}
else
{
printf("The stack is empty!!/n");
exit(1);
}
}
void Print_stack(Link_stack *LS)
{
Stack_node *node = NULL;
int count = 1;
node = LS->top;
while(node != NULL)
{
printf("The %d node's data is : %d/n", count, node->data);
count++;
node = node->next;
}
}
int Check_servive(Link_stack *LS, int key, int current) //檢查棧中是否已存在關鍵字key
{
Stack_node *node = NULL;
int num = 0;
node = LS->top;
while(node != NULL)
{
if(LS->top->data == current) //key 是當前棧頂,出棧
{
num = Pop_stack(LS);
return 0;
}
else if(node->data == key )
{
return 0;
}
node = node->next;
}
return 1;
}
int Check_success(int array_in[], int array_out[], int size)
{
//先讓輸出序列逆序壓棧
int i = 0, j = 0;
int outkey = 0;
int t = 0;
Link_stack *Out_stack = NULL;
Link_stack *In_stack = NULL;
Init_stack(&In_stack);
Init_stack(&Out_stack);
for(i = size - 1; i >= 0; --i)
{
Push_stack(Out_stack, array_out[i]);
}
while(! Is_empty(Out_stack))
{
outkey = Pop_stack(Out_stack);
//z在輸入序列中尋找
for(i = 0; i < size; ++i)
{
if(array_in[i] == outkey)
{
break;
}
}
if(i == size) //沒找到
{
return 0;
}
else
{
t = array_in[i];
array_in[i] = -1;//表示已找到用-1標記
for(j = 0; j <= i; ++j)
{
if(array_in[j] != -1 )
{
if( Check_servive(In_stack, array_in[j], outkey)) //檢查棧中是否已存在關鍵字key
{
Push_stack(In_stack, array_in[j]);
}
}
if(i == j) //入棧頂元素 是否為當前出棧的棧頂元素
{
Check_servive(In_stack, t , outkey);
}
}
}
}
if( Is_empty(In_stack) )
{
return 1; //成功
}
else
{
return 0;
}
}
/*
對於: int check[] = {1, 4, 5, 3, 2};
The check array is the pop squence!!
Press any key to continue
對於: int check[] = {4, 3, 5, 1, 2};
The check array is not the pop squence!!
Press any key to continue
對於:int check[] = {3, 5, 4, 2, 1 };
The check array is the pop squence!!
Press any key to continue
*/