1.栈

1.1.栈的顺序存储结构

1.1.1 C语言版

1.操作要点:

       (1)对于顺序栈,如栈时,首先判断栈是否满了,栈满的条件是:s->top == MAXSIZE-1,栈满时不能入栈;否则出现空间溢出,引起错误,称为上溢。

       (2)出栈和读栈顶元素操作,先判断栈是否为空,为空时不能操作,否则产生错误,通常栈空时常作为一种控制转移条件。

  (3)取栈顶元素与出栈的不同之处是出栈操作改变栈顶指针top的位置(栈顶指针下移一个位置),而取栈顶元素操作只是读出栈顶元素的值,栈顶指针top的位置不变。

2.顺序栈的类型描述

1 #define MAXSIZE  10
2 typedef int Emptype;
3 typedef int Decltype;
4 typedef struct node
5 {
6     Decltype data[MAXSIZE];
7     int top;
8 }SeqStack ,*Stack;
9 SeqStack *s;  //定义顺序栈指针

3.置空栈

1 SeqStack *Init_SeqStac()
2 {
3     SeqStack *s;
4     s = (Stack)malloc(sizeof(SeqStack));
5     s->top = -1;
6     return s;
7 }

4.判空栈

1  int Empty_SeqStack(SeqStack *s)
2  {
3      if (s->top == -1) return 1;
4      else return 0;
5  }

 5.入栈

 1 int Push_SeqStack(SeqStack *s, Decltype x)
 2 {
 3     if (s->top == MAXSIZE - 1) 
 4         return 0;
 5     else
 6     {
 7         s->top++;
 8         s->data[s->top] = x;
 9         return 1;
10     }
11 }

6.出栈

 1 int Pop_SeqStack(SeqStack *s, Decltype *x)
 2 {
 3     if (Empty_SeqStack(s))
 4         return 0;
 5     else
 6     {
 7         *x = s->data[s->top];
 8         s->top--;
 9         return 1;
10     }
11 }

 7.取栈顶元素

1 Decltype Top_SeqStack(SeqStack *s)
2 {
3     if (Empty_SeqStack(s))
4     return 0;
5     else
6         return (s->data[s->top]);
7 
8 }

8.两栈共享数据结构的定义

 1 typedef struct
 2 {
 3     Emptype stack][MAXSIZE];
 4     int lefttop;  //左栈栈定位置
 5     int righttop;  //右栈栈顶位置
 6 }dupsqstack;
 7 
 8 //设定左右栈标志
 9 char status;
10 status = 'L';
11 status = 'R';

 9.判断共享栈满

1 1 s->lefttop + 1 == s->righttop;

10.共享栈初始化操作

1 int initDupStack(dupsqstack *s)
2 {
3     if ((s = (dupsqstack*)malloc(sizeof(dupsqstack))) == NULL)
4         return false;
5     s->lefttop - 1;
6     s->righttop = MAXSIZE
7         return true;
8 }

11.共享栈入栈操作

 1 int pushDupsqstack(dupsqstack *s, char status, Emptype x)
 2 {
 3     //把数据元素x压入左栈或右栈
 4     if (s->lefttop + 1 == s->righttop)
 5         return false;
 6     if (status = 'L')
 7         s->stack[++s->righttop] = x;
 8     else if (status = 'R')
 9         s->stack[--s->lefttop] = x;
10     else return false;
11     return true;
12 }

12.共享栈出栈操作

 1 Emptype popDupsqstack(dupsqstack *s, char status)
 2 {
 3     //从左栈或右栈栈顶退出元素
 4     if (status = 'L')
 5     {
 6         if (s->lefttop < 0)
 7             return NULL;
 8         else return (s->stack[s->lefttop--]);
 9     }
10     else if (status = 'R')
11     {
12         if (s->righttop >MAXSIZE-1)
13             return NULL;
14         else return (s->stack[s->righttop++]);
15     }
16     else return NULL;
17 }

1.1.2 java版

  原理不再描述,只上代码

package com.test;
/**
 * @author Administrator
 * @param <T>
 * 在java中,用数组表示栈
 */
public class SqStack<T> {
    private T data[];  //
    private int top;   //栈顶指针,也是数组下标
    private int maxSize; //栈的大小,也是数组的大小
    
    //初始化栈
    public SqStack(int maxSize){
        this.maxSize = maxSize;
        this.top = -1;   
        this.data = (T[]) new Object[maxSize];
    }
    
    //1.判栈空,如果站顶指针还不是数组下标中的一个,则为空
    public boolean isNull(){
        if(this.top < 0){
            return true;
        }else{
            return false;
        }
    }
    //2.判栈满,如果站顶指针达到数组最大值,则为满
    public boolean isFull(){
        if(this.top == this.maxSize - 1){
            return true;
        }else{
            return false;
        }
    }
    //3.入栈,给数组中加入元素
    public boolean push(T value){
        if(isFull()){
            return false;
        }else{
            data[++top] = value;
            return true;
        }
    }
    
    //4.出栈
    public T Pop(){
        if(isNull()){
            return null;
        }else{
            T value = data[this.top];
            --this.top;
            return value;
        }
    }
    
}
View Code

相关文章:

  • 2022-12-23
  • 2021-04-20
  • 2021-04-03
  • 2021-12-08
  • 2022-02-07
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2021-11-28
  • 2021-12-24
  • 2021-10-24
  • 2021-08-02
相关资源
相似解决方案