Thứ Ba, 11 tháng 4, 2017

Danh sách Sinh Viên Danh sách Liên kết (Linklist) kiểu con trỏ

#include 
#include 
#include 

using namespace std;


struct Sinhvien
{
 string Masv;
 string ten;
 string lop;
 int namsinh;
 float dtb;
 Sinhvien *next;
};

typedef Sinhvien *List;



Sinhvien *MakeNode() {
 Sinhvien *sv = new Sinhvien();
 cin.ignore();
 cout << "nhap ten : ";
 getline(cin, sv->ten);
 cout << "nhap msv : ";
 getline(cin, sv->Masv);
 cout << "nhap lop : ";
 getline(cin, sv->lop);
 cout << "nhap nam sinh : ";
 cin >> sv->namsinh;
 cout << "nhap diem trung binh : ";
 cin >> sv->dtb;
 sv->next = NULL;
 return sv;
}

void Innit(List L) {
 L = NULL;
}

void ThemDau(List &L) {
 Sinhvien *newsv = new Sinhvien();
 newsv = MakeNode();
 newsv->next = L;
 L = newsv;
}

void insert_last(List &L) {
 Sinhvien *s;
 s = L;
 while (s->next != NULL)
 {
  s = s->next;
 }
 Sinhvien *k = new Sinhvien();
 k = MakeNode();
 s->next = k;
 k->next = NULL;
}
void themcuoi(List &L, int n) {
 for (int i = 0; i < n; i++)
 {
  if (L == NULL)
  {
   Sinhvien *k = MakeNode();
   L = k;
  }
  else
  {

   insert_last(L);
  }
 }

}

void Output(List L) {
 cout << " ten : ";
 cout << L->ten << endl;
 cout << " msv : ";
 cout << L->Masv << endl;
 cout << " lop : ";
 cout << L->lop << endl;
 cout << " nam sinh : ";
 cout << L->namsinh << endl;
 cout << "nhap diem trung binh : ";
 cout << L->dtb << endl;

}



void Them_vitri_K(List &L, int k) {
 Sinhvien *s = MakeNode();
 Sinhvien *m = L;
 for (int i = 0; i < k - 1; i++)
 {
  m = m->next;
 }
 s->next = m->next;
 m->next = s;
}


string *chuoi = new string[1000];
void NhapChuoi(int i, string tam) {

 *(chuoi + i) = tam;
}
int TimMax(int *a, int n) {
 int max = 0;
 for (int i = 0; i < n; i++)
 {
  if (max < *(a + i))
  {
   max = *(a + i);

  }
 }
 return max;
}
void GanMangVeKhong(int *a, int n) {
 for (int i = 0; i < n; i++)
 {
  *(a + i) = 0;
 }
}

void timkiem(int n, List L) {

 int *mangDoDai = new int[n];
 GanMangVeKhong(mangDoDai, n); // gan cac phan tu trong mang ve 0;
 cin.ignore();
 string timKiem;
 cout << "nhap ten nguoi muon tim kiem : ";
 getline(cin, timKiem);

 for (int k = 0; k < n; k++)
 {
  int maxchuoi = 0; // mot sau co the trung ky tu toi hai lan, nen phai dung bien so sanh
  for (int i = 0; i < (chuoi + k)->size(); i++)
  {
   int dem = 0;
   if (timKiem[0] == chuoi[k][i])
   {
    for (int j = 0; j < timKiem.size(); j++) // so sanh va dem so luong ky tu trong gan vao mang so luong
    {

     if (timKiem[j] != chuoi[k][i])
     {
      if (dem >= maxchuoi)
      {
       maxchuoi = dem;
       *(mangDoDai + k) = maxchuoi;
       break;
      }
      else
      {
       *(mangDoDai + k) = dem;
       break;
      }
     }
     else
     {
      dem++;
      if (j == timKiem.size() - 1)
      {

       *(mangDoDai + k) = dem;
      }
     }
     i++;
    }
   }
  }
 }

 cout << "\nchuoi tim duoc la : " << endl;
 int max = TimMax(mangDoDai, n);
 for (int i = 0; i < n; i++)
 {
  if (*(mangDoDai + i) == max)
  {
   Output(L);
   cout << endl << endl;
   L = L->next;
  }
  else
  {
   L = L->next;
  }
  
 }

}

void Tim_sinhvien(List L, int n) {
 int i = 0;
 Sinhvien *s;
 s = L;
 while (s != NULL)
 {
  NhapChuoi(i, s->ten);
  s = s->next;
  i++;
 }
 timkiem(n, L);
}
void xuat(List L) {
 while (L != NULL)
 {
  Output(L);
  L = L->next;
 }
}
int main() {
 List L = NULL;
 
 int n;
 cout << "nhap vao so luong : ";
 cin >> n;
 
 themcuoi(L, n);
 system("cls"); // lenh xoa man hinh
 xuat(L); 
 cout << "-----------------------------------------------" << endl; // endl la lenh suong dong
 Tim_sinhvien(L, n);
 system("pause");
 return 0;
}
Share:

0 nhận xét:

Đăng nhận xét