Untitled

 avatar
unknown
plain_text
2 years ago
4.1 kB
4
Indexable
#include <iostream>
#include <string.h>

using namespace std;

struct giaovien {
	char maso[32];
	char hoten[32];
	char namsinh[32];
	
	int sotiet;
	int Luong;
};

typedef giaovien ElementType;

struct Node {
	ElementType Element;
	Node *Next;
};
typedef Node *PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;

void Makenull_List(List &L);
int IsEmpty_List(List L);
int IsLast(Position P);
Position Locate(ElementType X, List L);
void Delete_List(ElementType X, List L);
Position FindPrevious(ElementType X, List L);
void Insert_List(ElementType X, Position P, List L);
void Delete_All_List(List );
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);

void Nhap(ElementType &X);
void Xuat(ElementType X);
void NhapGV(List L);
void XuatGV(List L);
void ds_gv_tangdan(List L);
Position timkiem_giaovien(const char *maso, List L);

// ------------------------------------------------

void Makenull_List(List &L) {
	L= new Node;
	if(L==NULL) {
		cout<<"Khong du bo nho";
		return;
	}
	L->Next = NULL;
}
int IsEmpty_List(List L) {
	return L->Next==NULL;
}
int IsLast(List P) {
	return P->Next==NULL;
}
Position Locate(ElementType X, List L) {
	Position P;
	P=L->Next;
	while(P!=NULL&&strcmp(P->Element.maso,X.maso)!=0)
		P=P->Next;
	return P;
}
void Insert_List(ElementType X, Position P, List L) {
	Position TmpCell;
	TmpCell=new Node;
	if(TmpCell==NULL)
		cout<<"Out of space!!!";
	TmpCell->Element=X;
	TmpCell->Next=P->Next;
	P->Next=TmpCell;
}
void Delete_List(ElementType X, List L) {
	Position P, TmpCell;

	P=FindPrevious(X,L);
	if(!IsLast(P)) {
		TmpCell=P->Next;
		P->Next=TmpCell->Next;
		delete(TmpCell);
	}
}
Position FindPrevious(ElementType X, List L) {
	Position P;
	P=L;
	while(P->Next != NULL && strcmp(P->Next->Element.maso,X.maso)!=0)
		P=P->Next;
	return P;
}
Position Header(List L) {
	return L;
}
Position First(List L) {
	return L->Next;
}
Position Advance(Position P) {
	return P->Next;
}
ElementType Retrieve(Position P) {
	return P->Element;
}

// ------------------------------------------------

void Nhap(ElementType &X) {
	fflush(stdin);
	cin.clear();
	cout<<"Nhap ma so giao vien : ";
	cin.getline(X.maso, 32);
	
	fflush(stdin);
	cin.clear();
	cout<<"Nhap ten giao vien : ";
	cin.getline(X.hoten, 32);
	
	fflush(stdin);
	cin.clear();
	cout<<"Nhap nam sinh giao vien : ";
	cin.getline(X.namsinh, 32);
	
	cout<<"Nhap so tiet day : ";
	scanf("%d", &X.sotiet);
	
	X.Luong = X.sotiet * 90000;
}
void Xuat(ElementType X) {
	cout<<X.maso<<"\t"<<X.hoten<<"\t"<<X.namsinh<<"\t"<<X.sotiet<<"\t"<<X.Luong<<"\t"<<endl;
}
void NhapGV(List L) {
	int n;
	cout<<"Nhap so luong giao vien : ";
	scanf("%d", &n);
	
	ElementType X;
	Position P = Header(L);
	
	for(int i = 0; i < n; i++) {
		Nhap(X);
		Insert_List(X, P, L);
	}
}
void XuatGV(List L) {
	Position P = First(L);
	while(P != NULL) {
		Xuat(P->Element);
		P = Advance(P);	
	}
}

void HoanVi(ElementType &X, ElementType &Y) {
	ElementType Z = X;
	X = Y;
	Y = Z;
}

void ds_gv_tangdan(List L) {
	Position P = First(L);
	while(P != NULL) {
		Position Q = Advance(L);
		while(Q != NULL) {
			if(P->Element.maso < Q->Element.maso) {
				HoanVi(P->Element, Q->Element);
			}
			Q = Advance(Q);
		}
		P = Advance(P);
	}
}

Position timkiem_giaovien(const char *maso, List L) {
	Position P = First(L);
	while(P != NULL) {
		if(strcmp(maso, P->Element.maso) == 0) 
			return P;
		P = Advance(P);
	}
	return NULL;
}
// ---------------------------------------
int main() {
	
	List L;
	Makenull_List(L);
	
	NhapGV(L);
	XuatGV(L);
	
	ds_gv_tangdan(List L);
	
	fflush(stdin);
	cin.clear();
	char maso_gv[32];
	cout<<"Nhap ma so giao vien can tim : ";
	cin.getline(maso_gv, 32);
	Position K = timkiem_giaovien(maso_gv, L);
	
	if(K != NULL) 
		Xuat(K->Element);
	else 
		cout<<"Khong tim thay ten mat hang nay."<<endl;
	return 1;
}
Editor is loading...