前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >栈模型的实现--链表实现

栈模型的实现--链表实现

作者头像
Enterprise_
发布2019-02-21 17:07:00
5470
发布2019-02-21 17:07:00
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆

栈是限制插入和删除只能在一个位置上进行的表,该位置是表的末端,叫做栈顶(top)。push进栈相当于插入,pop相当于删除最后插入的元素,一般不对空栈进行pop和top操作,还有一个,push的时候空间用尽是一个实现错误.

代码语言:javascript
复制
/*
栈的实现,包括对栈实现初始化,插入栈顶元素,删除栈顶元素,遍历栈,清空栈等基本操作
*/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<Windows.h>
#define STACK_SIZE 6
void Empty(struct Stack *temp);/*创建一个空栈的辅助函数*/
struct Stack *CreateStack(void);/*创建一个空栈*/
int IsEmpty(struct Stack *S);/*测试栈是否为空*/
void Push( struct Stack*sp);    /*进栈例程*/
int Top(struct Stack*temp); /*返回栈顶元素*/
void Pop(struct Stack*temp);    /*从栈弹出元素*/
void Rebuild(struct Stack *p);  /*释放栈空间*/
void Print();               /*菜单打印*/
void Exit();                /*退出*/

struct Stack {                  /*栈的声明*/
    int Element;
    struct Stack *next;
};
struct Stack *stack = NULL;
int main(void) {
    char ch = '\0';
    while (1) {
        system("cls");
        fflush(stdin);
        Print();
        scanf(" %c",&ch);
        switch (ch) {
        case'a':
            stack=CreateStack();
            break;
        case'b':
            Push(stack);
            break;
        case'c':
            Top(stack);
            break;
        case'd':
            Pop(stack);
            break;
        case'e':
            Rebuild(stack);
            break;
        case'f':
            Exit();
            break;
        default:
            printf("无此选择项!\n");
            system("pause");
            break;
        }
    }
    return 0;
}

void Rebuild(struct Stack *p) {
    if (p==NULL) {
        printf("Must use CreateStack first!");
        return;
    }
    struct Stack *temp = NULL;
    struct Stack *tt = NULL;
    temp = p;
    p->next = NULL;
    while (temp != NULL) {
        tt = temp->next;
        free(temp);
        temp = tt;
    }
    puts("Rebuild!\n");
    system("pause");
}
void Exit() {   /*这个是用来退出*/
    int i = 0;
    printf("退出中");
    for (i = 4; i > 0; --i) {
        Sleep(200);
        printf(".");
    }
    exit(0);
}
void Print()
{
    printf("-----主菜单功能如下:\n");
    printf("-----a.创建一个栈\n");
    printf("-----b.Push进栈\n");
    printf("-----c.Top返回栈顶元素\n");
    printf("-----d.Pop出栈\n");
    printf("-----e.释放栈空间\n");
    printf("-----f.退出\n");
}


int IsEmpty(struct Stack *S) {
    return S->next == NULL?1:0;
}

struct Stack *CreateStack(void){
    struct Stack *temp=NULL;
    struct Stack *current = NULL;
    struct Stack *last = NULL;
    int size = STACK_SIZE;
    int a = 0;
    temp = (struct Stack*)malloc(sizeof(struct Stack));
    while (size-- > 0)
    {
        current = (struct Stack*)malloc(sizeof(struct Stack));
        if (temp == NULL)
            temp = current;
        if (last != NULL)
            last->next = current;

        current->next = NULL;
        last = current;
    }
    puts("Created!");
    return (temp);
}

void Empty(struct Stack *temp) {
    if (temp == NULL) {
        printf("Must use CreateStack first!");
        return;
    }
    else
        temp->next = NULL;
}

void Push( struct Stack*sp)
{
    int x=0;
    struct Stack *temp = NULL;
    temp = (struct Stack*)malloc(sizeof(struct Stack));
    if (temp == NULL) {
        printf( "out of space!");
    }
    else {
        printf("Please enter a Element x!\n");
        scanf("%d",&x);
        temp->Element = x;
        temp->next = sp->next;
        sp->next = temp;
    }
}

int Top(struct Stack*temp) {
    if (!IsEmpty(temp))
        return temp->next->Element;
    printf( "Empty stack");
    return 0;     
}

void Pop(struct Stack*temp) {
    struct Stack *first = NULL;
    if(IsEmpty(temp))
        printf("Empty stack");
    else
    {
        first = temp->next;
        temp->next = temp->next->next;
        free(first);
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年05月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档