栈的模拟(大学程序设计作业)

C

struct

#include "stdio.h"
#include "stdlib.h"

struct node
{
    int nodeVal;
    struct node *preNode;
    struct node *nextNode;
};

struct stack
{
    int stackSize;
    struct node *stackNode;
};
struct stack *initStack()
{
    struct stack *tmpStack = (struct stack *)malloc(sizeof(struct stack));
    (*tmpStack).stackNode = (struct node *)malloc(sizeof(struct node));
    (*tmpStack).stackSize = 0;
    return tmpStack;
}
int stackPush(struct stack *tmpStack, int tmpVal)
{
    ++(*tmpStack).stackSize;
    (*(*tmpStack).stackNode).nextNode = (struct node *)malloc(sizeof(struct node));
    (*(*(*tmpStack).stackNode).nextNode).preNode = (*tmpStack).stackNode;
    (*(*(*tmpStack).stackNode).nextNode).nodeVal = tmpVal;
    (*tmpStack).stackNode = (*(*tmpStack).stackNode).nextNode;
    (*(*tmpStack).stackNode).nextNode = NULL;
    return 0;
}
int getStackTop(struct stack *tmpStack)
{
    if ((*tmpStack).stackSize != 0)
        return (*(*tmpStack).stackNode).nodeVal;
    return EOF;
}
int stackPop(struct stack *tmpStack)
{
    if ((*tmpStack).stackSize == 0)
        return EOF;
    (*tmpStack).stackNode = (*(*tmpStack).stackNode).preNode;
    free((*(*tmpStack).stackNode).nextNode);
    --(*tmpStack).stackSize;
    return 0;
}
int getStackSize(struct stack tmpStack)
{
    return tmpStack.stackSize;
}
int stackIfEmpty(struct stack tmpStack)
{
    return tmpStack.stackSize == 0;
}
void stackShow(struct stack *tmpStack)
{
    printf("stack: ");
    if ((*tmpStack).stackSize == 0)
    {
        printf("NULL\n");
        return;
    }
    struct node *currentNode = (*tmpStack).stackNode;
    int tmpSize = (*tmpStack).stackSize;
    while (tmpSize > 0) {
        printf("%d\t", (*currentNode).nodeVal);
        currentNode = (*currentNode).preNode;
        --tmpSize;
    }
    printf("\n");
}
int deleteStack(struct stack *tmpStack)
{
    free(tmpStack);
    return 0;
}

int main()
{
    struct stack *tmpStack = initStack();
    stackShow(tmpStack);
    stackPush(tmpStack, 1);
    stackPush(tmpStack, 2);
    stackPush(tmpStack, 3);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPush(tmpStack, 4);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    deleteStack(tmpStack);
    return 0;
}

typedef struct

#include "stdio.h"
#include "stdlib.h"

typedef struct node
{
    int nodeVal;
    struct node *preNode;
    struct node *nextNode;
} node;

typedef struct stack
{
    int stackSize;
    node *stackNode;
} stack;
stack *initStack()
{
    stack *tmpStack = (stack *)malloc(sizeof(stack));
    (*tmpStack).stackNode = (node *)malloc(sizeof(node));
    (*tmpStack).stackSize = 0;
    return tmpStack;
}
int stackPush(stack *tmpStack, int tmpVal)
{
    ++(*tmpStack).stackSize;
    (*(*tmpStack).stackNode).nextNode = (node *)malloc(sizeof(node));
    (*(*(*tmpStack).stackNode).nextNode).preNode = (*tmpStack).stackNode;
    (*(*(*tmpStack).stackNode).nextNode).nodeVal = tmpVal;
    (*tmpStack).stackNode = (*(*tmpStack).stackNode).nextNode;
    (*(*tmpStack).stackNode).nextNode = NULL;
    return 0;
}
int getStackTop(stack *tmpStack)
{
    if ((*tmpStack).stackSize != 0)
        return (*(*tmpStack).stackNode).nodeVal;
    return EOF;
}
int stackPop(stack *tmpStack)
{
    if ((*tmpStack).stackSize == 0)
        return EOF;
    (*tmpStack).stackNode = (*(*tmpStack).stackNode).preNode;
    free((*(*tmpStack).stackNode).nextNode);
    --(*tmpStack).stackSize;
    return 0;
}
int getStackSize(stack tmpStack)
{
    return tmpStack.stackSize;
}
int stackIfEmpty(stack tmpStack)
{
    return tmpStack.stackSize == 0;
}
void stackShow(stack *tmpStack)
{
    printf("stack: ");
    if ((*tmpStack).stackSize == 0)
    {
        printf("NULL\n");
        return;
    }
    node *currentNode = (*tmpStack).stackNode;
    int tmpSize = (*tmpStack).stackSize;
    while (tmpSize > 0)
    {
        printf("%d\t", (*currentNode).nodeVal);
        currentNode = (*currentNode).preNode;
        --tmpSize;
    }
    printf("\n");
}
int deleteStack(stack *tmpStack)
{
    free(tmpStack);
    return 0;
}

int main()
{
    stack *tmpStack = initStack();
    stackShow(tmpStack);
    stackPush(tmpStack, 1);
    stackPush(tmpStack, 2);
    stackPush(tmpStack, 3);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPush(tmpStack, 4);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    stackPop(tmpStack);
    stackShow(tmpStack);
    deleteStack(tmpStack);
    return 0;
}

C++

struct

#include <cstdio>
#include <cstdlib>

struct node
{
    int nodeVal;
    node *preNode;
    node *nextNode;

    node()
    {
        nodeVal = 0;
        preNode = nextNode = NULL;
    }
};

struct stack
{
    int stackSize;
    node *stackNode;

    stack()
    {
        stackNode = (node *)malloc(sizeof(node));
        stackSize = 0;
    }

    int push(const int &tmpVal)
    {
        ++stackSize;
        (*stackNode).nextNode = (node *)malloc(sizeof(node));
        (*(*stackNode).nextNode).preNode = stackNode;
        (*(*stackNode).nextNode).nodeVal = tmpVal;
        stackNode = (*stackNode).nextNode;
        (*stackNode).nextNode = NULL;
        return 0;
    }
    int pop()
    {
        if (stackSize == 0)
            return EOF;
        stackNode = (*stackNode).preNode;
        free((*stackNode).nextNode);
        --stackSize;
        return 0;
    }
    int top()
    {
        if (stackSize != 0)
            return (*stackNode).nodeVal;
        return EOF;
    }

    int size()
    {
        return stackSize;
    }
    int empty()
    {
        return stackSize == 0;
    }

    void show()
    {
        printf("stack: ");
        if (stackSize == 0)
        {
            printf("NULL\n");
            return;
        }
        node *currentNode = stackNode;
        int tmpSize = stackSize;
        while (tmpSize > 0)
        {
            printf("%d\t", (*currentNode).nodeVal);
            currentNode = (*currentNode).preNode;
            --tmpSize;
        }
        printf("\n");
    }
};

int main()
{
    stack tmpStack;
    tmpStack.show();
    tmpStack.push(1);
    tmpStack.push(2);
    tmpStack.push(3);
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.push(4);
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    return 0;
}

class

#include <cstdio>
#include <cstdlib>

class node
{
public:
    int nodeVal;
    node *preNode;
    node *nextNode;

    node()
    {
        nodeVal = 0;
        preNode = nextNode = NULL;
    }
};

class stack
{
private:
    int stackSize;
    node *stackNode;

public:
    stack()
    {
        stackNode = (node *)malloc(sizeof(node));
        stackSize = 0;
    }

    int push(const int &tmpVal)
    {
        ++stackSize;
        (*stackNode).nextNode = (node *)malloc(sizeof(node));
        (*(*stackNode).nextNode).preNode = stackNode;
        (*(*stackNode).nextNode).nodeVal = tmpVal;
        stackNode = (*stackNode).nextNode;
        (*stackNode).nextNode = NULL;
        return 0;
    }
    int pop()
    {
        if (stackSize == 0)
            return EOF;
        stackNode = (*stackNode).preNode;
        free((*stackNode).nextNode);
        --stackSize;
        return 0;
    }
    int top()
    {
        if (stackSize != 0)
            return (*stackNode).nodeVal;
        return EOF;
    }

    int size()
    {
        return stackSize;
    }
    int empty()
    {
        return stackSize == 0;
    }

    void show()
    {
        printf("stack: ");
        if (stackSize == 0)
        {
            printf("NULL\n");
            return;
        }
        node *currentNode = stackNode;
        int tmpSize = stackSize;
        while (tmpSize > 0)
        {
            printf("%d\t", (*currentNode).nodeVal);
            currentNode = (*currentNode).preNode;
            --tmpSize;
        }
        printf("\n");
    }
};

int main()
{
    stack tmpStack;
    tmpStack.show();
    tmpStack.push(1);
    tmpStack.push(2);
    tmpStack.push(3);
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.push(4);
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    tmpStack.pop();
    tmpStack.show();
    return 0;
}
无标签
评论区
头像
文章目录