栈的C语言实现

栈的C语言实现

按照《数据结构与算法分析》黑皮书中的描述实现

用数组实现栈

抽象数据类型ADT

#define MinStackSize 3
#define EmptyToStack -1
struct ElementType;
struct StackRecord;
typedef StackRecord *Stack;

int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int maxsize);
void DisposeStack(Stack &S);//销毁栈
void MakeEmpty(Stack &S);
void Push(ElementType X,Stack &S);
void Pop(Stack &S);
ElementType Top(Stack S);
ElementType TopAndPop(Stack &S);
void PrintStack(Stack S);
void PrintElement(ElementType e);
void Del_ElementType(ElementType &e);
struct ElementType
{
    int id;//唯一标识符
    char name[20];
};
struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};

完整测试代码

#include<stdio.h>
#include<stdlib.h>
#define MinStackSize 3
#define EmptyToStack -1
struct ElementType;
struct StackRecord;
typedef StackRecord *Stack;

int IsEmpty(Stack S);
int IsFull(Stack S);
Stack CreateStack(int maxsize);
void DisposeStack(Stack &S);//销毁栈
void MakeEmpty(Stack &S);
void Push(ElementType X,Stack &S);
void Pop(Stack &S);
ElementType Top(Stack S);
ElementType TopAndPop(Stack &S);
void PrintStack(Stack S);
void PrintElement(ElementType e);
void Del_ElementType(ElementType &e);
struct ElementType
{
    int id;//唯一标识符
    char name[20];
};
struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};

int IsEmpty(Stack S)
{
    return S->TopOfStack==EmptyToStack;
}

int IsFull(Stack S)
{
    return S->TopOfStack==S->Capacity-1;
}

Stack CreateStack(int maxsize)
{
    Stack S;
    if(maxsize<MinStackSize){
        printf("stack size is too small\n");
        exit(0);
    }

    S=(Stack)malloc(sizeof(StackRecord));
    if(S==NULL){
        perror("out of space");
        exit(0);
    }

    S->Array=(ElementType*)malloc(sizeof(ElementType)*maxsize);
    S->Capacity=maxsize;
    S->TopOfStack=EmptyToStack;
    return S;
}

void DisposeStack(Stack &S)
{
    if(S!=NULL){
        free(S->Array);
        free(S);
    }
}

void MakeEmpty(Stack &S)
{
    if(S->Array==NULL){
        printf("must use CreateStack first\n");
    }
    else {
        while (!IsEmpty(S))
        {
            Pop(S);
        }
    }
}

void Push(ElementType X,Stack &S)
{
    if(IsFull(S)){
        printf("Full Stack\n");

    }
    else{
        S->Array[++S->TopOfStack]=X;
    }
}

void Pop(Stack &S)
{
    if(IsEmpty(S)){
        printf("Empty Stack\n");
    }
    else{
       Del_ElementType(S->Array[S->TopOfStack--]);
    }
}

ElementType Top(Stack S)
{
    if(IsEmpty(S)){
        perror("Empty Stack\n");
        exit(0);
    }
    else{
       return S->Array[S->TopOfStack];
    }
}

ElementType TopAndPop(Stack &S)
{
    ElementType Tmp=Top(S);
    Pop(S);
    return Tmp;
}

void PrintStack(Stack S)
{
    printf("Stack:\n");

    if(!IsEmpty(S)){
        printf("id---name\n");
        int i=S->TopOfStack;
        printf("top:%d\n",i);
        while (i>=0)
        {
            printf("%d---%s\n",S->Array[i].id,S->Array[i].name);
            i--;
        }
    }
    else{
        printf("empty\n");
    }
}

void PrintElement(ElementType e)
{
    printf("id:%d---name:%s\n",e.id,e.name);
}

void Del_ElementType(ElementType &e)
{
    e.id=NULL;
    *e.name=NULL;
}
int main()
{
    ElementType e_1={1,"zhangsan"};
    ElementType e_2={2,"lisi"};
    ElementType e_3={3,"Alice"};
    ElementType e_4={4,"Bob"};    
    Stack S=CreateStack(4);
    PrintStack(S);
    Push(e_1,S);
    Push(e_2,S);
    Push(e_3,S);
    Push(e_4,S);
    Push(e_4,S);
    PrintStack(S);
    PrintElement(Top(S));
    Pop(S);
    Pop(S);
    Pop(S);
    PrintStack(S);
    Pop(S);
    Pop(S);
    Pop(S);

    Push(e_1,S);
    Push(e_2,S);
    Push(e_3,S);
    Push(e_4,S);
    PrintStack(S);
    MakeEmpty(S);
    PrintStack(S);
    DisposeStack(S);
    getchar();
    return 0;
}

用链表实现栈

ADT

#include<stdlib.h>
#include<stdio.h>
struct Node;
struct ElementType;
typedef Node *Ptr2Node;
typedef Ptr2Node Stack;

int IsEmpty(Stack S);
Stack CreateStack();
void DisposeStack(Stack &S);//销毁栈
void MakeEmpty(Stack &S);
void Push(ElementType X,Stack &S);
void Pop(Stack &S);
ElementType Top(Stack S);
void PrintStack(Stack S);
void PrintElement(ElementType e);
struct ElementType
{
    int id;//唯一标识符
    char name[20];
};


struct Node
{
    ElementType data;
    Ptr2Node next;
};

入栈操作

微信图片_20240609165918

完整测试代码

#include<stdlib.h>
#include<stdio.h>
struct Node;
struct ElementType;
typedef Node *Ptr2Node;
typedef Ptr2Node Stack;

int IsEmpty(Stack S);
Stack CreateStack();
void DisposeStack(Stack &S);//销毁栈
void MakeEmpty(Stack &S);
void Push(ElementType X,Stack &S);
void Pop(Stack &S);
ElementType Top(Stack S);
void PrintStack(Stack S);
void PrintElement(ElementType e);
struct ElementType
{
    int id;//唯一标识符
    char name[20];
};


struct Node
{
    ElementType data;
    Ptr2Node next;
};

int IsEmpty(Stack S)
{
    return S->next==NULL;
}

Stack CreateStack()
{
    Stack S;

    S=(Stack)malloc(sizeof(Node));
    if(S==NULL){
        printf("out of space \n");
        exit(0);
    }
    S->next=NULL;
    return S;

}

void MakeEmpty(Stack &S)
{
    if(S==NULL){
        perror("must use creatstack first. error");
    }
    else{
        while (!IsEmpty(S))
        {
            Pop(S);
        }
    }
}

void Push(ElementType X,Stack &S)
{
    Ptr2Node TmpCell;

    TmpCell=(Ptr2Node)malloc(sizeof(Node));
    if(TmpCell==NULL){
        perror("error");
    }
    else{
        TmpCell->data=X;
        TmpCell->next=S->next;
        S->next=TmpCell;
    }
}

ElementType Top(Stack S)
{
    if(IsEmpty(S)){
        perror("stack empty");
        exit(0);
    }
    else{
        return S->next->data;
    }
}

void Pop(Stack &S)
{
    if(IsEmpty(S)){
        perror("stack empty");
        return;
    }
    else{
        Ptr2Node TmpCell;
        TmpCell=S->next;
        S->next=TmpCell->next;
        free(TmpCell);
    }

}

void DisposeStack(Stack &S)
{
    MakeEmpty(S);
    free(S);
}

void PrintStack(Stack S)
{
    printf("Stack:\n");

    if(!IsEmpty(S)){
        printf("address---next_address---id---name\n");
        Ptr2Node ptr=S->next;
        while (ptr!=NULL)
        {
            printf("0x%x---0x%6x---%d---%s\n",ptr,ptr->next,
            ptr->data.id,ptr->data.name);
            ptr=ptr->next;
        }
    }
    else{
        printf("empty\n");
    }
}

void PrintElement(ElementType e)
{
    printf("id:%d---name:%s\n",e.id,e.name);
}
int main()
{
    ElementType e_1={1,"zhangsan"};
    ElementType e_2={2,"lisi"};
    ElementType e_3={3,"Alice"};
    ElementType e_4={4,"Bob"};    

    Stack S=CreateStack();
    PrintStack(S);
    Push(e_1,S);
    Push(e_2,S);
    Push(e_3,S);
    Push(e_4,S);
    PrintStack(S);
    PrintElement(Top(S));
    Pop(S);
    Pop(S);
    Pop(S);
    PrintStack(S);
    Pop(S);
    Pop(S);
    Pop(S);

    Push(e_1,S);
    Push(e_2,S);
    Push(e_3,S);
    Push(e_4,S);
    PrintStack(S);
    MakeEmpty(S);
    PrintStack(S);
    DisposeStack(S);
    getchar();
    return 0;
}
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值