You are on page 1of 4

#include<stdio.

h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<ctype.h>
typedef struct sinhvien{
int masv;
char ten[30];
float dtb;
};
typedef struct node{
sinhvien info;
node *next;
};
node *head;
int count=0;
void init(){
char *tl="Chua co";
head->info.masv=0;
strcpy(head->info.ten, tl);
head->info.dtb=0;
head->next=NULL;
count=0;
}
node *makenode(sinhvien x){
node *p=new node;
p->info.masv=x.masv;
strcpy(p->info.ten, x.ten);
p->info.dtb=x.dtb;
p->next=NULL;
return p;
}
void insert(node *p,int n){
node *q,*r;
if(n==0){

q=head->next;
head->next=p;
p->next=q;
cout<<"Da chen vao dau danh sach";
count++;
return;
}
q=head->next;int i=0;
while(q!=NULL&&i<n){
r=q;
q=q->next;
i++;
}
r->next=p;
p->next=q;
cout<<"\n Da chen vao vi tri"<<n<<" ";
count++;
}
void remove(int n){
node *p,*q=head,*r;
int i=0;
if(n==0){
p=head->next;
head->next=p->next;
cout<<"\nLoai bo sinh vien: ";
cout<<"\n Ma sinh vien: "<<p->info.masv;
cout<<"\n Ho va ten: "<<p->info.ten;
cout<<"\n Diem trung binh: "<<p->info.dtb;
getch();
delete p;
count--;
return;
}
if(n>count){
while(q->next!=NULL){
r=q;
q=q->next;
}
r->next=NULL;
cout<<"\nLoai bo sinh vien: ";
cout<<"\n Ma sinh vien: "<<q->info.masv;
cout<<"\n Ho va ten: "<<q->info.ten;
cout<<"\n Diem trung binh: "<<q->info.dtb;
getch();
delete q;
count--;
return;
}
while(i<n){
r=q;
q=q->next;
i++;
}
p=q->next;
r->next=p;
cout<<"\nLoai bo sinh vien: ";
cout<<"\n Ma sinh vien: "<<q->info.masv;
cout<<"\n Ho va ten: "<<q->info.ten;
cout<<"\n Diem trung binh: "<<q->info.dtb;
getch();
delete q;
count--;
return;
}
void traver(){
node *q=head->next;
cout<<"\n Cac sinh vien trong danh sach gom: ";
while(q!=NULL){
cout<<"\n Ma sinh vien: ";
cout<<q->info.masv<<" \n";
cout<<"\n Ho va Ten: ";
cout<<q->info.ten<<" \n";
cout<<"\n Diem trung binh: ";
cout<<q->info.dtb<<" \n";
q=q->next;
}
getch();
}
void main(){
clrscr();
int chon;
int n;
sinhvien x;
node *p,ketqua;
init();
while(1){
cout<<"\nDANH SACH LIEN KET DON\n";
cout<<"\n 1. Them mot sinh vien vao dau danh sach";
cout<<"\n 2. Them mot sinh vien vi tri bat ky";
cout<<"\n 3. Loai bo sinh vien o dau danh sach";
cout<<"\n 4. Loai bo sinh vien o vi tri bat ky";
cout<<"\n 5. Duyet danh sach";
cout<<"\n z. Thoat";
chon=getch();
chon=toupper(chon);
if(chon=='Z') break;
switch(chon){
case'1':
cout<<"\n Nhap thong tin cua sinh vien
can them: ";
cout<<"\n Ma sinh vien: ";
cin>>x.masv;
cout<<"\n Ten: ";
fflush(stdin);gets(x.ten);
cout<<"\n Diem trung binh: ";
cin>>x.dtb;
p=makenode(x);
insert(p,0);
break;
case'2':
cout<<"\n Nhap thong tin cua sinh vien
can them: ";
cout<<"\n Ma sinh vien: ";
cin>>x.masv;
cout<<"\n Ten: ";
fflush(stdin);gets(x.ten);
cout<<"\n Diem trung binh: ";
cin>>x.dtb;
p=makenode(x);
cout<<"\n Nhap vi tri can them: ";
cin>>n;
insert(p,n);
break;
case'3':
remove(0);
break;

case'4':
cout<<"\n Nhap vi tri can loai bo: ";
cin>>n;
remove(n);
break;
case'5':
traver();
break;
}
clrscr();
}
getch();
}

You might also like