Untitled
unknown
plain_text
a year ago
7.0 kB
16
Indexable
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <math.h>
using namespace std;
struct Node {
int info;
Node* Next;
};
struct SList {
Node* Head;
Node* Tail;
};
Node* createNode(int x) {
Node* p = new Node;
if(p == NULL) {
cout << "Khong du bo nho cap phat" << endl;
getch();
return NULL;
}
p->info = x;
p->Next = NULL;
return p;
};
void showNode(Node* p) {
cout << p->info << " ";
}
void initSList(SList &sl) {
sl.Head = NULL;
sl.Tail = NULL;
}
int IsEmpty(SList sl) {
return sl.Head == NULL;
}
void ShowSList(SList sl) {
if(IsEmpty(sl)) {
cout << "Danh sach rong" << endl;
return;
}
cout << "Noi dung cua danh sach la: ";
Node* p = sl.Head;
while (p != NULL) {
showNode(p);
p = p->Next;
}
cout << endl;
}
int InsertHead(SList &sl, Node* p) {
if(p == NULL) {
return 0;
}
if(IsEmpty(sl)) {
sl.Head = sl.Tail = p;
} else {
p->Next = sl.Head;
sl.Head = p;
}
return 1;
}
int InsertTail(SList &sl, Node* p) {
if(p == NULL) {
return 0;
}
if(IsEmpty(sl)) {
sl.Head = sl.Tail = p;
} else {
sl.Tail->Next = p;
sl.Tail = p;
}
return 1;
}
int InsertAfter(SList &sl, Node* q, Node* p) {
if(q == NULL || p == NULL) {
return 0;
}
p->Next = q->Next;
q->Next = p;
if (sl.Tail == q) {
sl.Tail = p;
}
return 1;
}
bool tonTai(SList sl, int x) {
Node* p = sl.Head;
while(p != NULL) {
if(p->info == x) return true;
p = p->Next;
}
return false;
}
int DeleteHead(SList &sl) {
if(IsEmpty(sl)) {
return 0;
}
Node* p = sl.Head;
sl.Head = sl.Head->Next;
if(sl.Head == NULL) {
sl.Tail = NULL;
}
delete p;
return 1;
}
int DeleteAfter(SList &sl, Node* q) {
if(q == NULL || q->Next == NULL) {
return 0;
}
Node* p = q->Next;
q->Next = p->Next;
if(sl.Tail == p) {
sl.Tail = q;
}
delete p;
return 1;
}
void xuatChan(SList sl) {
Node* p = sl.Head;
cout << "Cac phan tu chan: ";
while(p != NULL) {
if(p->info % 2 == 0) {
cout << p->info << " ";
}
p = p->Next;
}
cout << endl;
}
void createAutoSList(SList &sl) {
int n;
int x;
initSList(sl);
do {
cout << "Cho biet so phan tu cua danh sach (n > 0): ";
cin >> n;
} while(n <= 0);
srand(time(NULL));
for(int i = 1; i <= n; i++) {
x = (rand() % 199) - 10;
Node* p = createNode(x);
InsertTail(sl, p);
}
}
void Menu() {
cout << "===== MENU =====" << endl;
cout << "1. Them phan tu vao dau danh sach" << endl;
cout << "2. Them phan tu vao cuoi danh sach" << endl;
cout << "3. Them phan tu sau mot phan tu cho truoc" << endl;
cout << "4. Kiem tra phan tu ton tai" << endl;
cout << "5. Xoa phan tu dau danh sach" << endl;
cout << "6. Xoa phan tu sau mot phan tu cho truoc" << endl;
cout << "7. Xuat cac phan tu chan" << endl;
cout << "8. Hien thi danh sach" << endl;
cout << "9. Tu dong nhap danh sach ngau nhien" << endl;
cout << "0. Thoat" << endl;
cout << "================" << endl;
}
int main() {
SList sl;
initSList(sl);
int choice;
do {
Menu();
cout << "Nhap lua chon cua ban: ";
cin >> choice;
system("cls");
switch(choice) {
case 1: {
int x;
cout << "Nhap gia tri can them vao dau danh sach: ";
cin >> x;
Node* p = createNode(x);
InsertHead(sl, p);
break;
}
case 2: {
int x;
cout << "Nhap gia tri can them vao cuoi danh sach: ";
cin >> x;
Node* p = createNode(x);
InsertTail(sl, p);
break;
}
case 3: {
int x, y;
cout << "Nhap gia tri cua phan tu cho truoc: ";
cin >> x;
cout << "Nhap gia tri cho phan tu can them: ";
cin >> y;
Node* p = createNode(y);
Node* q = sl.Head;
while(q != NULL && q->info != x) {
q = q->Next;
}
if(q == NULL) {
cout << "Khong tim thay phan tu can them sau!" << endl;
} else {
if(InsertAfter(sl, q, p)) {
cout << "Da them thanh cong!" << endl;
} else {
cout << "Khong the them vao sau node cho truoc!" << endl;
}
}
break;
}
case 4: {
int x;
cout << "Nhap gia tri can kiem tra: ";
cin >> x;
if(tonTai(sl, x)) {
cout << "Phan tu " << x << " ton tai trong danh sach." << endl;
} else {
cout << "Phan tu " << x << " khong ton tai trong danh sach." << endl;
}
break;
}
case 5: {
if(DeleteHead(sl)) {
cout << "Da xoa phan tu dau danh sach!" << endl;
} else {
cout << "Danh sach rong, khong co gi de xoa!" << endl;
}
break;
}
case 6: {
int x;
cout << "Nhap gia tri cua phan tu can xoa sau no: ";
cin >> x;
Node* q = sl.Head;
while(q != NULL && q->info != x) {
q = q->Next;
}
if(q == NULL || q->Next == NULL) {
cout << "Khong the xoa phan tu sau node cho truoc!" << endl;
} else {
if(DeleteAfter(sl, q)) {
cout << "Da xoa phan tu sau phan tu cho truoc!" << endl;
} else {
cout << "Khong the xoa phan tu sau node cho truoc!" << endl;
}
}
break;
}
case 7: {
xuatChan(sl);
break;
}
case 8: {
ShowSList(sl);
break;
}
case 9: {
cout << "Tu dong them danh sach" << endl;
createAutoSList(sl);
break;
}
case 0: {
cout << "Thoat chuong trinh." << endl;
break;
}
default: {
cout << "Lua chon khong hop le. Vui long chon lai!" << endl;
break;
}
}
} while(choice != 0);
return 0;
}
Editor is loading...
Leave a Comment