Thứ Tư, 18 tháng 2, 2015

So sánh 2 cách tạo stack bằng mảng và bảng kiểu cấu trúc

 

So sánh 2 cách tạo stack bằng mảng và bảng kiểu cấu trúc nhé

Mảng: http://codepad.org/rTA0NJgL
#include <stdio.h>
#include<conio.h>
#define MAX 100
int stack[MAX + 1]; int top;
void khoi_tao_stack(){
top = -1;
}
int is_empty(){
return (top == -1);
}
int is_full(){
return (top == MAX);
}
int push(int value){
if (top < MAX) stack[++top] = value;
return top;
}
int pop(int *value){
*value = stack[top--];
return top;
}
int main(){
int k;
khoi_tao_stack();
printf("\nNhap cac phan tu vao stack (-1 de ket thuc) : ");
do {
scanf("%d", &k);
if (k != -1) push(k);
} while (k != -1 && !is_full());
printf("\n\nLay cac phan tu tu stack ra : ");
while (!is_empty()){
pop(&k);
printf("%d ", k);
}
return 0;
getch();
}

#include <conio.h> 
#include <stdio.h>
#define max 100
#define TRUE 1
#define FALSE 0
struct stack {
int top;
char st[max];
};
int empty(struct stack *ps) {
if(ps->top == -1)
return (TRUE);
else
return (FALSE);
}
void push(struct stack *ps,char a) {
if(ps->top== max-1)
printf("stack day");
else
ps->st[++(ps->top)] = a;
}
char pop(struct stack *ps) {
if(empty(ps))
printf("stack rong");
else return(ps->st[ps->top--]);
}
int main()
{
struct stack s;
s.top=-1;
for(int i=1;i<=5;i++)
push(&s,i);
while (!empty(&s))
printf("%d",pop(&s));
getch();
return 0;
}

Bài đăng phổ biến