Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.2 kB
1
Indexable
Never
#include <iostream>
using namespace std;

typedef struct node {
	int data;
	node* pnext;
}node;
struct dslk {
	node* phead = NULL;
	node* ptail = NULL;
};
node* khoi_tao_node(int x);
void them_cuoi(dslk& ds, int x);
void them_dau(dslk& ds, int x);
void xuat_ds(dslk ds);
void xoa_dau(dslk& ds);
void xoa_cuoi(dslk& ds);
void xoa_bki(dslk& ds, int x);
int main()
{
	dslk ds;
	bool kt = true;
	while (kt)
	{
		system("cls");
		cout << "1. Nhap phan tu vao dslk" << endl;
		cout << "2. Xuat dslk" << endl;
		cout << "3. Them dau" << endl;
		cout << "4. Xoa dau" << endl;
		cout << "5. Xoa cuoi" << endl;
		cout << "6. Xoa bat ki" << endl;;
		cout << "0. Thoat" << endl;
		int lc;
		cout << "Nhap lc :"; cin >> lc;
		switch (lc)
		{
		case 1: {
			int sl; cout << "Nhap sl ptu :"; cin >> sl;
			for (int i = 1; i <= sl; i++)
			{
				int x; cout << "Nhap ptu thu " << i << " : "; cin >> x;
				them_cuoi(ds, x);
			}
			break;
		}
		case 2: {
			xuat_ds(ds);
			system("pause");
			break;
		}
		case 3: {
			int x;
			cout << "nhap gia tri can them :"; cin >> x;
			them_dau(ds, x);
			break;
		}
		case 4: {
			xoa_dau(ds);
			system("pause");
			break;

		}
		case 5: {
			xoa_cuoi(ds);
			system("pause");
			break;

		}
		case 6: {
			int x;
			cout << "nhap gia tri can xoa :"; cin >> x;
			xoa_bki(ds, x);
			break;
		}
		case 0: {
			kt = false;
			break;
		}
		default:
			break;
		}
	}

}
node* khoi_tao_node(int x) {

	node* p = new node;
	p->data = x;
	p->pnext = NULL;
	return p;
}
void them_cuoi(dslk& ds, int x) {
	//b1: chuyen data -> node
	node* p = khoi_tao_node(x);//p giu dia chi ptu co data x
	//b2: them
	if (ds.phead == NULL) {	//TH1 : ds rong
		ds.phead = ds.ptail = p;
	}
	else {//TH2 : co n ptu
		ds.ptail->pnext = p;
		ds.ptail = p;
	}
}
void xuat_ds(dslk ds) {
	cout << "\t\t==========DSLK============" << endl;
	int i = 1;
	for (node* k = ds.phead; k != NULL; k = k->pnext) {
		cout << "PT" << i++ << " : " << k->data << endl;
	}
}
void them_dau(dslk& ds, int x) {
	node* p = khoi_tao_node(x);
	if (ds.phead == NULL) {
		ds.phead = ds.ptail = p;
	}
	else {
		p->pnext = ds.phead;
		ds.phead = p;
	}
}

void xoa_dau(dslk& ds) {
	if (ds.phead == NULL)
	{
		cout << " Danh sach rong !" << endl;
		return;
	}
	node* p = ds.phead;
	ds.phead = ds.phead->pnext;
	if (ds.phead == NULL) {
		ds.ptail == NULL;
	}
	p->pnext = NULL;
	delete[] p;
}
void xoa_cuoi(dslk& ds) {
	if (ds.phead == NULL)
	{
		cout << " Danh sach rong !" << endl;
		return;
	}
	node* q = NULL;
	node* k;
	for (k = ds.phead; k != ds.ptail; k = k->pnext) {
		q = k;
	}
	if (q == NULL) {
		delete k;
		ds.phead = NULL;
		ds.ptail = NULL;
	}
	else {
		delete k;
		q->pnext = NULL;
		ds.ptail = q;
	}
}
void xoa_bki(dslk& ds, int x) {
	node* q = NULL;
	node* tam = NULL;
	for (node* k = ds.phead; k != NULL; k = k->pnext) {
		if (k->data == x) {
			if (k == ds.phead) {
				xoa_dau(ds);
			}
			else if (k == ds.ptail) {
				xoa_cuoi(ds);
			}
			else {
				tam->pnext = k;
				q->pnext = k -> pnext ->pnext;
				k->pnext = NULL;
				tam->pnext = q;
				delete k;	
			}
		}
	}
}