My first stack

Copyright: Complimentary more micro-business marketing courses, please pay attention to the public No. [shop]! https://blog.csdn.net/gzw1623231307/article/details/54562683

Stack like a pistol, a hand gun, oh no, really pistols, metal, not you following the pistol.

When we put a bullet into the pistol, the last bullet, the bullet is fired out of the first. This is the principle stack.

Last in last out, last in, first out, is the official definition, maybe you think it is not easy to remember, remember it like a pistol, the purpose of this article is to let you stack and not to be confused with other things (I have not learn other things, how confused?)

hhh ~

Here is my first stack, novice program, it may not be the standard, but the most easily understood that slightly.

This is my hybrid procedures, c and c ++ hybrid out, only use the C ++ input and output only, but also because c ++ rookie, I will not new delete it.

 

#include <iostream>
#include <stdlib.h>
struct mystack
{
	int size = 2000;//这是栈的大小 ,容量。
	int *top;       //栈顶指针
	int *bottom;    //栈底指针
}stack;
int main()
{
	int n;
	std::cin>> n;                                     //输入的数作为自动创建栈的参数
	stack.top = (int *)malloc(2000*sizeof(int));      //初始化一块内存用来存数据
	stack.bottom = stack.top;                         //栈底 = 栈顶 表示栈为空。
	for(int i = 1; i <= n; i++)
	{
		*stack.top = i;                           //把 i复制进去
		stack.top++;                              //栈顶指针上移
	}
	
	for(int i = 1; i <= n; i++)
	{
	        stack.top--;
		std::cout<< *stack.top<< " ";

	}
        //输出
 return 0;
}

 

 

 

 

 

Guess you like

Origin blog.csdn.net/gzw1623231307/article/details/54562683