CTDL Pro

Màu nền
Font chữ
Font size
Chiều cao dòng

#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

struct HOCSINH

{

char hoten[20];

char diachi[50];

int mahs;

};

struct NODE

{

HOCSINHinfo;

struct NODE * pNext;

};

struct LIST

{

NODE * pHead;

NODE * pTail;

};

NODE* GetNode(HOCSINH hs)

{

NODE *p;

p = new NODE;

if (p==NULL)

{

cout<<"Khong du bo nho";

return NULL;

}

p->info = hs;

p->pNext = NULL;

return p;

}

void AddFirst(LIST &l, NODE* new_ele)

{

if (l.pHead == NULL)

l.pHead = l.pTail = new_ele;

else {

new_ele->pNext = l.pHead;

l.pHead = new_ele;

}

}

NODE* InsertHead(LIST &l, HOCSINH hs)

{

NODE* new_ele = GetNode(hs);

if (new_ele == NULL)

return NULL;

AddFirst(l, new_ele);

return new_ele;

}

void AddTail(LIST &l, NODE* new_ele)

{

if (l.pHead == NULL)

l.pHead = l.pTail = new_ele;

else l.pTail=l.pTail->pNext=new_ele;

}

NODE* InsertTail(LIST &l, HOCSINH hs)

{

NODE* new_ele = GetNode(hs);

if (new_ele == NULL)

return NULL;

AddTail(l, new_ele);

return new_ele;

}

void Nhaphs(HOCSINH & hs,LIST& l)

{

char c;

int chon;

do{

cout<<"Nhap ho ten:"<<endl;

cin>>hs.hoten;

cout<<"Nhap dia chi:"<<endl;

cin>>hs.diachi;

cout<<"Nhap ma hoc sinh:"<<endl;

cin>>hs.mahs;

do {

cout<<"Anh (chi) muon them vao dau hoac cuoi danh sach? (1: Dau, 2: Cuoi)";

cin>>chon;

if(chon==1)

{

InsertHead(l,hs);

break;

}

else if(chon==2)

{

InsertTail(l,hs);

break;

}

else cout<<"Vui long chon 1 hoac 2";

}while(chon!=1||chon!=2);

cout<<"Ban muon nhap tiep hay ko(Yes:y,No:n):"<<endl;

cin>>c;

if(c=='n'||c=='N')

break;

}while(c=='Y'||c=='y');

}

//xuat danh sach

void xuatdanhsach(NODE *p)

{

cout<<"Ho ten:"<<p->info.hoten<<endl;

cout<<"Dia chi:"<<p->info.diachi<<endl;

cout<<"Ma hoc sinh:"<<p->info.mahs<<endl;

}

void ProcessList (LIST &l)

{

NODE*p;

p = l.pHead;

int thutu=1;

cout<<"Danh sach cac hoc sinh:"<<endl;

while (p!= NULL)

{

cout<<"Thong tin hoc sinh thu:"<<thutu<<endl;

xuatdanhsach(p);

p = p->pNext;

                 thutu++;

}

}

void main()

{

struct HOCSINH hs;

struct LIST l;

struct NODE * new_ele;

Nhaphs(hs,l);

ProcessList(l);

}

Bạn đang đọc truyện trên: Truyen2U.Pro

#123