栈(stack)

栈结构定义
typedef int DataType; // 假设数据类型为int

typedef struct StackNode {
    DataType data;
    struct StackNode *pnext;
} SKNode;

typedef struct StackTop {
    SKNode *ptop;
    int clen;
} SKTop;
创建栈

核心逻辑:

分配一个 SKTop 类型结构体的内存

初始化:ptop = NULL(栈顶为空)clen = 0(栈长度为0)

SKTop *CreateStack() {
    SKTop *stack = malloc(sizeof(SKTop));
    if (NULL == stack) {
        printf("malloc stack error\n");
        return NULL;
    }
    stack->ptop = NULL;
    stack->clen = 0;
    return stack;
}
入栈操作

核心逻辑:

        创建一个新结点 SKNode,存储数据 data

        新结点的 pnext 指向当前栈顶

        栈顶指针更新为新结点

         栈长度 clen++

void PushStack(SKTop *stack, DataType data) {
    SKNode *pnode = malloc(sizeof(SKNode));
    if (NULL == pnode) {
        printf("malloc pnode error\n");
        return;
    }
    pnode->data = data;
    pnode->pnext = stack->ptop;
    stack->ptop = pnode;
    stack->clen++;
}
遍历栈

核心逻辑:

        从 ptop 开始逐个节点向下访问

        输出每个节点的 data

void ShowStack(SKTop *stack) {
    if (IsEmpty(stack)) {
        printf("Empty\n");
        return;
    }
    SKNode *temp = stack->ptop;
    while (temp) {
        printf("%d\n", temp->data);
        temp = temp->pnext;
    }
}
出栈操作

核心逻辑:

        如果栈空,返回

        保存栈顶元素数据(可选保存到 popdata

        栈顶指针后移,释放旧栈顶节点

        栈长度 clen--

void PopStack(SKTop *stack, DataType *popdata) {
    if (IsEmpty(stack)) {
        printf("Empty\n");
        return;
    }
    if (1 == stack->clen) {
        if (popdata != NULL) {
            *popdata = stack->ptop->data;
        }
        free(stack->ptop);
        stack->ptop = NULL;
    } else {
        SKNode *temp = stack->ptop;
        if (popdata != NULL) {
            *popdata = temp->data;
        }
        stack->ptop = temp->pnext;
        free(temp);
    }
    stack->clen--;
}
获取栈顶元素

核心逻辑:

        如果栈空,返回直接

        读取 ptop->data 保存到 topdata

void GetStackTop(SKTop *stack, DataType *topdata) {
    if (IsEmpty(stack)) {
        printf("Empty\n");
        return;
    }
    *topdata = stack->ptop->data;
}
销毁栈

核心逻辑:

        使用 PopStack 逐个释放节点

        最后释放栈顶结构本身

   SKTop*将传入指针设为 NULL

void DestroyStack(SKTop **stack) {
    SKNode *temp = (*stack)->ptop;
    while ((*stack)->ptop) {
        PopStack(*stack, NULL);
    }
    free(*stack);
    *stack = NULL;
}
判空操作
int IsEmpty(SKTop *stack) {
    return NULL == stack->ptop;
}

队列(Queue)

队列结构定义

头文件:

typedef int DataType;

typedef struct QNode {
    DataType data;
    struct QNode *pnext;
} QNode;

typedef struct Quene {
    QNode *phead;
    QNode *ptail;
    int clen;
} Quene;
创建队列

核心逻辑

        分配一个 Quene 结构体空间

        初始化三个成员:phead = NULL  ptail = NULL  clen = 0

Quene *CreateLequene() {
    Quene *quene = malloc(sizeof(Quene));
    if (NULL == quene) {
        perror("malloc quene error");
        return NULL;
    }
    quene->phead = NULL;
    quene->ptail = NULL;
    quene->clen = 0;
    return quene;
}
检查队列空
int IsEmpty(Quene *quene) {
    return NULL == quene->phead;
}
入队操作

核心逻辑

        创建一个新结点 QNode,保存传入的数据

        若队列为空:队头、队尾都指向新节点若队列

        非空:当前队尾的 pnext 指向新节点,更新 ptail 指向新节点,队列长度 clen++

int PushLequene(Quene *quene, DataType data) {
    if (NULL == quene) {
        fprintf(stderr, "Queue pointer is NULL\n");
        return -1;
    }

    QNode *pnode = malloc(sizeof(QNode));
    if (NULL == pnode) {
        perror("malloc pnode error");
        return -1;
    }
    pnode->data = data;
    pnode->pnext = NULL;

    if (IsEmpty(quene)) {
        quene->phead = pnode;
    } else {
        quene->ptail->pnext = pnode;
    }
    quene->ptail = pnode;
    quene->clen++;
    return 0;
}
出队操作

核心逻辑

        判断是否空队列,空则返回保存当前队头结点指针,

        准备释放将 phead 指针后移(指向下一个结点)

        如果出队后为空,将 ptail 也置为 NULL

        释放原队头结点,更新长度

int PopLequene(Quene *quene) {
    if (IsEmpty(quene)) {
        fprintf(stderr, "Queue is empty\n");
        return -1;
    }

    QNode *pdel = quene->phead;
    quene->phead = pdel->pnext;
    if (NULL == quene->phead) {
        quene->ptail = NULL;
    }
    free(pdel);
    quene->clen--;
    return 0;
}
获取队首元素
int GetTopofLeQuene(Quene *quene, DataType *Topdata) {
    if (IsEmpty(quene)) {
        fprintf(stderr, "Queue is empty\n");
        return -1;
    }
    *Topdata = quene->phead->data;
    return 0;
}

 

遍历队列
int ShowLeQuenue(Quene *quene) {
    if (IsEmpty(quene)) {
        printf("Queue is empty\n");
        return -1;
    }

    QNode *temp = quene->phead;
    while (temp) {
        printf("%d ", temp->data);
        temp = temp->pnext;
    }
    printf("\n");
    return 0;
}

销毁队列

核心逻辑

        通过 PopLequene 不断删除所有结点

        最后释放整个队列结构体本身

        将传入指针置为 NULL

int DestroyLeQuene(Quene **quene) {
    if (NULL == *quene) {
        fprintf(stderr, "Double pointer is NULL\n");
        return -1;
    }

    while ((*quene)->phead) {
        PopLequene(*quene);
    }
    free(*quene);
    *quene = NULL;
    return 0;
}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐