재 학 데이터 구조 003 ― 스 택 의 기본 조작 및 실현 (체인 저장)

1. 창고 의 개념
    한 끝 에 만 삽입 어 삭제 작업 을 할 수 있 는 표를 보 여 줍 니 다.정의 상 스 택 도 선형 표 이기 때문에 스 택 도 대부분 선형 표 가 갖 춘 기본 적 인 조작 을 가진다.그러나 정의 에서 알 수 있 듯 이 스 택 은 삽입, 삭제 작업 을 할 때 한 끝 에서 만 작업 을 할 수 있 고 이 한 끝 은 스 택 꼭대기 (top) 가 됩 니 다.
    스 택 의 가장 핵심 적 인 작업 은 주로 스 택 (Push), 스 택 (Pop), 스 택 정상 요소 (Top) 로 돌아 가 는 것 입 니 다.그 밖 에 스 택 이 비어 있 는 지, 스 택 을 만 들 고 스 택 을 비 우 는 지 등 조작 도 판단 한다.
    선형 표 인 이상 스 택 은 실현 방식 으로 볼 때 주로 두 가지 가 있다. 순서 저장 실현 (배열), 체인 저장 실현 (링크) 이다.다음은 체인 식 저장 실현 방식 에서 사이트 의 데이터 구조 정의 입 니 다.

  
  
  
  
  1. typedef struct Node *PtrToNode; 
  2. typedef PtrToNode Stack; 
  3. typedef struct Node 
  4. { 
  5.     int Element; 
  6.     PtrToNode Next; 
  7. }; 

    사이트 의 기본 동작 은 다음 과 같 습 니 다.

  
  
  
  
  1. //  
  2. int IsEmpty(Stack S); 
  3. //  
  4. Stack CreateStack(); 
  5. //  
  6. void DisposeStack(Stack S); 
  7. //  
  8. void MakeEmpty(Stack S); 
  9. //  
  10. void Push(int X,Stack S); 
  11. //  
  12. int Top(Stack S); 
  13. //  
  14. void Pop(Stack S); 

    다음은 스 택 에 대한 완전한 기본 작업 의 예 입 니 다.

  
  
  
  
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. typedef struct Node *PtrToNode; 
  5. typedef PtrToNode Stack; 
  6. typedef struct Node 
  7. { 
  8.     int Element; 
  9.     PtrToNode Next; 
  10. }; 
  11.  
  12. int IsEmpty(Stack S); 
  13. Stack CreateStack(); 
  14. void DisposeStack(Stack S); 
  15. void MakeEmpty(Stack S); 
  16. void Push(int X,Stack S); 
  17. int Top(Stack S); 
  18. void Pop(Stack S); 
  19.  
  20. //  
  21. int IsEmpty(Stack S) 
  22. { 
  23.     return S->Next == NULL; 
  24. } 
  25. //  
  26. Stack CreateStack() 
  27. { 
  28.     Stack S = malloc(sizeof(struct Node)); 
  29.     if(S == NULL) 
  30.     { 
  31.         printf("No enough memory!"); 
  32.         return NULL; 
  33.     } 
  34.     S->Next = NULL; 
  35.     MakeEmpty(S); 
  36.     return S; 
  37. } 
  38.  
  39. void MakeEmpty(Stack S) 
  40. { 
  41.     if(S == NULL) 
  42.     { 
  43.         printf("Use CreateStack First!"); 
  44.     } 
  45.     else 
  46.     { 
  47.         while(!IsEmpty(S)) 
  48.         { 
  49.             Pop(S); 
  50.         } 
  51.     } 
  52. } 
  53.  
  54. void Push(int X,Stack S) 
  55. { 
  56.     PtrToNode Tmp; 
  57.     Tmp = malloc(sizeof(struct Node)); 
  58.     if(Tmp != NULL) 
  59.     { 
  60.         Tmp->Element = X; 
  61.         Tmp->Next = S->Next; 
  62.         S->Next = Tmp; 
  63.     } 
  64.     else 
  65.     { 
  66.         printf("Out of space!"); 
  67.     } 
  68. } 
  69.  
  70. void Pop(Stack S) 
  71. { 
  72.      
  73.     if(IsEmpty(S)) 
  74.     { 
  75.         printf("The Stack is Empty!"); 
  76.     } 
  77.     else 
  78.     { 
  79.         PtrToNode Tmp = S->Next; 
  80.         S->Next = Tmp->Next; 
  81.         free(Tmp); 
  82.     } 
  83. } 
  84.  
  85. int Top(Stack S) 
  86. { 
  87.     if(IsEmpty(S)) 
  88.     { 
  89.         printf("The stack is empty!"); 
  90.         return 0; 
  91.     } 
  92.     else 
  93.     { 
  94.         return S->Next->Element; 
  95.     } 
  96. } 
  97.  
  98. int main(void) 
  99. { 
  100.     Stack S = CreateStack(); 
  101.     int i; 
  102.     for(i = 0; i < 5; i++) 
  103.     { 
  104.         Push(i,S); 
  105.     } 
  106.  
  107.     while(!IsEmpty(S)) 
  108.     { 
  109.         printf("%d
    "
    ,Top(S)); 
  110.         Pop(S); 
  111.     } 
  112.     return 0; 
  113. } 

좋은 웹페이지 즐겨찾기