Vy Thanh Định- Web Master
- Giới tính : Bài gửi : 228
Tổng Điểm : 544
Điểm Thưởng : 16
Sinh Nhật : 19/05/1990 Bị Dụ Dỗ : 11/09/2009
Tuổi : 34
by Vy Thanh Định 6/11/2009, 23:18
- Code:
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>
#define TRUE 1
#define FALSE 0
typedef struct sinhvien
{
unsigned mssv;
char ten[35];
};
struct node
{
sinhvien info;
struct node *next;
};
typedef node *NODE;
NODE getnode(void)
{
NODE p;
p = (NODE)malloc(sizeof(struct node));
return(p);
}
void XoaNode(NODE p)
{
free(p);
}
void KhoiTao(NODE *plist)
{
*plist = NULL;
}
void ThemNodeMoi(NODE *plist, sinhvien x)
{
NODE p;
p = getnode();
p->info = x;
p->next = *plist;
*plist = p;
}
void ChenVaoSauNodeP(NODE p, sinhvien x)
{
NODE 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;
}
}
void ThemVaoCoThuTu(NODE *plist, sinhvien x)
{
NODE p, q;
q = NULL;
for(p = *plist; p != NULL && x.mssv > p->info.mssv; p = p->next)
q = p;
if(q == NULL)
ThemNodeMoi(plist, x);
else
ChenVaoSauNodeP(q, x);
}
void XoaTatCaNode(NODE *plist)
{
NODE p, q;
q = NULL;
p = *plist;
while(p != NULL)
{
q = p;
p = p->next;
XoaNode(q);
}
*plist = NULL;
}
void OutPut(NODE *plist)
{
NODE p;
int stt = 0;
p = *plist;
if(p == NULL)
printf("\n (Khong co sinh vien trong danh sach)");
while(p != NULL)
{
printf("\n|%3d|%6d|%35s|", stt++, p->info.mssv, p->info.ten);
p = p->next;
}
}
void SapXepTangDan(NODE *plist)
{
NODE 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;
}
pmin->info = p->info;
p->info = min;
}
}
void main()
{
NODE plist;
sinhvien sv;
NODE p;
int vitri, chucnang;
char c;
KhoiTao(&plist);
do
{
cout<<"\n+------------------------------------------+";
cout<<"\n| CHUONG TRINH QUAN LY DANH SACH SINH VIEN |";
cout<<"\n+------------------------------------------+";
cout<<"\n| Cac chuc nang cua chuong trinh: |";
cout<<"\n| 1: Xem danh sach sinh vien |";
cout<<"\n| 2: Them sinh vien vao danh sach |";
cout<<"\n| 3: Sap xep theo MSSV |";
cout<<"\n| 4: Them SV vao dsach da co thu tu |";
cout<<"\n+------------------------------------------+";
cout<<"\n| 0: Thoat khoi chuong trinh |";
cout<<"\n+------------------------------------------+\n";
cin>>chucnang;
switch(chucnang)
{
case 1 :
{
cout<<"\n+----------------------------------------------+";
cout<<"\n| Danh sach sinh vien :bb |";
cout<<"\n+---+------+-----------------------------------+";
cout<<"\n|STT| MSSV | HO TEN SINH VIEN |";
cout<<"\n+---+------+-----------------------------------+";
OutPut(&plist);
cout<<"\n+---+------+-----------------------------------+";
break;
}
case 2 :
{
cout<<"Nhap ma so sinh vien :";
cin>>sv.mssv;
cout<<"Ten sinh vien :";
gets(sv.ten);
ThemNodeMoi(&plist,sv);
break;
}
case 3 :
{
cout<<"Sap xep theo MSSV, ban co chac khong ? (c/k): ";
c = getch();
if(c == 'C' || c == 'c')
SapXepTangDan(&plist);
break;
}
case 4 :
{
cout<<"Nhan phim bat ky de sap xep ..."; getch();
cout<<"Nhap ma so sinh vien :";
cin>>sv.mssv;
cout<<"Ten sinh vien :";
gets(sv.ten);
ThemVaoCoThuTu(&plist,sv);
break;
}
}
}while(chucnang != 0);
XoaTatCaNode(&plist);
}