30/09/2018, 21:23
Moi người giúp e xem hàm chèn của e sai đâu với !(DANH SACH LIEN KET DON)
Moi người giúp e xem hàm chèn của e sai đâu với !(DANH SACH LIEN KET DON)
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std ;
struct SV{
char ma[30];
char ten[30];
int tuoi;
};
struct Node{
SV data;
struct Node *pnext;
};
typedef struct Node NODE;
struct List{
NODE *phead;
NODE *ptail;
};
typedef struct List LIST;
NODE* GetNode(SV x)//tao mot nut new
{
NODE *p=new(NODE);
if(p==NULL)
return NULL;
else
{
p->data=x;//lay data gan vao x
p->pnext=NULL;
return p;
}
}
void init(LIST &l)
{
l.phead=l.ptail=NULL;
}
void input (SV &x)
{
cout<<"
Nhap MSSV: "; fflush(stdin); gets(x.ma);
cout<<"
Nhap ten: "; fflush(stdin); gets(x.ten);
cout<<"
Nhap tuoi: ";cin>>x.tuoi;
}
void chencuoi(LIST &l,NODE *p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
{
l.ptail->pnext=p;
l.ptail=p;
}
}
void nhap(LIST &l)
{
SV x;
char kt;
printf("
Nhan phim bat ki de tiep tuc nhap.");
printf("
Nhan 0 de dung nhap.");
do
{
kt=getch();
if(kt=='0') break;
input(x);
NODE *p=GetNode(x);
chencuoi(l,p);
} while (1);
}
void output (SV x)
{
cout<<"MA SINH VIEN:"<<x.ma<<endl;
cout<<"TEN SINH VIEN:"<<x.ten<<endl;
cout<<"TUOI SINH VIEN:"<<x.tuoi<<endl;
}
void xuat (LIST l)
{
NODE *p;
p=l.phead;
while(p!=NULL)
{
output(p->data);
cout<<"<----------------------------->"<<endl;
p=p->pnext;
}
}
void chendau(LIST &l,NODE *p)
{
if(l.phead==NULL)
l.phead=l.ptail=p;
else
p->pnext=l.phead;
l.phead=p;
}
void nhapchendau(LIST &l)
{
SV x;
char kt;
printf("
Nhan phim bat ki de chen dau.");
printf("
Nhan 0 de dung nhap.");
do
{
kt=getch();
if(kt=='0') break;
input(x);
NODE *p=GetNode(x);
chendau(l,p);
} while (1);
}
// chen vi tri bat ki tru dau va cuoi
void chen(LIST &l,NODE *p)
{
char a[5];
cout<<"Nhap vi tri muon chen";fflush(stdin);
cin>>a;
NODE *k;
while(k)
{
if(!strcmp(k->data.ma,a))// tim node muon chen vao sau no
{
NODE *g;
g=k->pnext;//g hien tai dang la k;lay k tro sang node sau node chuan bi chen
k->pnext=p;//lay k tro sang node chuan bi chen
p->pnext=g;//NOde chuan chuan bi chen tro sang cai node deo tiep the
}
return;
}
}
void nhapchentuyy(LIST &l)
{
SV x;
char kt;
printf("
Nhan phim bat ki de bat dau chen tuy y.");
printf("
Nhan 0 de dung nhap.");
do
{
kt=getch();
if(kt=='0') break;
input(x);
NODE *p=GetNode(x);
chen(l,p);
} while (1);
}
//xoa dau
void xoadau(LIST &l)
{
NODE *p=l.phead;
l.phead=l.phead->pnext;
delete p;
}
//xoa cuoi
void xoacuoi(LIST &l)
{
NODE *p;
for(NODE *k=l.phead;k;k=k->pnext)
{
if(k==l.ptail)
{
l.ptail=p;// t gan truoc p cai node cuoi i
p->pnext=NULL; //cat lien ket giua node p va node k
delete k;
return;
}
p=k;
}
}
//xoa vi tri bat ki
void xoa(LIST &l)
{
char a[5];
cout<<"Nhap vi tri can xoa:";fflush(stdin);
cin>>a;
NODE *g;
for(NODE *k=l.phead;k;k=k->pnext)
{
if(!strcmp(k->data.ma,a))
{
g=k->pnext;
k->pnext=g->pnext;
delete g;
return;
}
}
}
main()
{ LIST l; SV x;
init(l);
nhap(l);
cout<<"<-----------------Danh sach sau khi nhap------------------------>"<<endl;
xuat(l);
/* xoadau(l);
cout<<"<-----------------Danh sach sau khi xoa------------------------>"<<endl;
xuat(l);
xoa(l);
cout<<"<-----------------Danh sach sau khi xoa------------------------>"<<endl;
xuat(l);
nhapchendau(l);
cout<<"<-----------------Danh sach sau khi chen SV vao dau------------------------>"<<endl;
xuat(l);*/
nhapchentuyy(l);
cout<<"<-----------------Danh sach sau khi chen SV vao tuy y------------------------>"<<endl;
xuat(l);
getch();
}
Bài liên quan
This post was flagged by the community and is temporarily hidden.
tks vi da xem bai minh lam dc roi