[C / C ]Stack and applications- Stack trong C – Stack in C

Stack is an unordered list that allows insertion and deletion is performed at the end of the list and we call this the tip end (top) the stack.

Here is the code and also a number of operations performed by C Stack

#include <stdlib.h>
#include <stdio.h>
#include <stack> //khai bao Stack
using namespace std; 
int main()
{
	stack <int> S; //Khai bao Stack
	for (int x=0; x<10; x++) //Nhap Stack
		S.push(x*2); //Them phan tu vao Stack
	printf("nDo dai Stack: %d n",S.size()); //Do dai Stack
	while(!S.empty()) //Trong khi danh sach khong rong thi duyet va xuat ra Stack
	{
		int x = S.top(); //lay gia tri Top
		S.pop(); //Loai bo phan tu Top
		printf("%d  ",x); //Xuat ra Stack
	}
	return 0;
}

Application Stack
Stack has many applications in information technology as :
– Convert the number (Binary, decimal, octal,…)
– Jump infix expression to postfix, calculate the expression suffix,…
Now we will learn and installation 2 problem on
– Jump base: The following code will transfer the base 10 to base x keyboard input

#include <stdlib.h>
#include <stdio.h>
#include <stack> //khai bao Stack

using namespace std; 

int main()
{
	stack <int> S; 
	int coso, so, du, n;
	printf("Nhap so can chuyen: ");
	scanf("%d",&so);
	n = so;
	printf("Nhap co so can chuyen: ");
	scanf("%d",&coso);
	while(so)
	{
		du = so % coso;
		S.push(du);
		so /= coso;
	}
	printf ("So %d trong he %d la : ", so, coso);
	while(!S.empty())
	{
		printf("%d",S.top());
		S.pop();
	}
	return 0;
}