Diễn đàn sinh viên công nghệ thông tin, chia sẻ, giao lưu, học hỏi. Kết nối ... Những ngôn ngữ cơ bản mà bạn cần phải nắm nếu muốn thành 1 lập trình viên ...VuaTenMien.Com
Thứ Năm, 19 tháng 2, 2015
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();
}
Struct: http://codepad.org/dpnY8meC
#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;
}
Đăng ký:
Nhận xét (Atom)
Bài đăng phổ biến
-
import java.io.*; public class Test_Ngto{ public static void main(String args[]){ InputStream is = System.in; ...
-
Website-Watcher 2011 sẽ theo dõi và thông báo cho bạn biết mỗi khi trên website, forum, blog,… ưa thích có tin bài mới. Nhờ Website-Watcher...
-
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespa...
-
import java.io.*; public class ReadLine{ public static void main(String args[]){ InputStreamReader isr = new InputStreamReader...
-
1. Hãy xây dựng lớp Diem cùng với các đối tượng điểm trong mặt phẳng và các phương thức sau: - Phương thức khởi tạo - Phương...
-
<html> <head> <title>Giai phuong trinh bac 2</title> <script language="JavaScript"> function giai(fo...
-
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespa...
-
<%@ Page Language="VB" Debug="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespa...
-
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<co...
-
import java.io.*; public class Test_Tng{ public static void main(String args[]){ InputStream is = System.in; ...