Programming C: Posts 15 – Install stack (Stack)

Stacks (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

stack

In this article we learn how to install and on Stack on the array pointer

1. Stack installed on the array

#define Max 100 //so phan tu toi da cua Stack
typedef int item; //kieu du lieu cua Stack
struct Stack
{
	int Top; //Dinh Top
	item Data[Max]; //Mang cac phan tu
};

1.1 Initialize empty list, check list is empty, full

void Init (Stack &S) //khoi tao Stack rong
{
	S.Top = 0; //Stack rong khi Top la 0
}

int Isempty(Stack S) //kiem tra Stack rong
{
	return (S.Top == 0);
}

int Isfull(Stack S) //kiem tra Stack day
{
	return (S.Top == Max); //
}

1.2 Add elements to Stack (Push)

To insert element inserted into one location Stack Top, Top up and Funeral 1 unit
push

void Push(Stack &S, item x) //them phan tu vao Stack
{
	if (!Isfull(S))
	{
		S.Data[S.Top] = x; //Gan du lieu
		S.Top ++; //Tang Top len 1
	}
}

1.3 Get data on Top but not deleted (Peak)

int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa
{
	return S.Data[S.Top-1]; //Lay du lieu tai Top
}

1.4 Remove and retrieve data at Top (Pop)

pop

int Pop(Stack &S) //Loai bo phan tu khoi Stack
{
	if (!Isempty(S))
	{
		S.Top --; //Giam Top
		return S.Data[S.Top]; //Lay du lieu tai Top
	}
}

1.5 Code entire program (full)

#include <stdlib.h>
#include <stdio.h>

#define Max 100 //so phan tu toi da cua Stack
typedef int item; //kieu du lieu cua Stack
struct Stack
{
	int Top; //Dinh Top
	item Data[Max]; //Mang cac phan tu
};

void Init (Stack &S); //khoi tao Stack rong
int Isempty(Stack S); //kiem tra Stack rong
int Isfull(Stack S); //kiem tra Stack day
void Push(Stack &S, item x); //them phan tu vao Stack
int Peak(Stack S); //Lay phan tu o dau Stack nhung khong xoa
int Pop(Stack &S); //Loai bo phan tu khoi Stack
void Input (Stack &S); //Nhap Stack
void Output(Stack S); //Xuat Stack

void Init (Stack &S) //khoi tao Stack rong
{
	S.Top = 0; //Stack rong khi Top la 0
}

int Isempty(Stack S) //kiem tra Stack rong
{
	return (S.Top == 0);
}

int Isfull(Stack S) //kiem tra Stack day
{
	return (S.Top == Max); //
}

void Push(Stack &S, item x) //them phan tu vao Stack
{
	if (!Isfull(S))
	{
		S.Data[S.Top] = x; //Gan du lieu
		S.Top ++; //Tang Top len 1
	}
}

int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa
{
	return S.Data[S.Top-1]; //Lay du lieu tai Top
}

int Pop(Stack &S) //Loai bo phan tu khoi Stack
{
	if (!Isempty(S))
	{
		S.Top --; //Giam Top
		return S.Data[S.Top]; //Lay du lieu tai Top
	}
}

void Input (Stack &S)
{
	int n;
	item x;
	do
	{
		printf("Nhap so phan tu cua Stack (<%d) :",Max);
		scanf("%d",&n);
	} while (n>Max || n<1);
	for (int i=0; i<n; i++)
	{
		printf("Nhap phan tu thu %d : ", i+1);
		scanf("%d",&x);
		Push(S,x);
	}
}

void Output(Stack S)
{
	for (int i=S.Top-1; i>=0; i--)
		printf("%d   ",S.Data[i]);
	printf("\n");
}

int main()
{
    Stack S;
    Init(S);
    Input(S);
    Output(S);

    int lua_chon;
    printf("Moi ban chon phep toan voi DS LKD:");
    printf("\n1: Kiem tra Stack rong");
    printf("\n2: Kiem tra Stack day");
    printf("\n3: Them phan tu vao Stack");
    printf("\n4: Xoa phan tu trong Stack");
    printf("\n5: Xuat Stack");
    printf("\n6: Thoat");
    do
    {
        printf("\nBan chon: ");
        scanf("%d",&lua_chon);
		switch (lua_chon)
		{
			case 1:
			{
				if (Isempty(S)) printf("Stack rong !");
				else printf ("Stack khong rong !");
				break;
			}
			case 2:
			{
				if (Isfull(S)) printf("Stack day !");
				else printf ("Stack chua day !");
				break;
			}
			case 3:
			{
				item x;
				printf ("Nhap phan tu can chen vao DS: ");
				scanf("%d",&x);
				Push(S,x);
				break;
			}
			case 4:
			{
				Pop(S);
				break;
			}
			case 5: 
			{
				Output(S);
				break;
			}
			case 6: break;
		}
    }while (lua_chon !=6);
    return 0;
}

link redundancy

2. Stack installed on the cursor

Stack on the stack pointer is still functioning normally, but because it uses link pointer to allocate and manage, no excess or lack of anything.

stack pointer

2.1 Building structures

typedef int item; //kieu du lieu
struct Node
{
	item Data; //du lieu
	Node *Next; //link
};
typedef struct Stack
{
	Node *Top;
};

2.2 Initialize empty list, check list is empty, length list

void Init (Stack &S) //khoi tao Stack rong
{
	S.Top = NULL;
}

int Isempty(Stack S) //kiem tra Stack rong
{
	return (S.Top == NULL);
}

int Len (Stack S)
{
	Node *P = S.Top;
	int i=0;
	while (P != NULL) //trong khi chua het Stack thi van duyet
	{
		i++;
		P = P->Next;
	}
	return i;
}

2.3 Create 1 Node

Node *MakeNode(item x) //tao 1 Node
{
	Node *P = (Node*) malloc(sizeof(Node));
	P->Next = NULL;
	P->Data = x;
	return P;
}

2.4 Inserting elements into Stack (Push)

To insert the element into the stack pointer just for that point and Top Note, Then point it is finished Top

push

void Push(Stack &S, item x) //them phan tu vao Stack
{
	Node *P = MakeNode(x);
	P->Next = S.Top;
	S.Top = P;
}

2.5 Get data on Top but not deleted (Peak)

int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa
{
	return S.Top->Data;
}

2.6 Remove and retrieve data at Top (Pop)

One need only point to the cursor position Top 2 stop.

pop

int Pop(Stack &S) //Loai bo phan tu khoi Stack
{
	if (!Isempty(S))
	{
		item x = S.Top->Data; //luu lai gia tri
		S.Top = S.Top->Next; //Xoa phan tu Top
		return x;
	}
}

2.7 Complete program (full code)

#include <stdlib.h>
#include <stdio.h>

typedef int item; //kieu du lieu
struct Node
{
	item Data; //du lieu
	Node *Next; //link
};
typedef struct Stack
{
	Node *Top;
};

void Init (Stack &S); //khoi tao Stack rong
int Isempty(Stack S); //kiem tra Stack rong
int Len (Stack S); //Do dai Stack
void Push(Stack &S, item x); //them phan tu vao Stack
int Peak(Stack S); //Lay phan tu o dau Stack nhung khong xoa
int Pop(Stack &S); //Loai bo phan tu khoi Stack
void Input (Stack &S); //Nhap Stack
void Output(Stack S); //Xuat Stack
Node *MakeNode(item x); //Tao 1 Node

void Init (Stack &S) //khoi tao Stack rong
{
	S.Top = NULL;
}

int Isempty(Stack S) //kiem tra Stack rong
{
	return (S.Top == NULL);
}

int Len (Stack S)
{
	Node *P = S.Top;
	int i=0;
	while (P != NULL) //trong khi chua het Stack thi van duyet
	{
		i++;
		P = P->Next;
	}
	return i;
}

Node *MakeNode(item x) //tao 1 Node
{
	Node *P = (Node*) malloc(sizeof(Node));
	P->Next = NULL;
	P->Data = x;
	return P;
}

void Push(Stack &S, item x) //them phan tu vao Stack
{
	Node *P = MakeNode(x);
	P->Next = S.Top;
	S.Top = P;
}

int Peak(Stack S) //Lay phan tu o dau Stack nhung khong xoa
{
	return S.Top->Data;
}

int Pop(Stack &S) //Loai bo phan tu khoi Stack
{
	if (!Isempty(S))
	{
		item x = S.Top->Data; //luu lai gia tri
		S.Top = S.Top->Next; //Xoa phan tu Top
		return x;
	}
}

void Input (Stack &S) //nhap danh sach
{
    int i=0; 
    item x;
    do
    {
        i++;
        printf ("Nhap phan tu thu %d : ",i);
        scanf("%d",&x);
        if (x != 0) Push(S,x);
    } while(x != 0); //nhap 0 de ket thuc
}

void Output(Stack S)
{
	Node *P = S.Top;
	while (P != NULL)
	{
		printf("%d   ",P->Data);
		P = P->Next;
	}
	printf("\n");
}

int main()
{
    Stack S;
    Init(S);
    Input(S);
    Output(S);

    int lua_chon;
    printf("Moi ban chon phep toan voi DS LKD:");
    printf("\n1: Kiem tra Stack rong");
    printf("\n2: Do dai Stack");
    printf("\n3: Them phan tu vao Stack");
    printf("\n4: Xoa phan tu trong Stack");
    printf("\n5: Xuat Stack");
    printf("\n6: Thoat");
    do
    {
        printf("\nBan chon: ");
        scanf("%d",&lua_chon);
		switch (lua_chon)
		{
			case 1:
			{
				if (Isempty(S)) printf("Stack rong !");
				else printf ("Stack khong rong !");
				break;
			}
			case 2:
			{
				printf("Do dai Stack: %d",Len(S));
				break;
			}
			case 3:
			{
				item x;
				printf ("Nhap phan tu can chen vao DS: ");
				scanf("%d",&x);
				Push(S,x);
				break;
			}
			case 4:
			{
				Pop(S);
				break;
			}
			case 5: 
			{
				Output(S);
				break;
			}
			case 6: break;
		}
    }while (lua_chon !=6);
    return 0;
}

link redundancy

3. Using Stack is available in C

In C has built the method (jaw) related to Stack, It is only the declaration and use

#include <iostream> // io
#include <stack> // use stack

using namespace std;

int main() {
	stack <int> S; // khai bao Stack voi kieu du lieu la int
	
	for (int i = 0; i < 10; i++) {
		S.push(i*78 + 26); // chen cac phan tu vao stack
	}
	
	cout << "Do dai stack: " << S.size() << "\n";
	
	// xuat tat ca phan tu trong stack
	while (!S.empty()) { 	// trong khi stack chua trong
		int x = S.top(); 	// lay gia tri top
		S.pop(); 			// loai bo khoi stack
		cout << x << "  ";
	}
	cout << "\n";
	
	return 0;
}

4. Examples of applications of Stack

Stack has many applications, The following is 1 application of transformation of. The following code will transfer the base 10 to base x keyboard input

#include <iostream> // io
#include <stack> // use stack

using namespace std;

int main() {
	char num[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	stack <char> S; // khai bao Stack voi kieu du lieu la int
	
	int coSo, so, du;
	
	cout << "Nhap so can chuyen: ";
	cin >> so;
	
	cout << "Nhap co so can chuyen: ";
	cin >> coSo;
	
	// chuyen doi bang cach dua vao Stack
	while(so) {
		du = so % coSo;
		S.push(num[du]);
		so /= coSo;
	}
	
	// Xuat ra
	while(!S.empty()) {
		cout << S.top();
		S.pop();
	}
	
	return 0;
}

[rps-include post=”2703″ shortcodes=”false”]