Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.1 kB
2
Indexable
Never
#include<stdio.h>
#include<iostream>
struct SinhVien
{
	char MS[16];
	char Hoten[32];
	float Diem;
};
typedef  SinhVien ElementType;
struct Node
{
	ElementType Element;
	Node* Next;	
};
typedef Node* PtrToNode;
typedef PtrToNode Position;
typedef PtrToNode List;
void MakeNullList(List &L);
int IsEmptyList(List L);
ElementType Retrieve(Position P);
void InsertList(ElementType X, Position P, List L);
void NhapDS(List L);
void XuatDS(List L);
void Hoanvi(ElementType&x, ElementType&y);
void Sapxep(List L);
int main()
{
	List L;
	MakeNullList(L);
	NhapDS(L);
	XuatDS(L);
	Sapxep(L);
	std::cout<<"\nDanh sach sau khi sap xep: ";
	XuatDS(L);
	return 0;
}
void MakeNullList(List &L)
{
	L=new Node;
	L->Next=NULL;
}
int IsEmptyList(List L)
{
	return L->Next==NULL;
}
ElementType Retrieve(Position P)
{
	return P->Element;
}
void InsertList(ElementType X, Position P, List L)
{
	Position tempCell=new Node;
	if(tempCell==NULL)
	{
		std::cout<<"Khong du vung nho";
		return;
	}
	tempCell->Element=X;
	tempCell->Next=P->Next;
	P->Next=tempCell;
}
void Nhap(ElementType& X)
{
	std::cout<<"MS: ";
	fflush(stdin);
	gets(X.MS);
	std::cout<<"Ho ten: ";
	gets(X.Hoten);
	std::cout<<"Diem: ";
	std::cin>>X.Diem;
}
void NhapDS(List L)
{
	int n;
	std::cout<<"So phan tu: ";
	std::cin>>n;
	Position P=L;//Them dau ds
	for(int i=0;i<n;i++)
	{
		ElementType X;
		std::cout<<"Nhap mot so nguyen: ";
		Nhap(X);
		InsertList(X,P,L);
		//P=P->Next;//Them cuoi ds. 
	}	
	
}
void Xuat(ElementType X)
{
	std::cout<<X.MS<<"\t"<<X.Diem<<"\n";
}
void XuatDS(List L)
{
	Position P=L->Next;
	while(P!=NULL)
	{
		ElementType X=Retrieve(P);
		Xuat(X);
		P=P->Next;//node ke tiep
	}
}

void Hoanvi(ElementType&x, ElementType&y)
{
	ElementType z=x;
	x=y;
	y=z;
}
void Sapxep(List L)
{
	
	Position P,Q;
	for(P=L->Next;P!=NULL;P=P->Next)
	{
		for(Q=P->Next;Q!=NULL;Q=Q->Next)
		{
			if(P->Element.Diem>Q->Element.Diem)
			{
				Hoanvi(P->Element,Q->Element);
			}
		}
	}
}
Leave a Comment