Hiển thị các bài đăng có nhãn Cấu trúc dữ liệu và thuật toán. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Cấu trúc dữ liệu và thuật toán. Hiển thị tất cả bài đăng

Danh Sách Sinh Viên Cài Đặt Bằng Mảng

00:39 Add Comment
Danh Sách Sinh Viên Cài Đặt Bằng Mảng
Danh sách sinh vên cài đặt bằng mảng với các chức năng cơ bản

#include iostream
#include stdio.h
using namespace std;
#define N 50
typedef struct SinhVien {
 char HoTen[30];
 int NamSinh;
 float DiemTB;
};

typedef struct List {
 SinhVien SV[N];
 int Size;
};

bool KiemTraDay(List L) {
 if (L.Size == N) return true;
 else return false;
}
// hàm nhập thông tin cho 1 sinh viên
void NhapSV(SinhVien &x) {
 cin.ignore();
 cout << "Ho ten: ";
 cin.getline(x.HoTen, 30);
 cout << "Nam sinh: ";
 cin >> x.NamSinh;
 cout << "Diem tb: ";
 cin >> x.DiemTB;
 cout << "------------------------------------" << endl;
}
// hàm xuất thông tin cho 1 sinh viên
void XuatSV(SinhVien x) {
 cout << "Ho ten: " << x.HoTen << endl;
 cout << "Nam sinh: " << x.NamSinh << endl;
 cout << "Diem tb: " << x.DiemTB << endl;
 cout << "-------------------------------------" << endl;
}
// hàm nhập n sinh viên
void NhapDanhSach(List& L) {
 for (int i = 0; i < L.Size; i++) {
  cout << "Thong tin sinh vien thu " << i + 1 << endl;
  NhapSV(L.SV[i]);
  
 }
}
// hàm xuất n sinh viên
void XuatDanhSach(List L) {
 cout << "---------- DANH SACH SINH VIEN ----------" << endl;
 for (int i = 0; i < L.Size; i++) {
  XuatSV(L.SV[i]);
 }
}
// thêm 1 sinh viên vào vị trí thứ k
void ThemBatKy(List &L, SinhVien x, int k) {
 if (KiemTraDay(L)) cout << "Danh sach day. " << endl;
 else {
  if (k < 1 || k > L.Size + 1) cout << "vi tri khong hop le" << endl;
  else {
   for (int i = L.Size; i > k; i--) {
    L.SV[i] = L.SV[i - 1];
   }
   L.SV[k-1] = x;
   L.Size++;
  }
 }
}
// xóa 1 sinh viên vị trí thứ k
void XoaBatKy(List& L, int k) {
 if (KiemTraDay(L)) cout << "Danh sach day." << endl;
 else {
  if (k<1 k=""> L.Size) cout << "vi tri khong hop le. " << endl;
  else {
   for (int i = k - 1; i < L.Size - 1; i++) {
    L.SV[i] = L.SV[i + 1];
   }
   L.Size--;
  }
 }
}
// hàm tìm kiếm chính xác theo tên
void TimKiemChinhXac(List L, char s[]) {
 for (int i = 0; i < L.Size; i++) {
  if (L.SV[i].HoTen == s) {
   cout << "Co sinh vien " << s << " trong danh sach " << endl;
   XuatSV(L.SV[i]);
  }
 }
}
// hàm tìn kiếm theo tên gần đúng
void TimKiemGanDung(List L, char s[]) {
 for (int i = 0; i < L.Size; i++) {
  int dem = 0, j = 0;
  char u;
  while (dem != strlen(s) && j != strlen(s)) {
   u = s[j];
   for (int a = 0; a < strlen(L.SV[i].HoTen); a++) {
    if (u == L.SV[i].HoTen[a]) dem++;
   }
   j++;
  }
  if (dem == strlen(s)) {
   cout << "Co sinh vien " << s << " trong danh sach " << endl;
   XuatSV(L.SV[i]);
  }
 }
}

void LocDanhSach(List L) {
 for (int i = 0; i < L.Size; i++) {
  if (L.SV[i].DiemTB <= 5.0) {
   XuatSV(L.SV[i]);
  }
 }
}
int main() {
 List L;
 SinhVien x;
 int k;
 cout << "So luong sinh vien: ";
 cin >> L.Size;
 NhapDanhSach(L);
 system("cls");
 int chon;
 do {
  system("cls");
  cout << "1. Xuat danh sach. " << endl;
  cout << "2. Them bat ky. " << endl;
  cout << "3. Xoa bat ky. " << endl;
  cout << "4. Tim kiem. " << endl;
  cout << "5. Loc sinh vien diem trung binhf <= 5.0 " << endl;
  cin >> chon;
  switch (chon)
  {
  case 1:
  {
   XuatDanhSach(L);
   system("pause");
   break;
  }
  case 2: {
   NhapSV(x);
   cout << "Nhap vi tri: ";
   cin >> k;
   ThemBatKy(L, x, k);
   system("pause");
   break;
  }
  case 3: {
   cout << "Nhap vi tri: ";
   cin >> k;
   XoaBatKy(L, k);
   system("pause");
   break;
  }
  case 4: {
   char s[30];
   cin.ignore();
   cout << "Ten can tim: ";
   cin.getline(s, 30);
   TimKiemGanDung(L, s);
   system("pause");
   break;
  }
  case 5: {
   LocDanhSach(L);
   system("pause");
   break;
  }
  default:
   break;
  }
 } while (chon != 0);
}

Cây Nhị Phân Số Nguyên - BinaryTree

20:11 Add Comment
Cây Nhị Phân Số Nguyên
Cây nhị phân số nguyên có đầy đủ các chức năng cơ bản: nhập cây, duyệt cây, chèn một nút vào cây, xóa một nút khỏi cây, tìm kiếm nút

// Nguyen Cong Cuong - KTPM K14B
#include iostream
#include stdlib.h
using namespace std;

typedef struct Node{
 int Data;
 Node* Left;
 Node* Right;
};

typedef struct Node* BinaryTree;

// Khoi tao
void InIt(BinaryTree& T){
 T = NULL;
}

//kiem tra rong

bool IsEmpty(BinaryTree T){
 return (T == NULL);
}

// Tao Noode
Node* AddNode(Node* P,int x){
 P = (Node*)malloc(sizeof(Node));
 P->Data = x;
 P->Left = NULL;
 P->Right = NULL;
 return P;
}

//Them vao nut la
void AddTree(BinaryTree& T, int x){
 Node* P = AddNode(P,x);
 if (IsEmpty(T)) T = P;
 else {
  Node* M = T;
  Node* Q = NULL;
  while (M != NULL){
   Q = M;
   if (M->Data < x){
    M = M->Right;
   }
   else M = M->Left;
  }
  if (Q->Data < x) Q->Right = P;
  else Q->Left = P;
 }
}

void InPut(BinaryTree& T, int n){
 int x;
 for (int i = 0; i < n; i++){
  cout << "Value: " << i + 1 << endl;
  cin >> x;
  AddTree(T, x);
 }
}

void DuyetTruoc(BinaryTree T){ // duyet truoc
 Node* M = T;
 if (M != NULL){
  cout << M->Data << " ";
  DuyetTruoc(M->Left);
  DuyetTruoc(M->Right);
 }

}
void DuyetGiua(BinaryTree T){
 Node* M = T;
 if (M != NULL){

  DuyetGiua(M->Left);
  cout << M->Data << " ";
  DuyetGiua(M->Right);
 }
}
void DuyetSau(BinaryTree T){
 Node* M = T;
 if (M != NULL){
  DuyetSau(M->Left);
  DuyetSau(M->Right);
  cout << M->Data << " ";
 }
}
// tim kiem
Node* TimKiem(BinaryTree T, int x){
 if (IsEmpty(T)) return NULL;
 else {
  Node* M = T;
  if (M->Data > x) TimKiem(M->Left, x);
  else if (M->Data < x) TimKiem(M->Right, x);
  else return M;
 }
}
// Tim cha cua mot nut
Node* TimCha(BinaryTree T, int x){
 Node* M = T;
 if (IsEmpty(M)) return NULL;
 else {

  while (M != NULL){
   if (M->Data < x) {
    M = M->Right;
   }
   else{
    M = M->Left;
   }
   if (M->Left != NULL && M->Left->Data == x) return M;
   if (M->Right != NULL && M->Right->Data == x) return M;
  }
 }
}
// Xoa
void DeleteTree(BinaryTree& T, int x){
 Node* M = T;
 if (M != NULL){
  if (M->Left != NULL && M->Left->Data == x){
   // nut la
   if (M->Left->Left == NULL && M->Left->Right == NULL) M->Left = NULL;
   else {
    // nut co 1 con
    if (M->Left->Left != NULL && M->Left->Right == NULL) M->Right = M->Left->Left;
    if (M->Left->Left == NULL && M->Left->Right != NULL) M->Left = M->Left->Right;
   }
  }
  if (M->Right != NULL && M->Right->Data == x){
   if (M->Right->Left == NULL && M->Right->Right == NULL) M->Right = NULL;
   else {
    if (M->Right->Left != NULL && M->Right->Right == NULL) M->Right = M->Left->Left;
    if (M->Right->Left == NULL && M->Right->Right != NULL) M->Left = M->Left->Right;
   }
  }
  if (M->Data == x){
   // nut co 2 con
   if (M->Left != NULL && M->Right != NULL){
    if (M->Right->Right == NULL && M->Left->Left == NULL){
     M->Data = M->Right->Data;
     M->Right = NULL;
    }
    else{
     Node* Q = M->Right;
     while (Q->Left->Left != NULL){
      Q = Q->Left;
     }

     if (Q->Left != NULL){
      M->Data = Q->Left->Data;
      DeleteTree(M->Left, Q->Left->Data);
      DeleteTree(M->Right, Q->Left->Data);
     }
     else {
      M->Data = Q->Right->Data;
      DeleteTree(M->Left, Q->Right->Data);
      DeleteTree(M->Right, Q->Right->Data);
     }
    }
   }
  }
  DeleteTree(M->Left, x);
  DeleteTree(M->Right, x);
 }
}
// Chen
void InsertTree(BinaryTree& T, int x){
 Node* P = AddNode(P,x);
 if (IsEmpty(T)) {
  P->Left = NULL;
  P->Right = NULL;
  T = P;
 }
 else {
  if (T->Data > x) InsertTree(T->Left, x);
  else{
   if (T->Data < x) InsertTree(T->Right, x);
   else cout << "Da co roi." << endl;
  }
 }
}
// So nut trong cay
int Dem(BinaryTree T){
 if (IsEmpty(T)) return 0;
 else return 1 + Dem(T->Left) + Dem(T->Right);
}

int Max(int a, int b){
 if (a > b) return a;
 else return b;
}
// Chieu cao cua cay
int ChieuCao(BinaryTree T){
 if (IsEmpty(T)) return 0;
 else return Max(ChieuCao(T->Left), ChieuCao(T->Right)) + 1;
}
Node* ConTrai(BinaryTree T, int x){
 if (IsEmpty(T)) return NULL;
 else {
  Node* M = T;
  while (M != NULL){
   if (M->Data > x) M = M->Left;
   else if (M->Data < x) M = M->Right;
   else {
    if (M->Left != NULL) return  M->Left;
    else return NULL;
   }
  }
 }
}

Node* ConPhai(BinaryTree T, int x){
 if (IsEmpty(T)) return NULL;
 else {
  Node* M = T;
  while (M != NULL){
   if (M->Data > x) M = M->Left;
   else if (M->Data < x) M = M->Right;
   else {
    if (M->Right != NULL) return  M->Right;
    else return NULL;
   }
  }
 }
}

int SoNutLa(BinaryTree T){
 if (IsEmpty(T)) return 0;
 else {
  if (T->Left == NULL && T->Right == NULL) return 1;
  else return SoNutLa(T->Left) + SoNutLa(T->Right);
 }
}
int main(){
 BinaryTree T;
 InIt(T);
 int n, x;
 cout << "So luong: ";
 cin >> n;
 InPut(T, n);
 system("cls");
 int chon;
 do{
  system("cls");
  cout << "1. Chen." << endl;
  cout << "2. Xoa. " << endl;
  cout << "3. Tim kiem." << endl;
  cout << "4. Xuat." << endl;
  cout << "5. So nut" << endl;
  cout << "6. Tim cha." << endl;
  cout << "7. Chieu cao." << endl;
  cout << "8. Tim con. " << endl;
  cout << "9. So nut la. " << endl;
  cin >> chon;
  switch (chon)
  {
  case 1:{
       cout << "Nhap so can chen: ";
       cin >> x;
       InsertTree(T, x);
       system("pause");
       break;
  }
  case 2:{
       cout << "Nhap so can xoa: ";
       cin >> x;
       DeleteTree(T, x);
       system("pause");
       break;
  }
  case 3:{
       cout << "Nhap so can tim: ";
       cin >> x;
       if (TimKiem(T, x) != NULL) cout << x << " co trong cay" << endl;
       else cout << x << " khong co trong cay" << endl;
       system("pause");
       break;
  }
  case 4:{
       cout << "Duyet truoc: "; DuyetTruoc(T);
       cout << endl;
       cout << "Duyet giua: "; DuyetGiua(T);
       cout << endl;
       cout << "Duyet sau: "; DuyetSau(T);
       cout << endl;
       system("pause");
       break;
  }
  case 5:{
       int dem = 0;
       cout << Dem(T) << endl;
       system("pause");
       break;
  }
  case 6:{
       cout << "Tim cha cua: ";
       cin >> x;
       if (x != T->Data){
        if (TimCha(T, x) != NULL)
         cout << TimCha(T, x)->Data << endl;
        else cout << x << " khong co trong cay" << endl;
       }
       else cout << x << " khong co cha" << endl;
       system("pause");
       break;
  }
  case 7:{
       cout << ChieuCao(T) << endl;;
       system("pause");
       break;
  }
  case 8:{
       cout << "Nhap so: ";
       cin >> x;
       if (ConTrai(T, x) != NULL) cout << "Con trai: " << ConTrai(T, x)->Data << endl;
       if (ConPhai(T, x) != NULL) cout << "Con phai: " << ConPhai(T, x)->Data << endl;
       system("pause");
       break;
  }
  case 9:{
       cout << SoNutLa(T) << endl;
       system("pause");
       break;
  }
  }
 } while (chon != 0);
 return 0;
}

Danh Sách Sinh Viên Bằng Hàng Đợi - Queue

07:02 Add Comment
Danh Sách Sinh Viên Bằng Hàng Đợi - Queue
Danh sách sinh viên sử dụng hàng đợi - queue gồm đầy đủ các chức năng cơ bản
Lưu ý: Thêm < > vào chỗ khai báo thư viên

// Nguyen Cong Cuong - KTPM K14B
#include iostream
#include stdio.h
#include stdlib.h
using namespace std;
typedef struct SinhVien {
	char MaSV[30];
	char HoTen[30];
	int NamSinh;
	char Lop[30];
	float DTB;
};

typedef struct Node {
	SinhVien Data;
	Node* Next;
};

typedef struct Queue {
	Node* Front;
	Node* Rear;
};

void NhapTT(SinhVien &sv) {
	fflush(stdin);
	cout << "Ma SV: ";
	cin.getline(sv.MaSV, 30);
	cout << "Ho ten: ";
	cin.getline(sv.HoTen, 30);
	cout << "Nam sinh: ";
	cin >> sv.NamSinh;
	fflush(stdin);
	cout << "Lop: ";
	cin.getline(sv.Lop, 30);
	cout << "DTB: ";
	cin >> sv.DTB;
}

void XuatTT(SinhVien sv) {
	cout << "Ma SV: " << sv.MaSV << endl;
	cout << "Ho ten:" << sv.HoTen << endl;
	cout << "Nam sinh: " << sv.NamSinh << endl;
	cout << "Lop: " << sv.Lop << endl;
	cout << "DTB: " << sv.DTB << endl;
}

//1. Khoi tao

void InIt(Queue &Q) {
	Q.Front = Q.Rear = NULL;
}

//2. Kiem tra rong
bool IsEmpty(Queue Q) {
	if (Q.Rear == NULL) return true;
	else return false;
}

//3. Tao Node
Node*AddNode(Node* P, SinhVien x) {
	P = (Node*)malloc(sizeof(Node));
	P->Data = x;
	P->Next = NULL;
	return P;
}

//4.Dem so luong
int DemSoLuong(Queue Q) {
	int dem = 0;
	for (Node* M = Q.Front; M != NULL; M = M->Next) {
		dem++;
	}
	return dem;
}

void EnQueue(Queue &Q, SinhVien x) {
	Node* P = AddNode(P, x);
	if (IsEmpty(Q)) Q.Front = Q.Rear = P;
	else {
		Q.Rear->Next = P;
		Q.Rear = P;
	}
}

void DeQueue(Queue &Q) {
	Q.Front = Q.Front->Next;
}

void InPut(Queue &Q, int n) {
	SinhVien x;
	for (int i = 0; i < n; i++) {
		cout << "Sinh vien " << i + 1 << endl;
		NhapTT(x);
		EnQueue(Q, x);
		cout << "-------------------------------------" << endl;
	}
}

void OutPut(Queue Q) {
	for (Node* M = Q.Front; M != NULL; M = M->Next) {
		XuatTT(M->Data);
		cout << "---------------------------------------" << endl;
	}
}
void AddAny(Queue& Q, Queue& Q2, int k, SinhVien x) {
	InIt(Q2);
	SinhVien sv;
	int a = DemSoLuong(Q) - (k - 1);
	for (int i = 0; i < k - 1; i++) {
		sv = Q.Front->Data;
		DeQueue(Q);
		EnQueue(Q2, sv);
	}
	EnQueue(Q2, x);

	for (int i = 0; i < a; i++) {
		sv = Q.Front->Data;
		DeQueue(Q);
		EnQueue(Q2, sv);
	}
	Q = Q2;

}

void DeleteAny(Queue& Q, Queue& Q2, int k) {
	InIt(Q2);
	SinhVien sv;
	int a = (DemSoLuong(Q) - (k - 1)) - 1;
	for (int i = 0; i < k - 1; i++) {
		sv = Q.Front->Data;
		DeQueue(Q);
		EnQueue(Q2, sv);
	}
	DeQueue(Q);
	for (int i = 0; i < a; i++) {
		sv = Q.Front->Data;
		DeQueue(Q);
		EnQueue(Q2, sv);
	}
	Q = Q2;
}
int main() {
	SinhVien sv;
	Queue Q, Q2;
	InIt(Q);
	int n, chon;
	cout << "Nhap so luong: ";
	cin >> n;
	InPut(Q, n);
	do
	{
		system("cls");
		cout << "1. Them sinh vien. " << endl;
		cout << "2. Xuat sinh vien." << endl;
		cout << "3. Xoa sinh vien. " << endl;
		cout << "4. Them bat ky." << endl;
		cout << "5. Xoa bat ky. " << endl;
		cin >> chon;
		switch (chon) {
		case 1:
		{

			NhapTT(sv);
			EnQueue(Q, sv);
			break;
		}
		case 2:
		{
			OutPut(Q);
			system("pause");
			break;
		}
		case 3:
		{
			DeQueue(Q);
			break;
		}
		case 4:
		{
			int k;
			cout << "vi tri them: ";
			cin >> k;
			if (k < 1 || k >DemSoLuong(Q)) cout << "vi tri khong hop le. ";
			else {
				NhapTT(sv);
				AddAny(Q, Q2, k, sv);

			}
			system("pause");
			break;
		}
		case 5:
		{
			int k;
			cout << "vi tri xoa: ";
			cin >> k;
			if (k < 1 || k >DemSoLuong(Q)) cout << "vi tri khong hop le. ";
			else {

				DeleteAny(Q, Q2, k);

			}
			system("pause");
			break;
		}
		}
	} while (chon != 0);
	return 0;
}

Danh Sách Sinh Viên Bằng Ngăn Xếp - Stack

06:39 Add Comment
Danh Sách Sinh Viên Bằng Ngăn Xếp - Stack
Danh sách sinh viên sử dụng ngăn xếp Stack đầy đủ các chức năng cơ bản
Lưu ý: Thêm < > vào chỗ khai báo thư viện

// Nguyen Cong Cuong - KTPM K14B
#include iostream
#include stdio.h
#include stdlib.h
using namespace std;
typedef struct SinhVien {
	char MaSV[30];
	char HoTen[30];
	int NamSinh;
	char Lop[30];
	float DTB;
};

typedef struct Node {
	SinhVien Data;
	Node* Next;
};

typedef struct Node* Stack;

void NhapTT(SinhVien &sv) {
	fflush(stdin);
	cout << "Ma SV: ";
	cin.getline(sv.MaSV, 30);
	cout << "Ho ten: ";
	cin.getline(sv.HoTen, 30);
	cout << "Nam sinh: ";
	cin >> sv.NamSinh;
	fflush(stdin);
	cout << "Lop: ";
	cin.getline(sv.Lop, 30);
	cout << "DTB: ";
	cin >> sv.DTB;
}

void XuatTT(SinhVien sv) {
	cout << "Ma SV: " << sv.MaSV << endl;
	cout << "Ho ten:" << sv.HoTen << endl;
	cout << "Nam sinh: " << sv.NamSinh << endl;
	cout << "Lop: " << sv.Lop << endl;
	cout << "DTB: " << sv.DTB << endl;
}

//1. Khoi tao

void InIt(Stack &S) {
	S == NULL;
}

//2. Kiem tra rong
bool IsEmpty(Stack S) {
	return (S == NULL);
}

//3. Tao Node
Node*AddNode(Node* P, SinhVien x) {
	P = (Node*)malloc(sizeof(Node));
	P->Data = x;
	P->Next = NULL;
	return P;
}

//4.Dem so luong
int DemSoLuong(Stack S) {
	int dem = 0;
	for (Node* M = S; M != NULL; M = M->Next) {
		dem++;
	}
	return dem;
}
void Pop(Stack& S, SinhVien x) {
	Node* P = AddNode(P, x);
	P->Next = S;
	S = P;
}

void Push(Stack& S) {
	S = S->Next;
}

void InPut(Stack& S, int n) {
	InIt(S);
	SinhVien x;
	for (int i = 0; i < n; i++) {
		cout << "Sinh vien " << i + 1 << endl;
		NhapTT(x);
		Pop(S, x);
	}
}

void OutPut(Stack S) {
	for (Node* M = S; M != NULL; M = M->Next) {
		XuatTT(M->Data);
		cout << "------------------------------------" << endl;
	}
}

void AddAny(Stack& S, Stack &S2, int k, SinhVien x) {
	InIt(S2);
	SinhVien sv;
	for (int i = 0; i < k - 1; i++) {
		sv = S->Data;
		Push(S);
		Pop(S2, sv);
	}
	Pop(S, x);
	for (int i = 0; i < k - 1; i++) {
		sv = S2->Data;
		Push(S2);
		Pop(S, sv);
	}
}

void DeleteAny(Stack& S, Stack &S2, int k) {
	InIt(S2);
	SinhVien sv;
	for (int i = 0; i < k - 1; i++) {
		sv = S->Data;
		Push(S);
		Pop(S2, sv);
	}
	Push(S);
	for (int i = 0; i < k - 1; i++) {
		sv = S2->Data;
		Push(S2);
		Pop(S, sv);
	}
}

void Tach(Stack& S, Stack& S2, Stack& S3) {
	InIt(S3);
	InIt(S2);
	int temp = 0;
	SinhVien sv;
	for (Node*M = S; M != NULL; M = M->Next) {
		temp++;
		if ((M->Data).DTB > 5.0) {
			for (int i = 0; i < temp - 1; i++) {
				sv = S->Data;
				Push(S);
				Pop(S2, sv);

			}
			sv = S->Data;
			Push(S);
			Pop(S3, sv);
			for (int i = 0; i < temp - 1; i++) {
				sv = S2->Data;
				Push(S2);
				Pop(S, sv);
			}
			temp--;
		}
	}
}
int main() {
	SinhVien x;
	int chon;
	Stack S, S2, S3;
	int n;
	cout << "So luong sinh vien: ";
	cin >> n;
	InPut(S, n);
	do
	{
		system("cls");
		cout << "1. Them sinh vien. " << endl;
		cout << "2. Xuat sinh vien." << endl;
		cout << "3. Xoa sinh vien. " << endl;
		cout << "4. Them bat ky. " << endl;
		cout << "5. Xoa bat ky. " << endl;
		cout << "6. Tach diem trung binh. " << endl;
		cin >> chon;
		switch (chon) {
		case 1: {
			NhapTT(x);
			Pop(S, x);
			break;
		}
		case 2: {
			OutPut(S);
			system("pause");
			break;
		}
		case 3: {
			Push(S);
			break;
		}
		case 4: {
			int k;
			cout << "Them vao vi tri: ";
			cin >> k;
			if (k < 1 || k > DemSoLuong(S)) cout << "Vi tri khong hop le.";
			else {

				SinhVien x;
				NhapTT(x);
				AddAny(S, S2, k, x);
			}
			system("pause");
			break;
		}
		case 5: {
			int k;
			cout << "xoa vi tri: ";
			cin >> k;
			if (k < 1 || k > DemSoLuong(S)) cout << "Vi tri khong hop le.";
			else {
				DeleteAny(S, S2, k);
			}
			system("pause");
			break;
		}
		case 6:
		{
			Tach(S, S2, S3);
			cout << "stack 1" << endl;
			OutPut(S);

			cout << "stack 2 " << endl;
			OutPut(S3);
			system("pause");
			break;
		}
		}
	} while (chon != 0);
	return 0;
}

[C/C++] Danh Sách Sinh Viên Bằng Danh Sách Liên Kết

07:58 Add Comment
Danh Sách Liên Kết - Link List
danh sách sinh viên bằng danh sách liên kết

#include iostream
#include stdlib.h
using namespace std;
typedef struct SinhVien {
 char MaSV[30];
 char HoTen[30];
 int NamSinh;
 char Lop[30];
 float DTB;
};
typedef struct Node {
 SinhVien Data;
 Node* Next;
};

typedef struct Node* LinkList;

void InPutSV(SinhVien &sv) {
 fflush(stdin);
 cout << "Ma SV: ";
 cin.getline(sv.MaSV, 30);
 cout << "Ho ten: ";
 cin.getline(sv.HoTen, 30);
 cout << "Nam sinh: ";
 cin >> sv.NamSinh;
 fflush(stdin);
 cout << "Lop: ";
 cin.getline(sv.Lop, 30);
 cout << "DTB: ";
 cin >> sv.DTB;
}

void OutPutSV(SinhVien sv) {
 cout << "Ma SV: " << sv.MaSV << endl;
 cout << "Ho ten:" << sv.HoTen << endl;
 cout << "Nam sinh: " << sv.NamSinh << endl;
 cout << "Lop: " << sv.Lop << endl;
 cout << "DTB: " << sv.DTB << endl;
}
void InIt(LinkList& L) {
 L = NULL;
}

bool IsEmpty(LinkList L) {
 if (L == NULL) return true;
 return false;
}

int Quantity(LinkList L) {
 int temp = 0;
 for (Node* M = L; M != NULL; M = M->Next) {
  temp++;
 }
 return temp;
}
Node* AddNode(SinhVien x) {
 Node* P = (Node*)malloc(sizeof(Node));
 P->Data = x;
 P->Next = NULL;
 return P;
}

void AddHead(LinkList& L, SinhVien x) {
 Node* P = AddNode(x);
 if (IsEmpty(L)) L = P;
 else {
  P->Next = L;
  L = P;
 }
}

void AddTail(LinkList& L, SinhVien x) {
 Node* P = AddNode(x);
 if (IsEmpty(L)) L = P;
 else {
  Node* M = L;
  while (M->Next != NULL) {
   M = M->Next;
  }
  M->Next = P;
  P->Next = NULL;
 }
}

void AddAny(LinkList& L, int k, SinhVien x) {
 if (k < 1 || k > Quantity(L)) cout << "Not Add. " << endl;
 else {
  if (k == 1) AddHead(L, x);
  else {
   Node* P;
   P = AddNode(x);
   Node* M = L;
   int temp = 1;
   while (temp != k - 1 && M != NULL) {
    temp++;
    M = M->Next;
   }
   P->Next = M->Next;
   M->Next = P;
  }
 }
}

void DeleteHead(LinkList& L) {
 L = L->Next;
}
void DeleteTail(LinkList& L) {
 Node* M = L;
 while (M->Next->Next != NULL) {
  M = M->Next;
 }
 M->Next = M->Next = NULL;
}

void DeleteAny(LinkList& L, int k) {
 if (k < 1 || k> Quantity(L)) cout << "Not Delete." << endl;
 else {
  if (k == 1) DeleteHead(L);
  else {
   Node* M = L;
   int temp = 1;
   while (temp != k - 1 && M != NULL) {
    temp++;
    M = M->Next;
   }
   M->Next = M->Next->Next;
  }
 }
}
void InPut(LinkList& L, int n) {
 InIt(L);
 SinhVien x;
 for (int i = 0; i < n; i++) {
  cout << "Value " << i << endl;
  InPutSV(x);
  AddTail(L, x);
 }
}

void OutPut(LinkList L) {
 for (Node* M = L; M != NULL; M = M->Next) {
  OutPutSV(M->Data);
  cout << "-----------------------------------" << endl;
 }
}
int main() {
 LinkList L;
 int n, select;
 cout << "Quantity(So luong): ";
 cin >> n;
 InPut(L, n);
 do
 {
  system("cls");
  cout << "1. Add Head. " << endl;
  cout << "2. Add Tail. " << endl;
  cout << "3. Add Any. " << endl;
  cout << "4. Delete Head. " << endl;
  cout << "5. Delete Tail. " << endl;
  cout << "6. Delete Any. " << endl;
  cout << "9. OutPut. " << endl;
  cin >> select;
  switch (select)
  {
  case 1: {
     SinhVien x;
     InPutSV(x);
     AddHead(L, x);
     break;
  }
  case 2: {
     SinhVien x;
     InPutSV(x);
     AddTail(L, x);
     break;
  }
  case 3: {
     int k;
     cout << "Possity: ";
     cin >> k;
     SinhVien x;
     InPutSV(x);
     AddAny(L, k, x);
     break;
  }
  case 9: {
     OutPut(L);
     system("pause");
     break;
  }
  case 4: {
     DeleteHead(L);
     break;
  }
  case 5: {
     DeleteTail(L);
     break;
  }
  case 6: {
     int k;
     cout << "Possity Delete: ";
     cin >> k;
     DeleteAny(L, k);
     break;
  }
  }
 } while (select != 0);
}