zoukankan      html  css  js  c++  java
  • 栈的顺序存储实现2

    栈的头文件:

     1 #pragma once
     2 #include<stdlib.h>
     3 #include<stdio.h>
     4 
     5 //数组模拟栈
     6 #define MAX_SIZE 1024
     7 
     8 typedef struct SEQSTACK
     9 {
    10     void* data[MAX_SIZE];
    11     int size;
    12 }SeqStack;
    13 
    14 //初始化栈
    15 SeqStack* Init_SeqStack();
    16 //入栈
    17 void Push_SeqStack(SeqStack* stack, void* data);
    18 //出栈
    19 void Pop_SeqStack(SeqStack* stack);
    20 //返回栈顶元素
    21 void* Top_SeqStack(SeqStack* stack);
    22 //判断是否为空
    23 bool IsEmpty(SeqStack* stack);
    24 //清空栈
    25 void Clear_SeqStack(SeqStack* stack);
    26 //销毁栈
    27 void Free_SeqStack(SeqStack* stack);

    栈的实现:

      1 #include "栈的顺序存储.h"
      2 //初始化栈
      3 SeqStack* Init_SeqStack()
      4 {
      5     SeqStack* stack = (SeqStack*)malloc(sizeof(SeqStack));
      6     if (stack == NULL)
      7     {
      8         exit(1);
      9     }
     10 
     11     stack->size = 0;
     12     for (int i = 0; i < MAX_SIZE; ++i)
     13     {
     14         stack->data[i] = NULL;
     15     }
     16 
     17     return stack;
     18 }
     19 //入栈
     20 void Push_SeqStack(SeqStack* stack, void* data)
     21 {
     22     if (stack == NULL)
     23     {
     24         exit(1);
     25     }
     26 
     27     if (stack->size == MAX_SIZE)
     28     {
     29         exit(1);
     30     }
     31 
     32     if (data == NULL)
     33     {
     34         exit(1);
     35     }
     36 
     37     stack->data[stack->size] = data;
     38     stack->size++;
     39 }
     40 //出栈
     41 void Pop_SeqStack(SeqStack* stack)
     42 {
     43     if (stack == NULL)
     44     {
     45         exit(1);
     46     }
     47 
     48     if (stack->size == 0)
     49     {
     50         exit(1);
     51     }
     52 
     53     stack->size--;
     54 }
     55 //返回栈顶元素
     56 void* Top_SeqStack(SeqStack* stack)
     57 {
     58     if (stack == NULL)
     59     {
     60         exit(1);
     61     }
     62 
     63     if (stack->size == 0)
     64     {
     65         exit(1);
     66     }
     67 
     68     return stack->data[stack->size - 1];
     69 }
     70 //判断是否为空
     71 bool IsEmpty(SeqStack* stack)
     72 {
     73     if (stack == NULL)
     74     {
     75         exit(1);
     76     }
     77 
     78     return stack->size == 0;
     79 }
     80 //清空栈
     81 void Clear_SeqStack(SeqStack* stack)
     82 {
     83     if (stack == NULL)
     84     {
     85         exit(1);
     86     }
     87 
     88     stack->size = 0;
     89 }
     90 //销毁栈
     91 void Free_SeqStack(SeqStack* stack)
     92 {
     93     if (stack == NULL)
     94     {
     95         exit(1);
     96     }
     97 
     98     while (stack->size != 0)
     99     {
    100         Pop_SeqStack(stack);
    101         stack->size--;
    102     }
    103 
    104     free(stack);
    105 }

    栈的测试:

     1 #include"栈的顺序存储.h"
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #include<stdio.h>
     5 
     6 #define M 8
     7 #define N 3
     8 //数据元素结构体
     9 typedef struct PERSON
    10 {
    11     char name[64];
    12     int age;
    13 }Person;
    14 
    15 int main(int argc, const char* argv[])
    16 {
    17     //创建循环链表
    18     SeqStack* stack = Init_SeqStack();
    19 
    20     //创建数据
    21     Person p1 = { "aas",40 };
    22     Person p2 = { "bbb",50 };
    23     Person p3 = { "ccc",60 };
    24     Person p4 = { "ddd",70 };
    25     Person p5 = { "eee",80 };
    26     //是否为空
    27     printf("");
    28     //入栈
    29     Push_SeqStack(stack, &p1);
    30     Push_SeqStack(stack, &p2);
    31     Push_SeqStack(stack, &p3);
    32     Push_SeqStack(stack, &p4);
    33     Push_SeqStack(stack, &p5);
    34 
    35     //出栈
    36     Person* tmp = (Person*)Top_SeqStack(stack);
    37     printf("name %s age %d
    ", tmp->name, tmp->age);
    38 
    39     //删除
    40     Pop_SeqStack(stack);
    41 
    42     //出栈
    43     Person* tmp1 = (Person*)Top_SeqStack(stack);
    44     printf("name %s age %d
    ", tmp1->name, tmp1->age);
    45 
    46     //判断是否为空
    47     bool ret = IsEmpty(stack);
    48     printf("判断是否为空:%d
    ", ret);
    49     //清空栈
    50     Clear_SeqStack(stack);
    51 
    52 
    53     //判断是否为空
    54     ret = IsEmpty(stack);
    55     printf("判断是否为空:%d
    ", ret);
    56     
    57     //释放
    58     Free_SeqStack(stack);
    59     system("pause");
    60     return 0;
    61 }

    测试结果:

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/dhhu007/p/13492017.html
Copyright © 2011-2022 走看看