vcNa41eu2vNa4z/LVu7XNo6y1sdG51bu1xMqxuvKjrHRvcNa41evP8snP0sa2r6Os1rG1" /> vcNa41eu2vNa4z/LVu7XNo6y1sdG51bu1xMqxuvKjrHRvcNa41evP8snP0sa2r6Os1rG1">
Home Database Mysql Tutorial 数据结构栈

数据结构栈

Jun 07, 2016 pm 04:10 PM
one time study data structure notes Record language illustrate

说明:严蔚敏的《数据结构》(C语言版)学习笔记,记录一下,以备后面查看。 如上图所示,刚开始base指针和tZ喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcNa41eu2vNa4z/LVu7XNo6y1sdG51bu1xMqxuvKjrHRvcNa41evP8snP0sa2r6Os1rG1

说明:严蔚敏的《数据结构》(C语言版)学习笔记,记录一下,以备后面查看。

\

如上图所示,刚开始base指针和tZ喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcNa41eu2vNa4z/LVu7XNo6y1sdG51bu1xMqxuvKjrHRvcNa41evP8snP0sa2r6Os1rG1vdW7wvq686Os1bu2pda41et0b3DWuM/y1bvN4rXY1rejrLTLyrHO0sPH0OjSqtTZt9bF5NDCv9W85KGjPC9wPjxwPjwvcD48cHJlIGNsYXNzPQ=="brush:sql;">#include #include #include #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACKINCREMENT 10 //存储空间分配增量 const int OK = 1; //定义正确返回 const int ERROR = -1; //定义错误的返回 const int OVERFLOW = -2; //定义溢出 //定义元素类型 typedef int SElemType; //定义返回类型 typedef int Status; typedef struct{ SElemType *base; //栈底指针,在构造之前和销毁后base的值为NULL SElemType *top; //栈顶指针 int stacksize; //已分配的空间 }SqStack; //初始化栈 Status InitStack(SqStack &S){ S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK; } //获取栈顶元素 Status GetTop(SqStack S, SElemType &e){ if(S.top == S.base) return ERROR; e = *(S.top - 1); return OK; } //压栈 Status Push(SqStack &S, SElemType e){ if(S.top - S.base >= S.stacksize){ S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType)); if(!S.base) exit(OVERFLOW); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top = e; S.top++; return OK; } //出栈 Status Pop(SqStack &S, SElemType &e){ if(S.top == S.base) return ERROR; e = *(--S.top); return OK; } //判断栈是否为空 bool StackEmpty(const SqStack &S){ if(S.top == S.base) return true; else return false; } //十进制数转8进制数 void conversion(SqStack &S){ InitStack(S); printf("请输入10进制数,返回一个8进制数:\n"); int n; scanf("%d", &n); while(n){ Push(S, n % 8); n = n / 8; } SElemType e; printf("8进制数是:0x"); while(!StackEmpty(S)){ Pop(S, e); printf("%d", e); } printf("\n"); } int main(){ SqStack sq; //InitStack(sq); //Push(sq, 1); //Push(sq, 2); //Push(sq, 3); //SElemType e3; //Pop(sq, e3); //GetTop(sq, e3); //printf("%d", e3); conversion(sq); scanf("%d"); return 0; }

上面的conversion函数是一个将10进制转换为8进制的例子,这个就是栈的一个应用,还有例如,括号匹配的验证、迷宫求解等。

例如Hanoi塔问题:

假设有3个分别为a,b,c的三个塔座,a上有直径从大到小的圆盘,可以借助b塔座将a上的圆盘移动到c上,移动过程中大小顺序不变。

\

void movePic(char a, int n, char b){
    printf("将编号为%d的圆盘从%c上移动到%c上\n", n , a, b);
}

void hanuota(int n, char x, char y, char z){
    if(n == 1){
        movePic(x, 1, z);  //将编号为1的圆盘从x移到z
    }else{
        hanuota(n - 1, x, z, y); //将x上编号为1到n-1的圆盘移到y,z作辅助塔
        movePic(x, n, z);  //将编号为n的圆盘从x移到z
        hanuota(n - 1, y, x, z); //将y上编号为1到n-1的圆盘移到z,x作辅助塔
    }
}

int main(){
    hanuota(3, 'a', 'b', 'c');
}
Copy after login
我们可以将问题简单抽象成递归。

1、要将n个圆盘移动到c,则需要先将n-1个圆盘移动到b

\

2、再将a上的最底下的圆盘移动到c

\

3、最后将b上面的n-1个圆盘移动到c

经过这三个步骤就可以完成移动,在这三个步骤中,步骤1,从a将n-1个圆盘移动到b和问题本身是同一个问题,步骤3将n-1个圆盘从b移动到c也和问题本身是同一个问题,所以这两处我们就可以迭代调用。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of "What to do if the notes published by Xiaohongshu are missing" and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

Compare complex data structures using Java function comparison Compare complex data structures using Java function comparison Apr 19, 2024 pm 10:24 PM

When using complex data structures in Java, Comparator is used to provide a flexible comparison mechanism. Specific steps include: defining the comparator class, rewriting the compare method to define the comparison logic. Create a comparator instance. Use the Collections.sort method, passing in the collection and comparator instances.

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

As a lifestyle sharing platform, Xiaohongshu covers notes in various fields such as food, travel, and beauty. Many users want to share their notes on Xiaohongshu but don’t know how to do it. In this article, we will detail the process of posting notes on Xiaohongshu and explore how to block specific users on the platform. 1. How to publish notes tutorial on Xiaohongshu? 1. Register and log in: First, you need to download the Xiaohongshu APP on your mobile phone and complete the registration and login. It is very important to complete your personal information in the personal center. By uploading your avatar, filling in your nickname and personal introduction, you can make it easier for other users to understand your information, and also help them pay better attention to your notes. 3. Select the publishing channel: At the bottom of the homepage, click the "Send Notes" button and select the channel you want to publish.

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

How to change the language display of vivox60pro vivox60pro system language setting method How to change the language display of vivox60pro vivox60pro system language setting method Mar 23, 2024 am 09:06 AM

1. Click [System Management] in the phone settings menu. 2. Click the [Language] option. 3. Select the system language you want to use.

Thoughts and practice on assisted generation of B-end front-end code under large models Thoughts and practice on assisted generation of B-end front-end code under large models Apr 18, 2024 am 09:30 AM

1. Code specifications during background reconstruction work: During the B-end front-end development process, developers will always face the pain point of repeated development. The element modules of many CRUD pages are basically similar, but they still need to be developed manually, and time is spent on simple element construction. This reduces the development efficiency of business requirements. At the same time, because the coding styles of different developers are inconsistent, it makes it more expensive for others to get started during iterations. AI replaces simple brainpower: With the continuous development of large AI models, it has simple understanding capabilities and can convert language into instructions. General instructions for building basic pages can meet the needs of daily basic page building and improve the efficiency of business development in general scenarios. 2. Generate link list. B-side page lists, forms, and details can all be generated. Links can be roughly divided into the following categories:

Java data structures and algorithms: in-depth explanation Java data structures and algorithms: in-depth explanation May 08, 2024 pm 10:12 PM

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

See all articles