数据结构(二)栈


一、栈

栈是只允许在一端进行插入或删除操作的线性表。
栈顶:线性表允许进行插入删除的那一端
栈底:固定的,不允许进行插入和删除的那一端
空栈:不含如何元素的空表

顺序栈

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define Maxsize 50
#define Elemtype int
typedef struct 
{
    Elemtype data[Maxsize]; //存放栈中元素
    int top;    //栈顶指针
}SqStack;

//初始化栈
void InitStack(SqStack &S)
{
    S.top = -1;
}

//判空
bool StackEmpty(SqStack& S)
{
    if (S.top == -1)
    {
        return true;  //判空
    }
    else
    {
        return false;  //不空
    }
}


//进栈
bool Push(SqStack& S, Elemtype x)
{
    if (S.top == Maxsize - 1)
    {
        return false;  //栈满了
    }

    S.data[++S.top] = x;
    return true;
}

//出栈

bool Pop(SqStack& S, Elemtype &x)
{
    if (S.top == -1)
    {
        return false;  //栈尾了
    }

    x = S.data[S.top--];
    
    return true;
}

//读栈顶数据
bool GetTop(SqStack& S, Elemtype& x)
{
    if (S.top == -1)
    {
        return false;  //栈尾了
    }

    x = S.data[S.top];

    return true;
}


int main()
{
    SqStack S;
    int x = 0;
    InitStack(S);
    Push(S,2);
    Push(S, 3);
    Pop(S, x);
    for (int i = 0; i <= S.top; i++)
    {
        printf("%d \t", S.data[i]);
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

线性栈(不带头结点)

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define Maxsize 50
#define Elemtype int
typedef struct Linknode
{
    Elemtype data;     //数据域
    struct Linknode* next;   //指针域
}*LiStack;

//初始化栈  不带头结点
void InitStack(LiStack & S)
{
    S = NULL;
}

//判空
bool StackEmpty(LiStack& S)
{
    if (S == NULL)
    {
        return true;  //判空
    }
    else
    {
        return false;
    }
}


//进栈
bool Push(LiStack& S, Elemtype x)
{
    Linknode* p = (Linknode*)malloc(sizeof(Linknode));
    p->next = NULL;
    
    p->data = x;
    p->next = S;

    S = p;
    return true;
}

//出栈

bool Pop(LiStack& S)
{
    Linknode* p;
    if (S == NULL)
    {
        return false;
    }
    p = S;
    S = S->next;
    free(p);

    return true;



}

//读栈顶数据
bool GetTop(LiStack& S, Elemtype& x)
{
    if (S == NULL)
    {
        return false;
    }
    x = S->data;

    return true;
}


int main()
{
    LiStack S;
    InitStack(S);
    int x = 0;
    
    Push(S, 2);
    Push(S, 3);
    Pop(S);
    while (S != NULL)
    {
        printf("%d \t ",S->data);
        S = S->next;

    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

线性栈(带头结点)

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define Maxsize 50
#define Elemtype int
typedef struct Linknode
{
    Elemtype data;     //数据域
    struct Linknode* next;   //指针域
}*LiStack;

//初始化栈  带头结点
void InitStack(LiStack& S)
{
    //初始化
    S = (LiStack)malloc(sizeof(Linknode));
    S->data = 0;
    S->next = NULL;
}

//判空
bool StackEmpty(LiStack& S)
{
    if (S->next == NULL)
    {
        return true;  //判空
    }
    else
    {
        return false;
    }
}


//进栈
bool Push(LiStack& S, Elemtype x)
{
    Linknode* p = (Linknode*)malloc(sizeof(Linknode));
    p->data = x;

    p->next = S->next;
    S->next = p;
    

    return true;
}

//出栈

bool Pop(LiStack& S)
{
    Linknode* p;
    if (S->next == NULL)
    {
        return false;
    }
    p = S->next;
    S->next = p->next;
    free(p);

    return true;



}

//读栈顶数据
bool GetTop(LiStack& S, Elemtype& x)
{
    if (S->next == NULL)
    {
        return false;
    }
    x = S->next->data;

    return true;
}


int main()
{
    LiStack S;
    InitStack(S);
    int x = 0;
    Linknode* p;
    Push(S, 2);
    Push(S, 3);
    
    Pop(S);
    p = S->next;
    while (p != NULL)
    {
        printf("%d \t ", p->data);
        p = p->next;

    }

}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

共享栈

#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#define maxsize 3

#define elemtype int


typedef struct
{
    elemtype data[maxsize];
    int top[2];
}stk;

stk s;


int push(int i, elemtype x)
{
    if (i > 1 || i < 0)
    {
        printf("栈编号错误!\n");
        exit(0);
    }

    if ((s.top[1] - 1) == s.top[0])
    {
        printf("栈满了!");
        return -1;
    }

    switch (i)
    {
    case 1:
        s.data[--s.top[1]] = x;

        break;
    case 0:
        s.data[++s.top[0]] = x;
        break;
    }
    //添加成功
    return 1;




}


int pop(int i)
{
    if (i > 1 || i < 0)
    {
        printf("栈编号错误!\n");
        exit(0);
    }



    switch (i)
    {
    case 1:
        if (s.top[1] == maxsize)
        {
            printf("栈底了!\n");
            exit(0);
        }
        else
            return s.data[s.top[1]++];

        break;
    case 0:
        if (s.top[0] == -1)
        {
            printf("栈底了!\n");
            exit(0);
        }
        else
            return s.data[s.top[1]--];

        break;
    }
    //添加成功
    return 1;




}

int main()
{
    s.top[0] = -1;
    s.top[1] = maxsize;
    push(0, 2);
    push(1, 5);
    push(1, 7);
    push(1, 9);
    for (int i = 0; i < maxsize; i++)
    {
        printf("%d\n", s.data[i]);
    }


    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenshida_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值