王道C语言C++版栈总结

本文详细介绍了栈这一数据结构的基本概念和操作,包括初始化、判断栈空、进栈、出栈和获取栈顶元素等。同时,通过C语言展示了栈在顺序存储和链式存储结构中的具体实现,包括两种存储方式的代码示例。在顺序存储中,使用了固定大小的数组;在链式存储中,使用了动态内存分配的链表节点。
摘要由CSDN通过智能技术生成

请添加图片描述

定义
基本操作
顺序存储结构
链式存储结构


定义:只允许在一端进行插入或删除的线性表。
InitStack(&S);初始化一个空栈
StackEmpty(&S);判断一个栈是否为空
Push(&S, x);进栈(若未满
Pop(&S, &x);出栈(若非空)
GetTop(S, &x);读取栈顶元素(若非空,用x返回栈顶元素)
ClearStack(&S);销毁栈,并释放栈S所用的存储空间

栈的顺序存储结构

#include<cstdio>
#define Maxsize 50

typedef struct{
    int data[Maxsize];
    int top;
}SqStack;

void InitStack(SqStack &S){
    S.top = -1;
}
bool StackEmpty(SqStack &S){
    if(S.top == -1)
        return true;
    return false;
}
bool Push(SqStack &S, int x){
    if(S.top == Maxsize - 1)
        return false;
    S.data[++S.top] = x;
    return true;
}
bool Pop(SqStack &S, int &x){
    if(S.top == -1)
        return false;
    x = S.data[S.top--];
    return true;
}
bool GetTop(SqStack &S, int &x){
    if(S.top == -1)
        return false;
    x = S.data[S.top];
    return true;
}
bool ClearStack(SqStack &S){
    //free(S);
}

int main()
{
    SqStack s;
    InitStack(s);
    for(int i = 0; i < 10; i++)
        if(Push(s,i))
            printf("%d入栈\n", i);
    while(!StackEmpty(s)){
        int x;
        GetTop(s, x);
        printf("栈顶元素是%d\n", x);
        if(Pop(s, x))
            printf("%d出栈\n", x);
    }
    ClearStack(s);
    return 0;
}

栈的链式存储结构

#include<cstdio>
#include<cstdlib>
typedef struct Linknode{
    int data;
    struct Linknode *next;
} *LiStack;//栈结点类型指针

void InitStack(LiStack &S){
    S = NULL;
}
bool StackEmpty(LiStack &S){
    if(S == NULL)
        return true;
    return false;
}
//不带头结点的头插法
bool Push(LiStack &S, int x){
    Linknode* n = NULL;
    n = (Linknode*)malloc(sizeof(Linknode));
    if(!n)
        return false;

    n->data = x;
    n->next = S;
    S = n;
    return true;
}
bool Pop(LiStack &S, int &x){
    if(!S)
        return false;
    x = S->data;
    Linknode *p = S;
    S = S->next;
    free(p);
    return true;
}
bool GetTop(LiStack &S, int &x){
    if(!S)
        return false;
    x = S->data;
    return true;
}
bool ClearStack(LiStack &S){
    Linknode *p = S;
    while(p != NULL){
        Linknode *t = p;
        p = p->next;
        free(t);
        p = p->next;
    }
    free(p);
}
int main()
{
    LiStack s;
    InitStack(s);
    for(int i = 0; i < 10; i++)
        if(Push(s,i))
            printf("%d入栈\n", i);
    while(!StackEmpty(s)){
        int x;
        GetTop(s, x);
        printf("栈顶元素是%d\n", x);
        if(Pop(s, x))
            printf("%d出栈\n", x);
    }
    ClearStack(s);
    return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
王道数据结构课后代码的C语言完整版可以有很多种不同的代码示例,如下是一个实现链表的示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; return newNode; } void insert(Node** head, int data) { if (*head == NULL) { *head = createNode(data); return; } Node* curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = createNode(data); } void display(Node* head) { Node* curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } void delete(Node** head, int data) { if (*head == NULL) { return; } Node* curr = *head; Node* prev = NULL; // 如果要删除的元素是第一个节点 if (curr->data == data) { *head = curr->next; free(curr); return; } // 否则遍历链表找到要删除的元素 while (curr != NULL && curr->data != data) { prev = curr; curr = curr->next; } // 如果找到了要删除的元素 if (curr != NULL) { prev->next = curr->next; free(curr); } } int main() { Node* head = NULL; insert(&head, 1); insert(&head, 2); insert(&head, 3); insert(&head, 4); display(head); delete(&head, 2); display(head); return 0; } ``` 以上代码实现了一个简单的链表数据结构,在main函数中对链表进行了插入和删除操作,并打印出链表中的元素。这只是一个简单的示例,王道数据结构课本中还有很多其他的数据结构和算法代码可以一一学习和实现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值