Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.3 kB
2
Indexable
Never
#include<stdio.h>
#include<iostream>
typedef int 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 LietKe(ElementType X, List L);
void Hoanvi(ElementType&x, ElementType&y);
void Sapxep(List L);
int main()
{
	List L;
	MakeNullList(L);
	NhapDS(L);
	XuatDS(L);
	ElementType X;
	std::cout<<"Nhap gia tri can tim: ";
	std::cin>>X;
	LietKe(X,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 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: ";
		std::cin>>X;
		InsertList(X,P,L);
		//P=P->Next;//Them cuoi ds. 
	}	
}
void XuatDS(List L)
{
	Position P=L->Next;
	while(P!=NULL)
	{
		ElementType X=Retrieve(P);
		std::cout<<X<<"\t";
		P=P->Next;//node ke tiep
	}
}
void LietKe(ElementType X, List L)
{
	Position P=L->Next;//Node that
	int dem=0;
	int vitri=-1;
	while(P!=NULL)
	{
		ElementType Y=Retrieve(P);
		vitri++;
		if(Y==X)
		{			
			dem++;
			std::cout<<vitri<<" ";
		}
		P=P->Next;//node ke tiep
	}
	std::cout<<std::endl<<"Co "<<dem<<" phan tu bang "<<X;
}
void Hoanvi(ElementType&x, ElementType&y)
{
	ElementType z=x;
	x=y;
	y=z;
}
void Sapxep(List L)
{
	/*
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(a[i]>a[j])
			{
				Hoanvi(a[i], a[j]);
			}
		}
	}
	*/
	Position P,Q;
	for(P=L->Next;P!=NULL;P=P->Next)
	{
		for(Q=P->Next;Q!=NULL;Q=Q->Next)
		{
			if(P->Element>Q->Element)
			{
				Hoanvi(P->Element,Q->Element);
			}
		}
	}
}
Leave a Comment