微软等面试100题系列 29题解答 望各位同行指导
来源:互联网 发布:我的世界怎么玩 知乎 编辑:程序博客网 时间:2024/06/05 00:43
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
*/
- 微软等面试100题系列 29题解答 望各位同行指导
- 微软等面试100题系列 - 41
- 微软等数据结构+算法面试100题解答018
- 微软等面试100题系列--(1-20)
- 微软等面试100题系列--(21-40)
- 微软等面试100题系列--(41-60)
- 微软等面试100题系列--(61-80)
- 微软等面试100题系列 - 45(2)
- 微软等面试100题系列 - 45(3)
- 微软等100题系列
- 微软等面试100题第一题
- 微软等公司数据结构+算法面试100题系列(61——100题)
- 微软等面试经典100题
- 微软等数据结构+算法面试100题
- 微软等面试100题答案精选
- 微软等面试100题答案精选
- 微软等数据结构+算法面试 100 题
- 微软等数据结构+算法面试100题
- Ubuntu - 历史与发展过程
- jquery.validate验证
- 入门WP7 - 2.Windows Phone 7的开发人员训练包之Hello Windows Phone
- 注重实效的程序员(The Pragmatic Programmer)
- PlentyOfFish.com .NET网站的又一传奇
- 微软等面试100题系列 29题解答 望各位同行指导
- 生命的礼赞
- 欢迎大家来访
- heap的分析
- boj 1328 简单问题 不过要考虑输入大小 long long可以 不过VC++不能编译
- 2012的全球灾难 已被证实希望这不是真的就看半个月后唐山会不会地震了【不晓得是不是真的】
- 加密与解密
- 计算机二级考试中的几道题目
- poj 1753||poj 2965 枚举+dfs