danhsachlienketsinhvien
#include <conio.h>
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
typedef struct
{
int masv;
char hoten[20];
char lop[10];
char quequan[100];
int gioitinh[3];
float diemtb[10];
}sinhvien;
typedef struct node
{
sinhvien infor;
struct node *next;
}*NODEPTR;
//HAM KHOI TAO
void Initialize(NODEPTR *plist)
{
*plist =NULL;
}
//Ham cap phat bo nho ch 1 phan tu
NODEPTR Getnode()
{
NODEPTR p;
p=(NODEPTR)malloc(sizeof(struct node));
p->next=NULL;
return p;
}
//Ham chen 1 phan tu vao dau danh sach
void Inserttop(NODEPTR *plist, sinhvien x)
{
NODEPTR p;
p =Getnode();
p -> infor = x;
if(*plist == NULL)
*plist = p;
else
{
p->next = *plist;
*plist = p;
}
}
// Ham chen 1 phan tu vao cuoi danh sach
void Insertbottom(NODEPTR *plist, sinhvien x)
{
NODEPTR p, q;
p= Getnode();
p->infor = x;
if(*plist == NULL)
*plist = p;
else
{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
// Ham chen 1 phan tu vao sau 1 phan tu(chen vao giua danh sach)
void Insertafter(NODEPTR *plist, sinhvien x, int n)
{
NODEPTR p, q;
int count = 1;
p = Getnode();
p->infor = x;
if(n == 1)
{
p-> next = *plist;
*plist = p;
}
else
{
q = *plist;
while(count++ < n-1 && q->next != NULL)
q = q-> next;
p->next = q->next;
q->next = p;
}
}
// Ham xoa phan tu dau
void Deltop(NODEPTR *plist)
{
NODEPTR p, q;
p = *plist;
if(*plist == NULL)
{
printf("
Danh sach rong");
return;
}
q = p;
p =p->next;
*plist = p;
free(q);
}
// Ham xoa phan tu cuoi
void Delbottom(NODEPTR *plist)
{
NODEPTR p, q;
if(*plist == NULL)
{
printf("
Danh sach rong");
return;
}
p = *plist;
q = p->next;
while(q->next != NULL)
{
q = q->next;
p = p->next;
}
p->next = NULL;
free(q);
}
// Ham xoa phan tu thu i
void Delcurrent(NODEPTR *plist, int n)
{
NODEPTR p, q;
int i;
if(*plist == NULL)
{
printf("
Danh sach rong");
return;
}
p = *plist;
i = 0;
while(p != NULL && i<n-2)
{
i = i+1;
p = p->next;
}
q = p->next;
p->next = q->next;
free(q);
}
//ham duyet danh sach
void Travenode(NODEPTR *plist)
{
NODEPTR p;
if(*plist == NULL)
{
printf("
Danh sach rong");
}
else
{
p = *plist;
while(p != NULL)
{
printf("
%d\t%s\t%s\t%s\t%s\t%s", p->infor.masv, p->infor.hoten, p->infor.lop, p->infor.quequan, p->infor.gioitinh, p->infor.diemtb);
p = p->next;
}
}
}
//ham sap xep
void sortnode(NODEPTR *plist)
{
NODEPTRp,q;
sinhvien temp;
for(p=*plist;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(p->infor.masv > q->infor.masv)
{
temp=p->infor;
p->infor=q->infor;
q->infor=temp;
}
}
}
}
//tim kiem phan tu
void searchnode(NODEPTR *plist, int masv)
{
NODEPTR p;
p=*plist;
while(p!=NULL && p->infor.masv != masv)
p=p->next;
if(p==NULL)
printf("
Node khong ton tai");
else
printf("
Node can tim: %-5d%20s",p->infor.masv,p->infor.hoten, p->infor.lop, p->infor.quequan, p->infor.gioitinh, p->infor.diemtb);
}
int main()
{
char c;
int i;
sinhvien x;
NODEPTR plist;
Initialize(&plist);
while(1)
{
fflush(stdin);
printf("
CAC THAO TAC TREN DS LIEN KET DON ");
printf("
1. Them phan tu vao dau danh sach");
printf("
2. Them phan tu vao cuoi danh sach");
printf("
3. Them phan tu vao vi tri i");
printf("
4. Xoa phan tu dau danh sach");
printf("
5. Xoa phan tu cuoi danh sach");
printf("
6. Xoa phan tu tai vi tri i");
printf("
7. In danh sach");
printf("
8. Sap xep");
printf("
9. Tim kiem");
printf("
0. Thoat
");
printf("Chon : ");
c = getch();
switch(c)
{
//them phan tu vao dau danh sach
case '1':
printf("nhap vao ma so sv: ");
scanf("%d", &x.masv);
printf("nhap vao ho ten sv: ");
scanf("%s", &x.hoten);
printf("Lop: ");
scanf("%s", &x.lop);
printf("Nhap que quan: ");
scanf("%s", &x.quequan);
printf("Nhap gioi tinh: ");
scanf("%s", x.gioitinh);
printf("Nhap diem trung binh: ");
scanf("%s", x.diemtb);
Inserttop(&plist, x);
break;
case '2':
printf("nhap vao ma so sv: ");
scanf("%d", &x.masv);
printf("nhap vao ho ten sv: ");
scanf("%s", &x.hoten);
printf("Lop: ");
scanf("%s", &x.lop);
printf("Nhap que quan: ");
scanf("%s", &x.quequan);
printf("Nhap gioi tinh: ");
scanf("%s", x.gioitinh);
printf("Nhap diem trung binh: ");
scanf("%s", x.diemtb);
Insertbottom(&plist, x);
break;
case '3':
printf("nhap vao ma so sv: ");
scanf("%d", &x.masv);
printf("nhap vao ho ten sv: ");
scanf("%s", &x.hoten);
printf("nhap vao vi tri: ");
scanf("%d", &i);
printf("Lop: ");
scanf("%s", &x.lop);
printf("Nhap que quan: ");
scanf("%s", &x.quequan);
printf("Nhap gioi tinh: ");
scanf("%s", x.gioitinh);
printf("Nhap diem trung binh: ");
scanf("%s", x.diemtb);
Insertafter(&plist, x, i);
break;
case '4':
Deltop(&plist);
break;
case '5':
Delbottom(&plist);
break;
case '6':
printf("Nhap vao vi tri: ");
scanf("%d", &i);
Delcurrent(&plist, i);
break;
//in phan tu
case '7':
Travenode(&plist);
break;
case '8':
sortnode(&plist);
break;
case '9':
printf("Nhap vao masv: ");
scanf("%d", &i);
searchnode(&plist, i);
break;
case '0':
exit(0);
}
}
}
Bạn đang đọc truyện trên: Truyen4U.Com