PHP Code:
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include <dos.h>
#include <iostream.h>
#include <iostream.h>
#include <stdlib.h>
#include <io.h>
//-----------------------------------------------------------------------------//
struct data
{
char tu[25];
char nghia[50];
} ;
struct node
{
data info;
struct node * next;
} ;
struct list
{
node * head;
node * tail;
} ;
list Q; //tao ds ten Q
node * new_element;
data x; //KB phan tu x can tim
//------ Ham khoi tao 1 ds chua co pt nao -----------
void khoitao(list &Q)
{
Q.head = NULL;
Q.tail = NULL;
}
//--------------------------------
node *get_node(data x)
{
node *p;
p= (node*)malloc(sizeof(node));
if (p == NULL)
{
printf("Khong du bo nho !");
exit(1);
}
else
{
p -> info = x;
p -> next = NULL;
}
return p;
}
void hien_thi(list Q)
{
node *p;
p = Q.head;
printf("\n%7s%7s","tu","nghia");
printf("\n----------------------------------------------------------");
while(p!= NULL)
{
printf("\n%4s",p->info.tu);
printf("%12s",p->info.nghia);
p= p -> next;
}
printf("\n\n");
}
//Ham de in thong tin du lieu trong mot nut cua danh sach
void in(data x)
{
printf("\n%10s",x.tu);
printf("%10s",x.nghia);
}
//Ham cho phep chen them phan tu moi vao dau danh sach
void InsertFirst(list &Q, node *new_element)
{
if(new_element==NULL)
{
printf("Ko co phan tu de chen vao");
exit(1);
}
else
{
if ( Q.head == NULL ) //neu danh sach rong
{
Q.head = new_element;
Q.tail = Q.head;
}
else //danh sach khong rong
{
new_element -> next = Q.head;
Q.head = new_element;
}
}
}
//------------------Chen 1 pt vao cuoi day----------------------------------
void InsertLast(list &Q,node *new_element)
{
if (Q.head == NULL)
{
Q.head = new_element;
Q.tail = Q.head; }
else
{
Q.tail -> next = new_element;
Q.tail = new_element; }
}
//-------------------Chen 1 phan tu moi vao sau pt q------------------------
void InsertAfter(list &Q, node *q, node *new_element)
{
if( q!=NULL)
{
new_element -> next = q -> next;
q -> next = new_element;
if (q == Q.tail) Q.tail = new_element;
}
}
//------------------Huy 1 pt o dau DS------------------------------------
void RemoveHead( list &Q )
{
node *p;
if (Q.head != NULL)
{ p = Q.head;
Q.head = Q.head -> next;
free(p);
if ( Q.head == NULL ) Q.tail = NULL;
}
}
//----------HUY PHAN TU DUNG SAU PHAN TU Q ------------
void RemoveAfter(list &Q, node *q )
{
node *p;
if (q != NULL)
{ p = q -> next;
if (p != NULL)
{
if (p == Q.tail) { q->next = NULL; Q.tail = q;}
q -> next = p -> next;
free(p);
}
}
else RemoveHead(Q);
}
//---------HUY PHAN TU CO KHOA K - tu LA K ------------
int RemoveNode(list &Q,char k[])
{ node *p = Q.head;
node *q = NULL;
while( p != NULL)
{
if (strcmp(p -> info.tu,k)==0) break;
q = p; p = p->next;
}
if (p == NULL) return 0; //Khong tim thay k
if (q != NULL)
{
if(p == Q.tail) Q.tail = q;
q->next = p->next;
free(p);
}
else //p la phan tu dau xau
{
Q.head = p -> next;
if (Q.head == NULL) Q.tail = NULL;
}
return 1;
}
//------------------------------------
node *Search(list &Q,char k[])
{ node *p;
p = Q.head;
while (( p != NULL) && (strcmp(p -> info.tu,k) != 0))
p = p -> next;
return p;
}
//--------------------------------
//Ham sx danh sach cac tu theo thu tu tang dan
void ListSortInterchange(list &Q)
{
node *p, *q; //p và q la hai bien dieu khien
data tg;
p = Q.head;
while (p != NULL)
{
q = p -> next;
while(q!=NULL)
{
if(p->info.tu > q->info.tu)
{
tg=p->info;
p->info=q->info;
q->info=tg;
}
q = q->next;
}
p=p->next;
}
}
// ham them tu vao tu dien =-=--=-
void chenmang(char *a[],int &n,char ch[])
{ int i,h;
a[n]=(char*)malloc(25);
for(i=n;i>0;i--)
{h=strcmpi(a[i-1],ch);
if(h>0) strcpy(a[i],a[i-1]);
if(h<0) {strcpy(a[i],ch);break;}
}
if(i==0){strcpy(a[i],ch);}
n++;
}
//-----------------------------------------------------------------------
void main ()
{
int n; //Xac dinh so phan tu dau tien cua tu dien duoc luu tru
int ok;
char select;
khoitao(Q);
clrscr ();
do {
printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
printf("\n+ +");
printf("\n+ 0. dung chuong trinh +");
printf("\n+ 1. tac gia +");
printf("\n+ 2. tao danh sach tu dien +");
printf("\n+ 3. chen mot tu vao danh sach +");
printf("\n+ 4. xoa mot tu khoi tu dien +");
printf("\n+ 5. tim 1 tu trong tu dien +");
printf("\n+ 6. TIM KIEM MOT PHAN TU TRONG DANH SACH +");
printf("\n+ 7. SAP XEP DANH SACH +");
printf("\n+ 8. sua tu dien +");
printf("\n+ +");
printf("\n+ ..:: PHAT TRIEN BOI le duy tung ::.. +");
printf("\n+ +");
printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("\nban muon vao xem phan nao ? : ");
scanf("%d",&select);
switch (select)
{
case 0:
{ exit(1);
break;
}
case 1: printf("\n");
printf("######################################");
printf("\n@ @@@");
printf("\n@ TAC GIA CUA BAI TAP : ");
printf("\n@ Sinh Vien :LE DUY TUNG ");
printf("\n@ Lop : CN1K6C ");
printf("\n@ Home page : maiyeunguoi.go.to ");
printf("\n@ @@@");
printf("\n######################################");
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
break;
case 2:
{
printf("\nNHAP DANH SACH - TAO PT DAU TIEN\n");
printf("\n---------------------------------------------\n");
printf("\n NHAP SO TU: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
//-------------------
{
printf("\ntu: "); scanf("%s",&x.tu);
//-----------------------------
printf("\nnghia: ");
fflush(stdin);
gets(x.nghia);
new_element=get_node(x);
InsertFirst(Q,new_element);
// ----------------------------
}
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
}
break;
case 3 :
node *q;
char ok1;
char k[5];
printf("\nTHEM PHAN TU VAO DANH SACH ");
{
printf("\n---------------------------------------------------------\n");
printf("\n NHAP THONG TIN CAN BO SUNG VAO \n");
printf("\ntu "); scanf("%s",&x.tu);
//------------------------------------------
// do {
printf("\nnghia ");
fflush(stdin);
gets(x.nghia);
//------------------------------------------
new_element=get_node(x);
printf("THEM : DAU = 1 - CUOI = 2 - SAU Q = 3");
printf("\nXIN CHON CACH THEM: ");
scanf("%s",&ok1);
if (ok1 == '1')
{
InsertFirst(Q,new_element);
printf("\n KET QUA DANH SACH DA DUOC BO SUNG \n");
hien_thi(Q);
printf("\n-------------------------------");
}
else if (ok1 == '2')
{
InsertLast(Q,new_element);
printf("\n KET QUA DANH SACH DA DUOC BO SUNG \n");
hien_thi(Q);
printf("\n-------------------------------");
}
else if (ok1 == '3')
{
printf("Nhap tu dung truoc pt can them: ");
scanf("%s",&k);
node *q;
q = Search(Q,k);
InsertAfter(Q,q,new_element);
printf("\n KET QUA DANH SACH DA DUOC BO SUNG \n");
hien_thi(Q);
printf("\n-------------------------------");
}
else
printf("\nBAN DA NHAP SAI VUI LONG THUC HIEN LAI");
//--------------------------------------------------------
}
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
break;
//---------------------------------------------------
case 4: //Huy phan tu dau danh sach
printf("\nHUY PHAN TU TRONG DANH SACH ");
printf("\n---------------------------------------------");
char ok2;
{
printf("\nBAN MUON HUY PHAN TU NAO:\n");
printf("\nPHAN TU DAU CHON 1");
printf("\nPHAN TU SAU Q CHON 2");
printf("\nPHAN TU KHOA K CHON 3");
printf("\nCHON MOT CONG VIEC: ");
scanf("%s",&ok2);
if (ok2 == '1')
{ printf("\n-------------------------------\n");
RemoveHead(Q);
printf("\n KET QUA DANH SACH DA BI XOA PHAN TU DAU \n");
hien_thi(Q);
printf("\n-------------------------------");
}
else if (ok2 == '2')
{ char k[5];
printf("\nHUY PHAN TU DUNG SAU PHAN TU Q");
//Huy phan tu dung sau pt Q
printf("\n-------------------------------\n");
printf("\n Nhap tu can huy:");
scanf("%s",&k);
node *q;
q = Search(Q,k);
RemoveAfter(Q,q);
printf("\n KET QUA DANH SACH DA BI XOA PHAN TU DAU \n");
hien_thi(Q);
printf("\n-------------------------------");
}
else if (ok2 == '3')
{
printf("\nHUY PHAN TU CO KHOA K - MSV LA K");
//Tim kiem mot phan tu trong DS
{ char k[5];
printf("\n Nhap tu can tim kiem:");
scanf("%s",&k);
if(RemoveNode(Q,k) == -1)
printf("\n Ko tim thay tu co nghia la %d trong DS",k);
else {
printf("\nDay co pt %s xuat hien tai vi tri thu %s",k,RemoveNode(Q,k)+1);
hien_thi(Q); }
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
}
}
}
//--------------------------------------------------
break;
case 5:
printf("\ntra tu dien ");
//Tim kiem mot phan tu trong DS
{ char k[6];
printf("\n Nhap tu can tra:");
scanf("%s",&k);
node *q;
q = Search(Q,k);
if(q==NULL) printf("\n Ko tim thay tu %s trong DS",k);
else
{
printf("\nCo tu %s trong DS",k);
printf("\n%12s%12s", "tu","nghia");
printf("\n---------------------------------------------------------------------");
in(q->info);
printf("\n");
printf("\n---------------------------------------------------------------------");
}
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
//---------------------------------------------------
break;
case 6:
printf("\nSAP XEP DANH SACH");
{
printf("\n------------------------------- \n");
ListSortInterchange(Q);
printf("\n KET QUA DANH SACH SAU KHI SAP XEP \n");
hien_thi(Q);
printf("\n------------------------------- \n");
}
default:printf("\nBAN DA CHON SAI VUI LONG CHON LAI.\n");
printf("\n\nCHON PHIM 0 DE TIEP TUC : ");
printf("\n\nCHON PHIM 1-9 DE DUNG CHUONG TRINH: ");
scanf("%d",&ok);
//----------------------------------------------------
}
}
} while (ok<= 0) ;
getch();
}
Bạn chú ý lần sau bỏ code vào thẻ code nhé :