zoukankan      html  css  js  c++  java
  • 栈的链表实现

    /* 
    MyArryLinkstack.h 
    */
    #ifndef _MYARRYLINKSTACK_H_
    #define _MYARRYLINKSTACK_H_
    #include <stdlib.h>
    #define ElementType int
    #define EmptyTOS (-1)
    #define MinStackSize (5)
    typedef struct {
    	int Capacity;
    	int Top;
    	ElementType *Array;
    }StackRecord, *pStack;
    int IsEmpty(pStack S);
    int IsFull(pStack S);
    pStack CreatStack(int MaxElements);
    void DisposeStack(pStack S);
    void MakeEmpty(pStack S);
    ElementType GetTop(pStack S);
    void Push(pStack S, ElementType x);
    void Pop(pStack S);
    ElementType TopAndPop(pStack S);
    #endif
    
    /*
    MyArryLinkstack.c
    */
    #include "MyArryLinkstack.h"
    /*栈的创建,数组实现*/
    pStack CreatStack(int MaxElements)
    {
    	pStack S;
    	if (MaxElements < MinStackSize)
    		perror(" Stack is too small!");
    
    	S =(pStack) malloc(sizeof(StackRecord));
    	if (S == NULL)
    		perror("Out of Space!");
    	
    	S->Array = malloc(sizeof(ElementType) * MaxElements);
    	if (S->Array == NULL)
    		perror("Out of Space!!!");
    	S->Capacity = MaxElements;
    	MakeEmpty(S);
    
    	return S;
    }
    /*创建一个空栈*/
    void MakeEmpty(pStack S)
    {
    	S->Top = EmptyTOS; 
    }
    
    /*判断是否空栈*/
    int IsEmpty(pStack S)
    {
    	return S->Top == EmptyTOS;
    }
    
    /*判断是否满栈*/
    int IsFull(pStack S)
    {
    	return S->Top == S -> Capacity - 1;
    }
    
    /*进栈*/
    void Push(pStack S, ElementType x)
    {
    	if (IsFull(S))
    		perror("Full Stack!");
    	else
    		S->Array[++S->Top] = x;
    }
    /*出栈*/
    void Pop(pStack S)
    {
    	if (IsEmpty(S))
    		perror("Empty Stack!");
    	else
    		S->Top--;
    }
    /*取得栈顶元素*/
    ElementType GetTop(pStack S)
    {
    	if (IsEmpty(S)){
    		perror("Empty Stack!");
    		return 0;
    	}
    	else
    		return S->Array[S->Top];
    }
    
    /*
    main.c
    */
    /* LinkStackTest.c */
    #include <stdio.h>
    #include "MyArryLinkstack.h"
    
    int main()
    {
    	pStack pS = NULL;
    	int a;
    	pS = CreatStack(5);
    	Push(pS, 1);
    	a = GetTop(pS);
    	Push(pS, 3);
    	a = GetTop(pS);
    	Push(pS, 5);
    	a = GetTop(pS);
    	Push(pS, 7);
    	a = GetTop(pS);
    	Push(pS, 9);
    	a = GetTop(pS);
    	Pop(pS);
    	a = GetTop(pS);
    }
    

      

  • 相关阅读:
    使用css制作三角
    BZOJ3029: 守卫者的挑战
    Codeforces 442B. Andrey and Problem
    嘴巴题8 BZOJ2318: Spoj4060 game with probability Problem
    嘴巴题7 BZOJ1426: 收集邮票
    嘴巴题6 BZOJ3450JoyOI1952 Easy
    BZOJ1453: [Wc]Dface双面棋盘
    BZOJ2957: 楼房重建
    BZOJ4515: [Sdoi2016]游戏
    BZOJ3679: 数字之积
  • 原文地址:https://www.cnblogs.com/mrethan/p/4149542.html
Copyright © 2011-2022 走看看