微軟等面試100題系列 29題解答 望各位同行指導

來源:互聯網
上載者:User

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
*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.