#数据结构---实验5: 顺序栈的基本操作及应用

✨一、实验要点

  1. 在进行进制转换时,要特别注意十六进制,此时可以打表,也可以按照ASCII码输出对应数 值:注意,ASCII码中需要特别记住 '0' = 48, 'a' = 97, 'A' = 65即可

     2.  在实验过程中如果销毁栈之后,再次输入如查看栈的长度等应该输出专门的提示:如栈已销毁。所以,要在销毁栈的时候将 A == NULL (设为Stack *A), 但有意思的地方来了, free(p); // p 所指的内存被释放,但是p 所指的地址仍然不变,所以如果要在函数块中使得主函数中的A == NULL, 则应该传入A的指针,详情可见代码实现部分

二、实验目标

(1)实验目的

通过该实验,让学生掌握栈的相关基本概念,认识栈是插入和删除集中在一端进行的线性结构,掌握栈的“先入后出”操作特点。栈在进行各类操作时,栈底指针固定不动,掌握栈空、栈满的判断条件。

(2)实验内容

用顺序存储结构,实现教材定义的栈的基本操作,提供数制转换功能,将输入的十进制整数转换成二进制、八进制十六进制

三、代码实现 (可以重点看一下实验要点中的提示)

//
//  main.c
//  数据结构---顺序栈
//
//  Created by *** on 17/10/2023.
//

// 用顺序存储结构,实现教材定义的栈的基本操作,提供数制转换功能,
// 将输入的十进制整数转换成二进制、八进制或十六进制。


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10

// 顺序栈的创建
typedef struct Stack
{
    int elems[STACK_INIT_SIZE];
    int top;
    int capacity;
   
} Stack;

// 顺序栈的初始化
Stack* StackInit(void)
{
    Stack *A = (Stack*)malloc(sizeof(int) * STACK_INIT_SIZE);
    A->top = 0;
    A->capacity = STACK_INIT_SIZE;
    
    return A;
}

// 入栈操作
bool push_back(Stack *A, int e)
{
    if (A->top >= A->capacity)
        return false;
    
    A->elems[A->top ++ ] = e;
    
    return true;
}

// 出栈操作
bool pop_back(Stack *A, int *e)
{
    if (A->top == 0)
        return false;
    A->top --;
    *e = A->elems[A->top];
    
    return true;
}

// 清空栈
bool StackFree(Stack *A, int *e)
{
    while (A->top > 0)
        pop_back(A, e);
    
    return A;
}

// 销毁栈
bool StackDestroy(Stack **A)  // 要点一
{
    free(*A);
    *A = NULL;
    
    return true;
}

// 进制转化,将char dnum中的十进制数,转换成x进制数, 并将每一位都存储在栈Stack A中
bool Dconversion(int dnum, int x, Stack *A)
{
    int r = 0;
    while (dnum)
    {
        r = dnum % x;
        push_back(A, r);
        dnum /= x;
    }
    
    return true;
}

int dnum, x, e, order;
Stack *A = NULL;

int main(int argc, const char * argv[]) {
    
    printf("Please enter the corresponding letter to order : \n"
           "1 --- Initialize the stack\n"
           "2 --- Destroy the stack\n"
           "3 --- Free the stack\n"
           "4 --- Is Stack Empty?\n"
           "5 --- Calculate the length of the lovely stack\n"
           "6 --- Get the top element of the stack\n"
           "7 --- Insert an element\n"
           "8 --- Delete an element\n"
           "9 --- Print all the elements\n"
           "10 --- Convert the number scale\n"
           "Attention --- enter 0 to quit\n");
    printf("Please give me an order : ");
    scanf("%d", &order);
    
    while (order > 0)
    {
        switch(order)
        {
            case 1:
                // Stack *A = StackInit();
                A = StackInit();
                printf("Your stack A has been created.\n");
                break;
            case 2:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if (StackDestroy(&A))
                    printf("Your stack A has been destroyed.\n");
                break;
            case 3:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if (StackFree(A, &e))
                    printf("Your stack A has been freed\n");
                break;
            case 4:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if (A->top > 0)
                    printf("Your stack A is not empty\n");
                else
                    printf("Your stack A is empty\n");
                break;
            case 5:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else
                    printf("The length of stack A is %d\n", A->top);
                break;
            case 6:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if(A->top == 0)
                    printf("Your stack is empty. Please insert the elements first\n");
                else
                    printf("The top element of stack A is %d\n", A->elems[A->top - 1]);
                break;
            case 7:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else {
                    printf("Please enter the number your want to insert : ");
                    scanf("%d", &e);
                    push_back(A, e);
                    printf("The stack A is : \n");
                    for (int i = 0; i < A->top; i ++ ) printf("%d ", A->elems[i]);
                }
                
                break;
            case 8:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if(A->top == 0)
                    printf("Your stack is empty. Please insert the elements first\n");
                else
                {
                    printf("Attention : only the toppest element could be deleted"
                           "The stack A now is : ");
                    pop_back(A, &e);
                    for (int i = 0; i < A->top; i ++ ) printf("%d ", A->elems[i]);
                }
                
                break;
            case 9:
                if(A == NULL)
                    printf("Please initialize stack A first\n");
                else if(A->top == 0)
                    printf("Your stack is empty. Please insert the elements first\n");
                else{
                    printf("The stack now is : ");
                    for (int i = 0; i <= A->top; i ++ ) printf("%d ", A->elems[i]);
                }
                
                break;
            case 10:
                printf("Please enter the decimal number : ");
                scanf("%d", &dnum);
                printf("\nPlease enter which scale you want to convert to : ");
                scanf("%d", &x);
                
                Stack *A = StackInit();
                Dconversion(dnum, x, A);
                
                while (A->top > 0)
                {
                    pop_back(A, &e);
                    if (x == 16 && e >= 10)
                        printf("%c", e + 55);  //  要点二 :输出ASCII码对应的字符, '0' = 48, 'a' = 97, 'A' = 65
                    else
                        printf("%d", e);
                }
                printf("\n");
                
                break;
            default:
                printf("Sorry, please enter the right number to order\n");
                break;
        }
        
        printf("Please give me an order : ");
        scanf("%d", &order);
    }
    
    
    
   
    return 0;
}

四、总结

总的来说,顺序栈的实现并不是很困难,如果想要提高熟练度,可以删了再打一遍,一共3~5遍左右基本就差不多了。

具体栈的应用如:表达式求值、迷宫等等会在“严蔚敏《数据结构》代码实现”中给出具体的实                                      现,感兴趣的uu们可以期待一下~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
重庆邮电大学数据结构实验包括顺序表、链表、栈、队列、二叉树、图的遍历、查找算法和排序算法等内容。学生需要通过实验课程来掌握这些数据结构的基本概念、实现方法和应用场景。以下是每个实验的简要介绍: 1. 实验一:顺序表的实现及应用 顺序表是一种线性表,它的元素在内存中是连续存储的。在这个实验中,学生需要实现顺序表的基本操作,如插入、删除、查找等,并且通过实现一个简单的通讯录应用应用顺序表。 2. 实验二:链表的实现及应用 链表是一种线性表,它的元素在内存中不是连续存储的。在这个实验中,学生需要实现链表的基本操作,如插入、删除、查找等,并且通过实现一个简单的火车调度系统来应用链表。 3. 实验三:栈的实现及应用 栈是一种特殊的线性表,它的元素只能在表的一端进行插入和删除操作。在这个实验中,学生需要实现栈的基本操作,如入栈、出栈等,并且通过实现一个简单的括号匹配应用应用栈。 4. 实验四:队列的实现及应用 队列是一种特殊的线性表,它的元素只能在表的一端进行插入操作,在另一端进行删除操作。在这个实验中,学生需要实现队列的基本操作,如入队、出队等,并且通过实现一个简单的银行排队应用应用队列。 5. 实验五:二叉树操作及应用 二叉树是一种非常重要的数据结构,它的应用非常广泛。在这个实验中,学生需要实现二叉树的基本操作,如插入、删除、查找等,并且通过实现一个简单的表达式求值应用应用二叉树。 6. 实验六:图的遍历操作及应用 图是一种非常复杂的数据结构,它的应用也非常广泛。在这个实验中,学生需要实现图的基本操作,如深度优先遍历、广度优先遍历等,并且通过实现一个简单的迷宫游戏应用应用图。 7. 实验七:查找算法的实现 查找算法是一种非常重要的算法,它的应用也非常广泛。在这个实验中,学生需要实现查找算法的基本操作,如顺序查找、二分查找等,并且通过实现一个简单的电话号码查找应用应用查找算法。 8. 实验八:排序算法的实现 排序算法是一种非常重要的算法,它的应用也非常广泛。在这个实验中,学生需要实现排序算法的基本操作,如冒泡排序、快速排序等,并且通过实现一个简单的成绩排序应用应用排序算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值