HÀNG ĐỢI

Màu nền
Font chữ
Font size
Chiều cao dòng

//////////////////////////Hang Doi////////////////////////////

#include<stdio.h>

#include<conio.h>

#include<string.h>

/*Never give up*/

//cai dat hang doi bang

//cua truc mot node

struct Node{

// tin cua node

int info;

//chi toi node tiep theo

Node *next;

};

//lp hang doi

class Queue{

private:

//chi toi dau hang doi

Node *first;

//chi toi cuoi hang doi

Node *last;

public:

//khoi tao hang doi khong tham so

Queue();

//khoi tao hang doi co tham so voi node co noi dung la x

Queue(int x );

//them vao hang doi

void Push(int x);

//lay ra khoi hang doi

int Pop();

//kiem tra hang doi co rong khong

int Empty(){

if(first == NULL){

return 1;

}

return 0;

}

};

//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Queue :: Queue(){

first = NULL;

last = NULL;

}

Queue :: Queue(int x){

Node *tmp = new Node;

tmp->info = x;

//thiet lap lien ket

first = tmp;

//thiet lap lien ket

last = tmp;

}

void Queue :: Push(int x){

//node duoc them vao

Node *tmp = new Node;

tmp->info = x;

tmp->next = NULL;

if(first == NULL){

first = tmp;

last = tmp;

}

//thiet lap lien ket

last->next = tmp;

last = tmp;

}

int Queue :: Pop(){

if(first == NULL)

{

printf("

Hang trong khong the lay ra");

}else{

int x = first->info;

first = first->next;

return x;

}

}

/*

*/

void main()

{

int i;

Queue H;

for(i=1;i<=10; i++){

H.Push(i);

}

while( !H.Empty() ){

printf("

%d ", H.Pop() );

}

getch();

return 0;

}

////////////////////////////////////////HANG DOI MANG TINH~///////////////////////

#include<stdio.h>

#include<conio.h>

#include<string.h>

/*Never give up*/

//hang doi su dung mang tinh

class Queue{

private:

int n;//so phan tu trong hang doi

int a[10]; //luu cac phan tu cua hang doi

int dau, cuoi; //dau va cuoi cua hang doi

public:

//khoi tao ko tham so

Queue(){

n = 0;

dau = 0;

cuoi = 0;

}

//kiem tra rong

int Empty(){

if(n==0)

return 1;

return 0;

}

//kiem tra day

int Full(){

if(n==9)

return 1;

return 0;

}

//Them phan tu vao hang

void Them(int x){

//neu hang day

if(Full()){

printf("

Hang day");

}

//hang ko day

if(cuoi<9){

a[cuoi] = x;

cuoi ++;

n++;

}

if(cuoi == 9){

a[0] = x;

cuoi = 0;

n++;

}

}

int Lay(){

if(Empty() ){

printf("

Hang rong khong lay duoc");

return -10000;

}

int tmp = a[dau];

dau++;

n--;

return tmp;

}

};

/*****************************************************

**/

void main()

{

Queue Q;

int i;

for(i = 0; i<= 9; i++){

Q.Them(i);

}

while(!Q.Empty() ){

printf("

%d",Q.Lay() );

}

getch();

return 0;

}

Bạn đang đọc truyện trên: Truyen2U.Pro

#chikaki