栈的C语言实现

来源:互联网 发布:深圳朗尼科道闸软件 编辑:程序博客网 时间:2024/05/01 14:27

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;

}

 

 

原创粉丝点击