堆栈

来源:互联网 发布:ws 15 知乎 编辑:程序博客网 时间:2024/06/06 19:11

顺序栈

//实验要求1#include<malloc.h>#include<stdio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2typedef int ElemType;typedef int Status;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef struct{ElemType *base;ElemType *top;int stacksize;}SqStack;Status InitStack(SqStack *S){    (*S).base=(ElemType *)malloc((STACK_INIT_SIZE)*sizeof(ElemType));    if(!(*S).base)        exit(OVERFLOW);    (*S).top=(*S).base;    (*S).stacksize=STACK_INIT_SIZE;    return OK;}Status StackEmpty(SqStack S){    if(S.base==S.top)        return OK;    return FALSE;}int StackLength(SqStack S){    return S.top-S.base;}Status GetTop(SqStack S,ElemType *e){    if(!StackEmpty(S))        return ERROR;        S.top--;      *e=*S.top;      return OK;}Status Push(SqStack *S,ElemType e){    if((*S).top-(*S).base>=(*S).stacksize)//达到最大容量        {            (*S).base=(ElemType *)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(ElemType));            if(!(*S).base)                exit(OVERFLOW);         (*S).top=(*S).base+(*S).stacksize;         (*S).stacksize+=STACKINCREMENT;    }     *(*S).top=e;     (*S).top++;     return OK;}Status Pop(SqStack *S,ElemType *e){    if(StackEmpty(*S))        return ERROR;    (*S).top-=1;    *e=*(*S).top;    return OK;}void Printf(ElemType e){    printf("%d ",e);}Status StackTraverse(SqStack S,void (*visit)(ElemType))//不改变S{    S.top--;    while(S.top>=S.base)//注意这点    {       visit(*S.top);       S.top--;    }    printf("\n");    return OK;}int main(){   SqStack S;   InitStack(&S);   printf("输入顺序栈的元素个数\n");   int n,i;   scanf("%d",&n);   printf("输入数据元素\n");   ElemType e;   for(i=1;i<=n;i++)        {            scanf("%d",&e);            Push(&S,e);}   printf("顺序栈的元素为\n");   StackTraverse(S,Printf);   printf("输入要入栈的数据元素\n");   scanf("%d",&e);   Push(&S,e);   printf("顺序栈的元素为\n");   StackTraverse(S,Printf);   Pop(&S,&e);   printf("栈顶元素:%d\n",e);   printf("栈顶元素出栈后,顺序栈元素为:\n");   StackTraverse(S,Printf);   return 0;}

链栈

链栈是操作受限的链表
它的插入和删除只能在首元结点操作(含头结点)

#include<stdio.h>#include<malloc.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2typedef int Status;typedef int ElemType;typedef struct Snode{    ElemType data;    struct Snode *next;}SNode,*LinkStack;Status InitLStack(LinkStack *S){    *S=(SNode*)malloc(sizeof(SNode));    if(!*S)        exit(OVERFLOW);    (*S)->next=NULL;    return OK;}Status LPush(LinkStack S,ElemType e){ //链栈是操作受限的链表,插入删除只能在首元结点    LinkStack p=(LinkStack)malloc(sizeof(SNode));    if(!p)        exit(OVERFLOW);    p->data=e;    p->next=S->next;    S->next=p;    return OK;}Status LPop(LinkStack S,ElemType *e){    if(S->next==NULL)        return ERROR;    LinkStack p;    p=S->next;    S->next=p->next;    *e=p->data;    free(p);    return OK;}void Printf(ElemType e){    printf("%d ",e) ;}Status LStackTraverse(LinkStack S,void (* visit)(ElemType)){    LinkStack p;    p=S->next;    printf("链栈数据元素为:\n");    while(p!=NULL)    {        visit(p->data);        p=p->next;    }    printf("\n");    return OK;}int  main(){    LinkStack S;    InitLStack(&S);    ElemType e;    printf("输入将要入栈的元素个数,以及元素值\n");    int i,n;    scanf("%d",&n);    for(i=1;i<=n;i++)    {        scanf("%d",&e);        LPush(S,e);    }    LStackTraverse(S,Printf);    printf("输入将要入栈的数据元素:\n");    //getchar();    scanf("%d",&e);    LPush(S,e);    LStackTraverse(S,Printf);    LPop(S,&e);    printf("栈顶元素为:%d\n",e);    LStackTraverse(S,Printf);    return 0;}