栈的基本操作(静态顺序栈,动态顺序栈,链栈)

本人为一名准大二学生,在此将暑假期间复习的数据结构代码内容传于此,供初学者学习,或复习者复习,若部分代码存在问题,请不吝赐教,提出问题于评论区,对此在下表示感谢。(之后还会将本人学习数据结构的笔记给诸位奉上)

1.静态顺序栈

2.1动态顺序栈(1)

2.2动态顺序栈(2)

3.1链栈

//静态顺序栈
//5
//1 2 3 4 5
//5 4 3 2 1
//5
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 100

typedef struct {
    int data[MAX_SIZE];
    int top;  // 栈顶指针
}Stack;

// 初始化栈
void initStack(Stack* stack) {
    stack->top = 0;  // 初始时栈为空
}
// 判断栈是否为空
bool isEmpty(Stack* Stack)
{
    if(Stack->top==0)
        return true;
    else return false;
}
// 判断栈是否已满
bool isFull(Stack* Stack)
{
    if(Stack->top==MAX_SIZE-1)
        return true;
    else return false;
}
// 元素入栈
void push(Stack* stack, int val) {
    if (isFull(stack)) {
        printf("Stack is full, cannot push element.\n");
        return;
    }
    stack->data[++(stack->top)] = val;  // 栈顶指针加一,并将元素入栈
}
// 元素出栈
int pop(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty, cannot pop element.\n");
        return -1;
    }
    return stack->data[(stack->top)--];  // 返回栈顶元素,并将栈顶指针减一
}

// 获取栈顶元素
int top(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty.\n");
        return -1;
    }

    return stack->data[stack->top];
}
void traverse(Stack* stack)
{
    while(isEmpty(stack)!=true)
    {
        printf("%d ",stack->data[stack->top]);
     stack->top--;
    }
    printf("\n");
}
// 测试静态顺序栈的基本操作
void clearStack(Stack* stack)
{
    stack->top=0;//此时的top的值得和之前的初始化的top相同。
}
int main() {
    Stack stack;
    initStack(&stack);
    int i=0,n,val;
    scanf("%d",&n);
    while(i<n)
    {
        scanf("%d",&val);
        push(&stack,val);
        i++;
    }
    printf("%d\n", top(&stack));
     traverse(&stack);
     clearStack(&stack);
     traverse(&stack);
    return 0;
}
*/

//动态1
//5
//1 2 3 4 5
//5
//4 3 2 1
//
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define initsize 10

typedef struct {
    int* data;
    int top;
    int capacity;
}Stack;

Stack* createStack(int capacity) {
    Stack* stack = (Stack*)malloc(sizeof(Stack));
    stack->data = (int*)malloc(capacity * sizeof(int));
    stack->top = -1;
    stack->capacity = capacity > 0 ? capacity : 10;  // 如果如果传入的capacity值小于等于0,则将其设定为一个默认容量,例如10。
    //这样,即使没有显式提供capacity值,动态顺序栈仍会具有一个有效的初始容量。
    return stack;
}
int isFull(Stack* stack) {
    return stack->top == stack->capacity - 1;
}

int isEmpty(Stack* stack) {
    return stack->top == -1;
}

void push(Stack* stack, int element) {
    if (isFull(stack)) {
        int newCapacity = stack->capacity * 2;  // 扩容为原容量的两倍
        stack->data= (int*)realloc(stack->data, newCapacity * sizeof(int));
        stack->capacity = newCapacity;
    }
    stack->data[++stack->top] = element;
}

int pop(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty\n");
        exit(1);
    }
    return stack->data[stack->top--];
}

int top(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty\n");
        exit(1);
    }
    return stack->data[stack->top];
}

void clear(Stack* stack) {
    stack->top = -1;
}

int size(Stack* stack) {
    return stack->top + 1;
}

bool traverse(Stack* stack, int* pVal) {
    while (stack->top!=-1) {
        *pVal = stack->data[stack->top];
         stack->top--;
        //printf("%d ", *pVal);
        return true;
    }
    printf("\n");
    return false;
}


int main() {
    int n,val;
    scanf("%d",&n);
    Stack* stack = createStack(n);
    while(n>0)
    {
        scanf("%d",&val);
        push(stack,val);
        n--;
    }

    printf("\n");
    printf("Top element: %d\n", top(stack));  // 输出:30
    printf("Popped element: %d\n", pop(stack));  // 输出:30
    printf("Is the stack empty? %s\n", isEmpty(stack) ? "Yes" : "No");  // 输出:No
    //int *pval1=0,*pval=0;//错误写法
    int* pval1 = malloc(sizeof(int));
    int* pval = malloc(sizeof(int));
    while(traverse(stack,pval)==true)
    {
        *pval1=*pval;
        printf("%d ",*pval1);
    }

    clear(stack);
    printf("Is the stack empty? %s\n", isEmpty(stack) ? "Yes" : "No");  // 输出:Yes

    free(stack->data);
    free(stack);

    return 0;
}

*/

//
//动态顺序栈2
//5
//1 2 3 4 5
//5 4 3 2 1
//
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define initsize 10

typedef struct {
    int* data;
    int top;
    int capacity;
} Stack;

Stack* createStack(int capacity) {
    Stack* stack = (Stack*)malloc(sizeof(Stack));
    stack->data = (int*)malloc(capacity * sizeof(int));
    stack->top = -1;
    stack->capacity = capacity > 0 ? capacity : 10;
    return stack;
}

int isFull(Stack* stack) {
    return stack->top == stack->capacity - 1;
}

int isEmpty(Stack* stack) {
    return stack->top == -1;
}

void push(Stack* stack, int element) {
    if (isFull(stack)) {
        int newCapacity = stack->capacity * 2;
        stack->data = (int*)realloc(stack->data, newCapacity * sizeof(int));
        stack->capacity = newCapacity;
    }
    stack->data[++stack->top] = element;
}

int pop(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty\n");
        exit(1);
    }
    return stack->data[stack->top--];
}

int top(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty\n");
        exit(1);
    }
    return stack->data[stack->top];
}

void clear(Stack* stack) {
    stack->top = -1;
}

int size(Stack* stack) {
    return stack->top + 1;
}

void traverse(Stack *stack)
{
    int pTop=stack->top+1;//此处若不加一的话则无法打印出,第一个栈顶元素,有知道者请不吝赐教于评论区,在下多谢。
    while(pTop!=-1)
    {
        printf("%d ",stack->data[pTop]);
        pTop--;
    }
    printf("\n");
}


int main() {
    int n, val;
    scanf("%d", &n);

    if (n <= 0) {
        printf("Invalid input\n");
        return 0;
    }

    Stack* stack = createStack(n);

    while (n > 0) {
        scanf("%d", &val);
        push(stack, val);
        n--;
    }

    printf("Top element: %d\n", top(stack));
    printf("Popped element: %d\n", pop(stack));
    printf("Is the stack empty? %s\n", isEmpty(stack) ? "Yes" : "No");
    traverse(stack);
    clear(stack);
    printf("Is the stack empty? %s\n", isEmpty(stack) ? "Yes" : "No");
    int* pval1 = (int*)malloc(sizeof(int));
    int* pval = (int*)malloc(sizeof(int));
    free(pval1);
    free(pval);
    free(stack->data);
    free(stack);
    return 0;
}
*/


//链栈
//5
//1 2 3 4 5
//5 4 3 2 1
//5
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct node
{
    int data;
    struct node *pNext;
}NODE,*PNODE;
typedef struct stack
{
    PNODE pTop;
    PNODE pBottom;
}stack,*STACK;

void initstack(STACK pS) {
    pS->pTop = (PNODE)malloc(sizeof(NODE));
    pS->pBottom = pS->pTop;
    pS->pTop->pNext = NULL;
}

bool push(STACK pS, int val) {
    PNODE pNew = (PNODE)malloc(sizeof(NODE));
    if (pNew == NULL) {
        return false;
    }
    pNew->data = val;
    pNew->pNext = pS->pTop;//头插法来创建链栈
    pS->pTop = pNew;
    return true;
}


bool isempty(STACK pS)
{
    if(pS->pTop==pS->pBottom)
        return true;
    else return false;
}
bool pop(STACK pS,int *pVal)
{
  if(isempty(pS))
  {
      printf("Stack is empty\n");
      return false;
  }
  else
  {
      PNODE p=pS->pTop;
      *pVal=p->data;
      pS->pTop=pS->pTop->pNext;
      free(p);
      p=NULL;
      return true;
  }
}
void clear(STACK pS)
{
    PNODE p=pS->pTop;
    PNODE q=NULL;
    while(p!=pS->pBottom)
    {
        q=p->pNext;
        free(p);
        p=q;
    }
    pS->pTop=pS->pBottom;
}
void traverse(STACK pS)
{
    PNODE pTail=pS->pTop;
    while(pTail!=pS->pBottom)
    {
        printf("%d ",pTail->data);
        pTail=pTail->pNext;
    }
    printf("\n");
}
void destroy(STACK pS) {
    clear(pS);
    free(pS->pBottom);
    pS->pTop=NULL;
    pS->pBottom=NULL;
}
int main()
{
    int i,n,val;
    stack pS;
    initstack(&pS);
    scanf("%d",&n);
    printf("请输入要进栈的元素:");
    for(i=0;i<n;i++)
    {
        scanf("%d",&val);
        push(&pS,val);
    }
    int* pval;
    printf("出栈的元素:");
    traverse(&pS);
    if(pop(&pS,&pval))
    {
        printf("出栈成功的元素是:%d\n ",pval);
    }
      destroy(&pS);
    return 0;
}
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值