Quan ly danh sach SV sap xep theo masv
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#define TRUE 1
#define FALSE 0
typedef struct sinhvien
{
int mssv;
char ten[12];
};
struct node
{
sinhvien info;
struct node *next;
};
typedef node *NODEPTR;
// tac vu getnode: cap phat mot nut cho danh sach lien ket
NODEPTR getnode(void)
{
NODEPTR p;
p = (NODEPTR)malloc(sizeof(struct node));
return(p);
}
// tac vu freenode: huy nut da cap phat
void freenode(NODEPTR p)
{
free(p);
}
/* Tac vu nodepointer: xac dinh con tro cua nut i trong danh sach lien ket
(i = 0, 1, 2, ...) */
NODEPTR nodepointer(NODEPTR *plist, int i)
{
NODEPTR p;
int vitri;
p = *plist;
vitri = 0;
while(p != NULL && vitri < i)
{
p = p->next;
vitri++;
}
return(p);
}
// Tac vu position: xac dinh vi tri cua nut p trong danh sach lien ket
int position(NODEPTR *plist, NODEPTR p)
{
int vitri;
NODEPTR q;
q = *plist;
vitri = 0;
while(q != NULL && q != p)
{
q = q->next;
vitri++;
}
if(q == NULL)
return(-1);
return(vitri);
}
// tac vu initialize: khoi dong danh sach lien ket
void initialize(NODEPTR *plist)
{
*plist = NULL;
}
// Tac vu empty: kiem tra danh sach lien ket co bi rong khong
int empty(NODEPTR *plist)
{
return(*plist == NULL ? TRUE : FALSE);
}
// tac vu push: them nut moi vao dau danh sach lien ket
void push(NODEPTR *plist, sinhvien x)
{
NODEPTR p;
p = getnode();
p->info = x;
p->next = *plist;
*plist = p;
}
// tac vu insafter: them nut moi sau nut p
void insafter(NODEPTR p, sinhvien x)
{
NODEPTR q;
if(p == NULL)
printf("khong them sinh vien vao danh sach duoc");
else
{
q = getnode();
q->info = x;
q->next = p->next;
p->next = q;
}
}
// tac vu pop: xoa nut o dau danh sach lien ket
sinhvien pop(NODEPTR *plist)
{
NODEPTR p;
sinhvien x;
if(empty(plist))
printf("Khong co sinh vien trong danh sach");
else
{
p = *plist; // nut can xoa la nut dau
x = p->info; // x la noi dung nut bi xoa
*plist = p->next;
freenode(p);
return(x);
}
}
// Tac vu delafter: xoa nut sau nut p
sinhvien delafter(NODEPTR p)
{
NODEPTR q;
sinhvien x;
// neu p la NULL hoac p chi nut cuoi
if((p == NULL) || (p->next == NULL))
printf("khong xoa sinh vien nay duoc");
else
{
q = p->next; // q chi nut can xoa
x = q->info; // x la noi dung nut bi xoa
p->next = q->next;
freenode(q);
return(x);
}
}
/* tac vu place: tac vu nay chi su dung khi them nut vao danh sach lien ket
da co thu tu */
void place(NODEPTR *plist, sinhvien x)
{
NODEPTR p, q; // q la nut truoc, p la nut sau
q = NULL;
for(p = *plist; p != NULL && x.mssv > p->info.mssv; p = p->next)
q = p;
if(q == NULL) // them nut vao dau danh sach lien ket
push(plist, x);
else // them nut vao sau nut q
insafter(q, x);
}
// tac vu clearlist: xoa tat ca cac nut trong danh sach lien ket
void clearlist(NODEPTR *plist)
{
NODEPTR p, q; // q la nut truoc, p la nut sau
q = NULL;
p = *plist;
while(p != NULL)
{
q = p;
p = p->next;
freenode(q);
}
*plist = NULL; // khoi dong lai danh sach lien ket
}
// tac vu traverse: duyet danh sach lien ket
void traverse(NODEPTR *plist)
{
NODEPTR p;
int stt = 0;
p = *plist;
if(p == NULL)
printf("
(Khong co sinh vien trong danh sach)");
while(p != NULL)
{
printf("
%5d%8d%12s", stt++, p->info.mssv, p->info.ten);
p = p->next;
}
}
/* Tac vu search: tim kiem theo phuong phap tim kiem tuyen tinh, neu khong
tim thay ham nay tra ve NULL, neu tim thay ham nay tra ve con tro chi
nut tim thay */
NODEPTR search(NODEPTR *plist, int x)
{
NODEPTR p;
p = *plist;
while(p->info.mssv != x && p != NULL)
p = p->next;
return(p);
}
// Tac vu selectionsort: sap xep danh sach lien ket theo MSSV tang dan
void selectionsort(NODEPTR *plist)
{
NODEPTR p, q, pmin;
sinhvien min;
for(p = *plist; p->next != NULL; p = p->next)
{
min = p->info;
pmin = p;
for(q = p->next; q != NULL; q = q->next)
if(min.mssv > q->info.mssv)
{
min = q->info;
pmin = q;
}
// hoan doi truong info cua hai nut p va pmin
pmin->info = p->info;
p->info = min;
}
}
// chuong trinh chinh
void main()
{
NODEPTR plist;
sinhvien sv;
NODEPTR p;
int vitri, chucnang;
char c;
clrscr();
// khoi dong danh sach lien ket
initialize(&plist);
do
{
printf("
\t\tCHUONG TRINH QUAN LY DANH SACH SINH VIEN");
printf("
Cac chuc nang cua chuong trinh:
");
printf(" 1: Xem danh sach sinh vien
");
printf(" 2: Them sinh vien vao danh sach
");
printf(" 3: Xoa sinh vien trong danh sach
");
printf(" 4: Hieu chinh sinh vien
");
printf(" 5: Sap xep danh sach theo MSSV
");
printf(" 6: Tim kiem sinh vien theo MSSV
");
printf(" 7: Them sinh vien vao danh sach da co thu tu
");
printf(" 8: Xoa toan bo danh sach
");
printf(" 0: Ket thuc chuong trinh
");
printf("Chuc nang ban chon: ");
scanf("%d", &chucnang);
switch(chucnang)
{
case 1:
{
printf("
Danh sach sinh vien: ");
printf("
STT MSSV TEN");
traverse(&plist);
break;
}
case 2:
{
printf("
Vi tri them (0, 1, 2, ...): ");
scanf("%d", &vitri);
printf("Ma so sinh vien: ");
scanf("%d", &sv.mssv);
printf("Ten sinh vien: ");
scanf("%s", &sv.ten);
if(vitri == 0)
push(&plist, sv);
else
{
p = nodepointer(&plist, vitri-1);//p chi nut truoc nut can them
if(p == NULL)
printf("Vi tri khong hop le");
else
insafter(p, sv);
}
break;
}
case 3:
{
printf("
Vi tri xoa (0, 1, 2, ...): ");
scanf("%d", &vitri);
if(vitri == 0)
pop(&plist);
else
{
p = nodepointer(&plist, vitri-1);//p chi nut truoc nut can xoa
delafter(p);
}
break;
}
case 4:
{
printf("
Vi tri hieu chinh (0, 1, 2, ...): ");
scanf("%d", &vitri);
p = nodepointer(&plist, vitri); // p chi nut can hieu chinh
if(p == NULL)
printf("Vi tri khong phu hop");
else
{
printf("
STT:%d MSSV:%d TEN:%s", vitri,
p->info.mssv, p->info.ten);
printf("
Ma so sv moi: ");
scanf("%d", &sv.mssv);
printf("Ten sv moi: ");
scanf("%s", &sv.ten);
p->info = sv;
}
break;
}
case 5:
{
printf("
Ban co chac khong? (c/k): ");
c = getche();
if(c == 'c' || c == 'C')
selectionsort(&plist);
break;
}
case 6:
{
printf("
Ma so sinh vien can tim: ");
scanf("%d", &sv.mssv);
p = search(&plist, sv.mssv);
if(p == NULL)
printf("Khong co sinh vien co MSSV %d trong danh sach",
sv.mssv);
else
printf("Tim thay o vi tri %d trong danh sach", position(&plist, p));
break;
}
case 7:
{
printf("
Ban nho sap xep danh sach truoc. Nhan phim bat ky ...");
getche();
printf("
Ma so sinh vien: ");
scanf("%d", &sv.mssv);
printf("Ten sinh vien: ");
scanf("%s", &sv.ten);
place(&plist, sv);
break;
}
case 8:
{
printf("
Ban co chac khong (c/k): ");
c = getche();
if(c == 'c' || c == 'C')
clearlist(&plist);
break;
}
}
} while(chucnang != 0);
// xoa tat ca cac nut tren danh sach lien ket
clearlist(&plist);
}
Coder: bugatino
Nguồn: http://hauionline.com
Bạn đang đọc truyện trên: AzTruyen.Top