Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- typedef struct sinhvien
- {
- char ht[50], qq[50];
- float diem;
- }
- typedef struct Node
- {
- item Data;
- Node *next;
- };
- typedef Node *List;
- void Init (List &L)
- {
- L=NULL;
- }
- int Isempty (List L)
- {
- return (L==NULL);
- }
- int Len (List L)
- {
- Node *P=L;
- int i=0;
- while (P!=NULL)
- {
- i++;
- P=P->next;
- }
- return i;
- }
- void Make_sv (item &sv)
- {
- char ht[50], qq[50];
- float diem;
- printf ("\tHo ten: ");
- fflush(stdin);
- gets(ht);
- strcpy(sv.ht,ht);
- printf ("\tQue quan: ");
- fflush(stdin);
- gets(qq);
- strcpy(sv.qq,qq);
- fflush(stdin);
- printf ("\tDiem: ");
- scanf("%f",&diem);
- sv.diem=diem;
- }
- void Make_Node (Node *P, item sv)
- {
- P->next = NULL;
- strcpy (P->Data.ht,sv.ht);
- strcpy (P->Data.qq,sv.qq);
- P->Data.diem = sv.diem;
- }
- void Insert_first (List &L, item sv)
- {
- Node *P;
- P = (Node *) malloc (sizeof (Node));
- Make_Node(P,sv);
- P->next = L;
- L = P;
- }
- void Insert_k (List &L, item sv, int k)
- {
- Node *P, *Q = L;
- P = (Node *) malloc (sizeof (Node));
- int i=1;
- if (k<1 || k> Len(L)+1) printf("Vi tri chen khong hop le !");
- else
- {
- Make_Node(P,sv);
- if (k == 1) Insert_first(L,sv);
- else
- {
- while (Q != NULL && i != k-1)
- {
- i++;
- Q = Q->next;
- }
- P->next = Q->next;
- Q->next = P;
- }
- }
- }
- int Search_ht (List L, char ht[50])
- {
- Node *P=L;
- int i=1;
- while (P != NULL && strcmp(P->Data.ht,ht))
- {
- P = P->next;
- i++;
- }
- if (P != NULL) return i;
- else return 0;
- }
- int Search_qq (List L, char qq[50])
- {
- Node *P=L;
- int i=1;
- while (P != NULL && strcmp(P->Data.qq,qq))
- {
- P = P->next;
- i++;
- }
- if (P != NULL) return i;
- else return 0;
- }
- void Del_frist (List &L, item &sv)
- {
- sv = L->Data;
- L = L->next;
- }
- void Del_k (List &L, item &sv, int k)
- {
- Node *P=L;
- int i=1;
- if (k<1 || k>Len(L)) printf("Vi tri xoa khong hop le !");
- else
- {
- if (k==1) Del_frist(L,sv);
- else
- {
- while (P != NULL && i != k-1)
- {
- P=P->next;
- i++;
- }
- P->next = P->next->next;
- }
- }
- }
- void Del_sv_ht (List &L, item &sv, char ht[50])
- {
- while (Search_ht(L,ht)) Del_k (L,sv,Search_ht(L,ht));
- }
- void Input (List &L)
- {
- int i=0;
- char ht[50], qq[50];
- float diem;
- item sv;
- do
- {
- i++;
- printf ("Nhap thong tin sinh vien thu %d : \n",i);
- printf ("\tHo ten: ");
- fflush(stdin);
- gets(ht);
- if (ht[0] !='\0')
- {
- strcpy(sv.ht,ht);
- printf ("\tQue quan: ");
- fflush(stdin);
- gets(qq);
- strcpy(sv.qq,qq);
- fflush(stdin);
- printf ("\tDiem: ");
- scanf("%f",&diem);
- sv.diem=diem;
- Insert_k(L,sv,Len(L)+1);
- }
- } while(ht[0] !='\0');
- }
- void Output (List L)
- {
- Node *P=L;
- printf ("%20s %20s %10s\n","Ho ten","Que quan","Diem");
- while (P != NULL)
- {
- printf("%20s %20s %10.2f\n",P->Data.ht,P->Data.qq,P->Data.diem);
- P = P->next;
- }
- printf("\n");
- }
- int main()
- {
- List L;
- Init(L);
- Input(L);
- Output(L);
- int lua_chon;
- printf("Moi ban chon phep toan voi DS LKD:");
- printf("\n1: Kiem tra DS rong");
- printf("\n2: Do dai DS");
- printf("\n3: Chen sinh vien vao vi tri k trong DS");
- printf("\n4: Tim sinh vien theo Ho ten");
- printf("\n5: Tim sinh vien theo Que quan");
- printf("\n6: Xoa sinh vien tai vi tri k");
- printf("\n7: Xoa sinh vien theo Ho ten");
- printf("\n8: Thoat");
- do
- {
- printf("\nBan chon: ");
- scanf("%d",&lua_chon);
- switch (lua_chon)
- {
- case 1:
- {
- if (Isempty(L)) printf("DS rong !");
- else printf ("DS khong rong !");
- break;
- }
- case 2: printf ("Do dai DS la: %d.",Len(L));break;
- case 3:
- {
- item sv;
- int k;
- printf ("Nhap phan tu can chen vao DS: \n");
- Make_sv(sv);
- printf ("Nhap vi tri can chen: ");
- scanf ("%d",&k);
- Insert_k (L,sv,k);
- printf ("DS sau khi chen:\n");
- Output(L);
- break;
- }
- case 4:
- {
- char ht[50];
- printf ("Moi ban nhap vao Ho ten can tim: ");
- fflush(stdin);
- gets(ht);
- int k = Search_ht (L,ht);
- if (k) printf ("Tim thay sinh vien co %s trong DS tai vi tri thu: %d",ht,k);
- else printf ("Khong tim thay sinh vien co %d trong danh sach !",ht);
- break;
- }
- case 5:
- {
- char qq[50];
- printf ("Moi ban nhap vao Que quan can tim: ");
- fflush(stdin);
- gets(qq);
- int k = Search_qq (L,qq);
- if (k) printf ("Tim thay sinh vien co %s trong DS tai vi tri thu: %d",qq,k);
- else printf ("Khong tim thay sinh vien co %d trong danh sach !",qq);
- break;
- }
- case 6:
- {
- int k;
- item sv;
- printf ("Nhap vi tri can xoa: ");
- scanf ("%d",&k);
- Del_k (L,sv,k);
- printf ("DS sau khi xoa:\n");
- Output(L);
- break;
- }
- case 7:
- {
- item sv;
- char ht[50];
- printf ("Nhap Ho ten can xoa: ");
- fflush(stdin);
- gets(ht);
- Del_sv_ht (L,sv,ht);
- printf ("DS sau khi xoa:\n");
- Output(L);
- break;
- }
- case 8: break;
- }
- }while (lua_chon !=7);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement