栈的C语言实现

1。栈的C语言实现-数组实现

 

#include <stdio.h>

#include <stdlib.h>

 

#define DEP 3

 

int stack[DEP];

int top = -1;

 

void push(int value)

{

    if(top < DEP - 1)

        stack[++top] = value;

    else

        printf("stack is full/n");

}

 

int pop()

{

    int temp;

    if(top >= 0)

    {

        temp = stack[top];

        top --;

        return temp;

    }

    else

    {

        printf("stack is empty/n");

        return -1;

    }

}

 

void stack_print()

{

    int temp = top;

    if(temp < 0){

        printf("stack is empty/n");

        return;

    }

    while(temp >= 0)

    {

        printf("[%i]", stack[temp--]);

    }

 

    printf("/n");

}

 

int main(int argc, char *argv[])

{

    int select, temp;

    do

    {

        printf("1.Input a data/n");

        printf("2.Output a data/n");

        printf("3.Exit/n");

        printf("Please input u choice:");

        scanf("%d", &select);

 

        switch(select)

        {

        case 1:

            printf("Please input data:");

            scanf("%d", &temp);

            printf("/nBefor:/n");

            stack_print();

            push(temp);

            printf("/nAfter:/n");

            stack_print();

            break;

        case 2:

            printf("/nBefor:/n");

            stack_print();

            if((temp = pop()) != -1)

                printf("pop[%i]/n",temp);

            printf("/nAfter:/n");

            stack_print();

            break;

        default:

            break;

        }

    }while(select != 3);

 

    return 0;

}

 

2。栈的C语言实现-链表实现

 

#include <stdio.h>

#include <stdlib.h>

 

struct s_node

{

    int data;

    struct s_node *next;

};

 

typedef struct s_node node;

 

typedef node* link;

 

link stack = NULL;

 

void push(int value)

{

    link newnode = (link)malloc(sizeof(node));

    newnode->data = value;

    newnode->next = stack;

    stack = newnode;

}

 

int pop()

{

    int temp;

    if(stack != NULL)

    {

        temp = stack->data;

        stack = stack->next;

        return temp;

    }

    else

    {

        printf("empty stack");

        return -1;

    }

}

 

void stack_print()

{

    link temp = stack;

    while(temp != NULL)

    {

        printf("[%i]", temp->data);

        temp = temp->next;

    }

    printf("/n");

}

 

int main(int argc, char *argv[])

{

    int select, temp;

    do{

        printf("1.Input a data/n");

        printf("2.Output a data/n");

        printf("3.Exit/n");

        printf("Please input u choice:");

        scanf("%d", &select);

        switch(select)

        {

        case 1:

            printf("Please input data:");

            scanf("%d", &temp);

            printf("/nBefor:/n");

            stack_print();

            push(temp);

            printf("/nAfter:/n");

            stack_print();

            break;

        case 2:

            printf("/nBefor:/n");

            stack_print();

            if((temp = pop()) != -1)

                printf("pop[%i]/n",temp);

            printf("/nAfter:/n");

            stack_print();

            break;

        default:

            break;

        }

    }while(select != 3);

 

    return 0;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值