Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.9 kB
1
Indexable
Never
#include<stdio.h>
#include<math.h>

struct node {
	int data;
	struct node* pNext;
};
typedef struct node NODE;

struct list {
	NODE* pHead;
	NODE* pTail;
};
typedef struct list LIST;

void KhoiTao(LIST& l) {
	l.pHead = NULL;
	l.pTail = NULL;
}

NODE* KhoiTaoNode(int x) {
	NODE* p = new NODE;
	if (p == NULL)	printf("Khong the cap phat bo nho");
	else {
		p->data = x;
		p->pNext = NULL;
		return p;
	}
}

void ThemCuoi(LIST& l, NODE* p) {
	if (l.pHead == NULL)	l.pHead = l.pTail = p;
	else {
		l.pTail->pNext = p;
		l.pTail = p;
	}
}

void ThemDau(LIST& l, NODE* p) {
	if (l.pHead == NULL)	l.pHead = l.pTail = p;
	else {
		p->pNext = l.pHead;
		l.pHead = p;
	}
}

void ChenXTruocY(LIST& l) {
	int x, y;
	printf("Ban hay nhap gia tri x: ");	scanf_s("%d", &x);
	printf("Ban hay nhap y:");	scanf_s("%d", &y);
	NODE* j = new NODE;
	NODE* p = KhoiTaoNode(x);
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		if (k->data == y) {
			if (k->data==l.pHead->data)	ThemDau(l, p);
			NODE* h = KhoiTaoNode(p->data);
			h->pNext = k;
			j->pNext = h;
		}
		j = k;
	}
}

void XoaLonHonX(LIST& l) {
	int x;
	printf("Ban hay nhap x: ");	scanf_s("%d", &x);
	NODE *prev = NULL;
	NODE* k = l.pHead;
	while (k != NULL) {
		if (k->data > x) {
			if (k == l.pHead) {
				l.pHead = k->pNext;
				delete k;
				k = l.pHead;
			}
			else {
				prev->pNext = k->pNext;
				delete k;
				k = prev->pNext;
			}
		}
		else {
			prev = k;
			k = k->pNext;
		}
	}
}

void XoaChan(LIST& l) {
	NODE* prev = NULL;
	NODE* k = l.pHead;
	while (k != NULL) {
		if (k->data % 2 == 0) {
			if (k == l.pHead) {
				l.pHead = k->pNext;
				delete k;
				k = l.pHead;
			}
			else {
				prev->pNext = k->pNext;
				delete k;
				k = prev->pNext;
			}
		}
		else {
			prev = k;
			k=k->pNext;
		}
	}
}

void swap(int &x, int &y) {
	int temp = x;
	x = y;
	y = temp;
}

void SapXepTang(LIST& l) {
	if (l.pHead == NULL)	printf("Danh sach dang rong");
	else{
		for (NODE* i = l.pHead; i != NULL; i = i->pNext) {
			for (NODE* j = i; j != NULL; j = j->pNext) {
				if (i->data > j->data)	swap(i->data, j->data);
			}
		}
	}
}

void SapXepGiam(LIST& l) {
	if (l.pHead == NULL)	printf("Danh sach dang rong");
	else{
		for (NODE* i = l.pHead; i != NULL; i = i->pNext) {
			for (NODE* j = i; j != NULL; j = j->pNext) {
				if (i->data < j->data)	swap(i->data, j->data);
			}
		}
	}
}

int DemSNT(LIST l) {
	int dem = 0;
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		int tam = 0;
		for (int i = 2; i <= k->data; i++) {
			if (k->data % i == 0)	tam++;
		}
		if (tam == 1)	dem++;
	}
	return dem;
}

int TongSoChinhPhuong(LIST l) {
	int tong = 0;
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		if (k->data == pow(sqrt(k->data), 2)) {
			tong = tong + k->data;
		}
	}
	return tong;
}

int max(LIST l) {
	int max = l.pHead->data;
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		if (k->data > max)	max = k->data;
	}
	return max;
}

int min(LIST l) {
	int min = l.pHead->data;
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		if (k->data < min)	min = k->data;
	}
	return min;
}

int DemXLonGapDoiSauNo(LIST l) {
	int dem = 0;
	NODE* k = l.pHead;
	while (k != NULL && k->pNext != NULL) {
		if (k->data / 2 == k->pNext->data)	dem++;
		k = k->pNext;
	}
	return dem;
}

void XuatDS(LIST l) {
	for (NODE* k = l.pHead; k != NULL; k = k->pNext) {
		printf("%4d", k->data);
	}
}

int main() {
	LIST l;
	KhoiTao(l);
	LIST l1;
	KhoiTao(l1);
	LIST l2;
	KhoiTao(l2);
	printf("\n1. Them phan tu vao cuoi danh sach\n2. Xuat danh sach");
	printf("\n3. Chen them phan tu co gia tri x vao truoc phan tu co gia tri y");
	printf("\n4. Xoa cac phan lon hon x\n5. Xoa cac phan tu chan trong dslk");
	printf("\n6. Sap xep danh sach lien ket tang");
	printf("\n7. Sap xep danh sach lien ket giam");
	printf("\n8. Cho biet trong dslk co bao nhieu so nguyen to");
	printf("\n9. Tinh tong cac so chinh phuong trong dslk");
	printf("\n10. Phan tu lon nhat dslk\n11. Phan tu nho nhat dslk");
	printf("\n12. Dem so phan tu lon hon gap doi phan tu phia sau no");
	while (true) {
		int lc;
		printf("\nBan chon so may: ");	scanf_s("%d", &lc);
		switch (lc) {
		case 1:
		{
			int x;
			printf("Ban hay nhap gia tri can them: ");	scanf_s("%d", &x);
			NODE* p = KhoiTaoNode(x);
			ThemCuoi(l, p);
		}
		case 2:
			printf("\nDSLKD:");
			XuatDS(l);	break;
		case 3:
			ChenXTruocY(l);	break;
		case 4:
			XoaLonHonX(l);	break;
		case 5:
			XoaChan(l);	break;
		case 6:
			SapXepTang(l);	break;
		case 7:
			SapXepGiam(l);	break;
		case 8:
			printf("Trong dslk co %d so nguyen to", DemSNT(l));	break;
		case 9:
			printf("Tong cac so chinh phuong trong dslk = %d", TongSoChinhPhuong(l));
			break;
		case 10:
			printf("Phan tu lon nhat dslk la: %d", max(l));	break;
		case 11:
			printf("Phan tu nho nhat dslk la: %d", min(l));	break;
		case 12:
			printf("Co %d phan tu lon hon gap doi phan tu phia sau no", DemXLonGapDoiSauNo(l));
			break;
		}
	}
}