สแต็กคือโครงสร้างข้อมูลที่จัดเก็บองค์ประกอบ มีการดำเนินการสองอย่างบนสแต็ก พุชที่เพิ่มองค์ประกอบใหม่ให้กับสแต็ก ป๊อปที่ลบองค์ประกอบออกจากสแต็ก
สแต็คสามารถเติบโตขึ้นและลงตามลักษณะของโปรแกรมที่ใช้งาน โปรแกรมหาทิศทางการเติบโตของ stack ในโปรแกรม
อัลกอริทึม
Step 1: Create a local variable in the main function. Step 2: Create a function that with a local variable. Step 3: Call the function from the main function. And then compare the local variables of in both these functions. Step 4: Compare the address of local variables in main and the function. Step 5: If address variable in main is more than local variable of the function, then stack grows upward otherwise it grows downward.
ตัวอย่าง
#include<stdio.h>
void fun(int *main_local_addr){
int fun_local;
if (main_local_addr < &fun_local)
printf("Stack grows upward\n");
else
printf("Stack grows downward\n");
}
int main(){
int main_local;
fun(&main_local);
return 0;
} ผลลัพธ์
Stack grows downward