C 언어 데이터 구 조 는 은행 시 뮬 레이 션 을 실현 한다.

C 언어 데이터 구 조 는 은행 시 뮬 레이 션 을 실현 한다.
구현 코드:

#include <stdio.h> 
#include <stdlib.h> 
#include <windows.h> 
#define MAX_WIN 20 
#define MAX_STAY 100 
typedef struct customer *link; 
struct customer 
{ 
  int stay; 
  link next; 
}; 
 
link GUY(int stay, link next) 
{ 
  link c = malloc(sizeof *c); 
  c->stay = stay; 
  c->next = next; 
  return c; 
} 
 
 
link win[MAX_WIN]; 
void morning() 
{ 
  int i; 
  for(i = 0; i < MAX_WIN; i++) 
  { 
    win[i] = NULL; 
  } 
} 
 
 
void come(int w, int stay) 
{ 
  if(win[w] == NULL) 
  { 
        win[w] = GUY(stay, NULL); 
        win[w]->next = win[w]; 
  } 
  else 
      win[w] = win[w]->next = GUY(stay, win[w]->next); 
} 
 
void leave(int w) 
{ 
  if(win[w]->next == win[w]) 
  { 
    free(win[w]); 
    win[w] = NULL; 
  } 
  else 
  { 
    link t = win[w]->next; 
    win[w]->next = t->next; 
    free(t); 
  } 
       
} 
 
 
void guys() 
{ 
  int i; 
  link t; 
  system("clear"); 
  for(i = 0; i < MAX_WIN; i++, puts(" ")) 
  { 
      printf("WIN%3d:_", i); 
      if((t = win[i]) == NULL) 
          continue; 
      for(; t->next != win[i]; t = t->next) 
      { 
        printf("%4d", t->next->stay); 
      } 
  } 
  Sleep(1); 
}   
 
void later() 
{ 
  int i; 
  for(guys(), i = 0; i < MAX_WIN; i++) 
  {   
    if(win[i] == NULL) 
        continue; 
    if(win[i]->next->stay > 0) 
        (win[i]->next->stay)--; 
    else 
        leave(i); 
  } 
} 
 
 
 
int main() 
{ 
  srand(time(NULL)); 
  for(morning; ;later()) 
  { 
    come(rand()%MAX_WIN, rand()%MAX_STAY+1); 
  } 
  return 0; 
} 
     여 기 는 생 성 된 난수 이기 때문에 프로그램 이 계속 변 합 니 다.ctrl+c 종료 프로그램 누 르 기


이상 은 C 언어 데이터 구조 가 은행 시 뮬 레이 션 을 실현 하 는 사례 에 대한 상세 한 설명 입 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남기 거나 본 사이트 지역사회 에 가서 토론 을 하 십시오.읽 어 주 셔 서 감사합니다. 여러분 께 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기