• c/c++ 线性栈


    c/c++ 线性栈

    线性栈

    下面的代码实现了以下功能

    函数 功能描述
    push 压入
    pop 弹出
    show_list 打印
    clear 移动top指针到栈底
    destroy 释放所有内存空间

    seqstack.h

    #ifndef __SEQSTACK__
    #define __SEQSTACK__
    
    #include <stdio.h>
    #include <malloc.h>
    #include <assert.h>
    #include <memory.h>
    #include <stdbool.h>
    
    #define SEQSTACK_INIT_SIZE 8
    #define ADD_SIZE 1000
    
    typedef int ElemType;
    
    typedef struct seqstack{
      int size;
      ElemType *base;
      ElemType *top;
    }seqstack;
    
    void init(seqstack*);
    void push(seqstack*, ElemType);
    void show_list(seqstack*);
    void pop(seqstack*);
    int length(seqstack*);
    void clear(seqstack*);
    void destroy(seqstack*);
    #endif
    
    

    seqstack.c

    #include "seqstack.h"
    
    bool reInit(seqstack* seq){
      ElemType* new = (ElemType*)realloc(seq->base, ADD_SIZE *sizeof(ElemType));
      if(NULL == new)return true;
      if(seq->base != new){
        seq->base = new;
        seq->top = seq->base + seq->size + 1;
      }
      return false;
    }
    void init(seqstack* seq){
      ElemType* e = (ElemType*)malloc(sizeof(ElemType) * SEQSTACK_INIT_SIZE);
      seq->base = seq->top = e;
      seq->size = 0;
    }
    void push(seqstack* seq, ElemType x){
      if(seq->size >= SEQSTACK_INIT_SIZE && reInit(seq)){
        printf("stack is full
    ");
        return;
      }
      //先赋值,后移动top的指向
      *((seq->top)++) = x;
      seq->size++;
    }
    void show_list(seqstack* seq){
      ElemType* e = seq->top;
      while(e-- != seq->base){
        printf("%d
    ",*e);
      }
    }
    void pop(seqstack* seq){
      --seq->top;
      seq->size--;
    }
    int length(seqstack* seq){
      return seq->size;
    }
    void clear(seqstack* seq){
      seq->top = seq->base;
      seq->size = 0;
    }
    void destroy(seqstack* seq){
      free(seq->base);
    }
    
    

    seqstackmain.c

    #include "seqstack.h"
    
    int main(){
      seqstack list;
      init(&list);
      int select = 1;
      ElemType item;
      int index;
      while(select){
        printf("*****************************************
    ");
        printf("*** [1]   push        [2]  pop        ***
    ");
        printf("*** [3]   show_list   [4]  length     ***
    ");
        printf("*** [5]   clear       [6]  destroy    ***
    ");
        printf("*** [0]   quit                        ***
    ");
        printf("*****************************************
    ");
        printf("请选择:>");
        scanf("%d", &select);
        if(0 == select)
          break;
        switch(select){
        case 1:
          printf("请输入要插入的数据>
    ");
          scanf("%d",&item);
          push(&list, item);	
          show_list(&list);
          break;
        case 2:
          pop(&list);	
          show_list(&list);
          break;
        case 3:
          show_list(&list);
          break;
        case 4:
          printf("length is %d
    ", length(&list));
          show_list(&list);
          break;
        case 5:
          clear(&list);
          show_list(&list);
          break;
        case 6:
          destroy(&list);
          break;
        default:
          printf("输入的选择错误,请重新选择
    ");
          break;
        }
      }
      destroy(&list);
    }
    
    
  • 相关阅读:
    win7下安装配置tomcat,java运行环境
    Ubuntu 12.10安装配置JDK7环境
    全面介绍Linux终端命令
    Ubuntu 配置 Tomcat
    Linux手动导入导出mysql数据库
    navicat数据库管理软件(支持mysql,oracle,sqlserver,sqlite,postgreSQL)
    信号量(semaphore)和互斥量(mutex)
    最新版fcitx 4.1.2源码编译安装(ubuntu 10.04)
    Groovy动态语言简介
    读书笔记:《java脚本编程:语言、框架与模式》(2)jvm内部的脚本语言
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9240694.html
Copyright © 2020-2023  润新知