c语言描述-栈的基本操作

一朵花儿开,就有一朵花儿败,满山的鲜花,只有代码最可爱

栈的顺序存取

#include<stdio.h>
#include <stdlib.h>
#define MAXSIZE 100        //定义最大存储空间
#define STACKINCREMENT 10  //增加的存储空间,increment意为:加薪、增值

typedef struct {
    int *base;
    int *top;
    int sum;  //记录元素个数
    int stacksize;  
}Stack;

//初始化栈
void Init(Stack *s){
    s->base=(int *)malloc(MAXSIZE*sizeof(Stack));
    if(s->base==NULL){
        printf("内存分配失败!");
    }
    s->top=s->base;
    s->stacksize=MAXSIZE;
    s->sum=0;
}

//在栈顶插入一个元素
void Push(Stack *s){
    int e;
    printf("请输入要插入的元素:");
    scanf("%d",&e);
    if(s->top-s->base>=s->stacksize){
        s->base=(int *)realloc(s->base,s->stacksize+STACKINCREMENT*sizeof(Stack));
        if(s->base==NULL)
        {
            printf("内存添加失败!");
        }
        s->stacksize=s->stacksize+STACKINCREMENT;
    }
    *(s->top++)=e;
    s->sum++;
}

//向栈内输入元素
void Input(Stack *s){
    int e;
    printf("请依次输入数据\n");
    while(s->top-s->base<s->stacksize){
        //e=NULL;
        scanf("%d",&e);
        s->sum++;
        *(s->top++)=e;
        if(getchar()=='\n')
            break;
    }
}

//打印栈内元素
void PrintStack(Stack *s){
    int e;
    printf("栈内元素为:");
    for(int i=1;i<=s->sum;i++)
    {
        e=*(s->top-i);
        printf("%d ",e);
    }
    printf("\n");
}

//情况栈内元素
void ClearStack(Stack *s){
    while(s->top!=s->base)
    {
       --s->top;
    }
}
//删除栈顶元素
void Pop(Stack *s){
    int e;
    if(s->base==s->top){
        printf("栈为空!");
        exit(0);
    }
    else{
        e=*(--s->top);
    }
    s->sum--;
}

//返回元素个数
int Stacklength(Stack *s){
    return s->sum;

}
//返回栈顶元素
void GetTop(Stack *s)
{
    int e;
    if(s->base==s->top)
    {
        printf("栈为空!");
        exit(0);
    }
    e=*(s->top-1);
    printf("栈顶元素为:%d\n",e);
}

int main(){
    Stack stack1,*p;
    p=&stack1;
    Init(p);
    Input(p);
    Push(p);
    GetTop(p);
    PrintStack(p);
    Pop(p);
    PrintStack(p);
    ClearStack(p);
}
/* 运行结果
请依次输入数据
1 2 3 4 5 6
请输入要插入的元素:9
栈顶元素为:9
栈内元素为:9 6 5 4 3 2 1 
栈内元素为:6 5 4 3 2 1 
Program ended with exit code: 0
*/

上面代码要注意--s->top与top-1的区别,执行--s->top时,栈的长度会改变,因为指针下移,而执行top-1时,指针的指向并未改变;

栈的链式存取

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

typedef struct Stack
{
    int  date;
    int stacksize;  //记录元素个数
    struct Stack *next;
    struct Stack *base;  //栈底指针
    struct Stack *top;  //栈顶指针
}Stack;
//输入数据
void InitStack(Stack *s)
{
    s->stacksize=0;
    int e;
    Stack *p;
    printf("请输入数据:\n");
    for (int i=0;;i++)
    {
        p=(Stack *)malloc(sizeof(Stack));
        if(!p)
            exit(0);
        scanf("%d",&e);   
        p->date=e;
        if(s->stacksize==0)  //当插入第一个元素时,指针的变化
        {
            s->base=p;    //赋给栈底指针
            s->top=p;
            p->next=NULL;
        }
        else
        {
            p->next=s->top;
            s->top=p;
            
        }
        s->stacksize++;
        if(getchar()=='\n') //此句要放最后
            break;
    }
}

//在栈顶插入数据
void push(Stack *s,int *num)
{
    Stack *p;
    p=(Stack *)malloc(sizeof(Stack));
    if(!p)
        exit(0);
    p->date=*num;
    p->next=s->top;
    s->top=p;
    s->stacksize++;
}
//显示栈内元素
void ShowStack(Stack *s)
{
    int e;
    Stack *p;
    p=s->top;
    for(int i=0;i<s->stacksize;i++)
    {
        e=p->date;
        p=p->next;
        printf("%d ",e);
    }
    printf("\n");
}

//栈出栈顶元素
void Pop(Stack *s)
{
    Stack *p;
    p=s->top;
    s->top=p->next;
    free(p);
    s->stacksize--;
}

int main()
{
    int e=9;
    Stack stack1,*p;
    p=&stack1;
    InitStack(p);
    push(p,&e);
    ShowStack(p);
    Pop(p);
    ShowStack(p);
    return 0;
}
//运行结果
/*
请输入数据:
1 2 3 4
9 4 3 2 1 
4 3 2 1 
Program ended with exit code: 0
*/
上一篇:二叉树遍历-非递归算法


下一篇:webpack4配置优化