Nhap Xuat Them Xoa Hang Doi
// HangDoi.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
struct Queue
{
int info;
struct Queue *next;
};
typedef struct Queue *QUEUE;
//Khai Bao Prototype
void InitializeQ (QUEUE *pfirst, QUEUE *plast);
int EmptyQ (QUEUE *pfirst);
QUEUE GetQueue();
void PushQ (QUEUE *pfirst, QUEUE *plast, int x);
int PopQ (QUEUE *pfirst, QUEUE *plast);
void TopQ (QUEUE *pfirst);
//==============================
void main()
{
QUEUE pfirst, plast;
int x, y, vt, chon, n, i;
char ch;
InitializeQ (&pfirst, &plast);
do
{
printf("\n\n\t CHUONG TRINH DEMO HANG DOI THAO TAC TREN DAY NGUYEN !");
printf("\n\t1 : Khoi Tao Hang Doi !");
printf("\n\t2 : Kiem Tra Hang Doi Rong !");
printf("\n\t3 : Tao Hang Doi !");
printf("\n\t4 : Xuat Va Huy Hang Hoi !");
printf("\n\t5 : Them 1 Phan Tu Vao Hang Doi !");
printf("\n\t6 : Xoa 1 PTu Ra Khoi hang Doi !");
printf("\n\t7 : Truy Xuat Noi Dung O Dinh Hang Doi !");
printf("\n\t0 : THOAT KHOI CHUONG TRINH !");
printf("\n\tBan Chon Chuc Nang Nao ? ");
scanf ("%d", &chon);
switch (chon)
{
case 1:
{
InitializeQ (&pfirst, &plast);
printf("\n\t Hang Doi Da Duoc Khoi Tao !");
getch();
break;
}
case 2:
{
printf("\n\t Kiem Tra Hang Doi Rong !");
if (EmptyQ (&pfirst))
printf("\n\t Hang Doi Rong !");
else
printf("\n\t Hang Doi Khong Rong !");
getch();
break;
}
case 3:
{
printf("\n\t Tao Hang Doi !");
InitializeQ (&pfirst, &plast);
printf("\n\t Nhap SPTu Trong Hang Doi !");
scanf ("%d", &n);
printf("\n\t Tao Hang Doi !");
for (i = 1; i <= n; i++)
{
printf("\t\t\n Nhap PTu Thu %d : ", i);
scanf("%d", &x);
//Neu La Cau Truc : x = Nhap1PTu();
PushQ(&pfirst, &plast, x);
}
getch();
break;
}
case 4:
{
printf("\n\t Xuat Va Huy Hang Doi !");
if (EmptyQ (&pfirst))
printf("\n\t Hang Doi Rong !");
else
{
printf("\n\t Noi Dung Hang Doi Vua Nhap !");
while(EmptyQ (&pfirst) == 0)
{
TopQ (&pfirst);
y = PopQ (&pfirst, &plast);
}
}
getch();
break;
}
case 5:
{
printf("\n\t Them 1 PTu Vao Hang Doi !");
printf("\n\t Nhap Noi Dung X Can Them : ");
scanf ("%d", &x);
PushQ(&pfirst, &plast, x);
getch();
break;
}
case 6:
{
printf("\n\t Xoa PTu Dinh !");
y = PopQ (&pfirst, &plast);
getch();
break;
}
case 7:
{
printf("\n\t Truy Xuat Noi Dung PTu Dinh !");
TopQ (&pfirst);
getch();
break;
}
}//KT Switch
}while (chon > 0);
getch();
}//KThuc Ham Main
//==============================
//Cai Dat Cac Prototype
//======================================
void InitializeQ (QUEUE *pfirst, QUEUE *plast)
{
*pfirst = *plast = NULL;
}
//========================================
int EmptyQ (QUEUE *pfirst)
{
if (*pfirst == NULL)
return 1; //Rong
return 0; //Khong Rong
}
//========================================
QUEUE Getnode()//Cap Phat Vung Nho
{
QUEUE p;
p = (QUEUE) malloc (sizeof (struct Queue));
p -> next = NULL;
return p;
}
//========================================
int PopQ (QUEUE *pfirst, QUEUE *plast)
{
QUEUE p;
int x;
if (EmptyQ(pfirst))
printf("\n\t DS Rong, Khong Xoa Duoc !");
else
{
p = *pfirst;
x = p -> info;
if (p -> next == NULL) //DS Co 1 Phan Tu
*pfirst = *plast = NULL;
else
*pfirst = p -> next;
return x;
}
}
//========================================
void PushQ (QUEUE *pfirst, QUEUE *plast, int x)
{
QUEUE p;
p = Getnode();
p -> info = x;
p -> next = NULL;
if (EmptyQ (pfirst))
{
*pfirst = p;
*plast = *pfirst;
}
else
{
(*plast) -> next = p;
*plast = p;
}
}
//========================================
void TopQ (QUEUE *pfirst)
{
if (EmptyQ (pfirst))
printf("\n\t Hang Doi Rong !");
else
{
printf("%5d", (*pfirst) -> info);
//Neu La Cau Truc In1PTu
}
}
0 nhận xét:
Đăng nhận xét