Mong các anh sửa lổi giúp em .em chỉ mới vừa học danh sách liên kết thôi.In ra bị lổi rôi.
Code:
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
typedef struct tag_SNODE
{
int info;
tag_SNODE *pnext;
} SNODE,*SNODEPTR;
typedef struct {
SNODEPTR phead,ptail;
}list;
SNODEPTR CreateSNODE(int key)
{
SNODEPTR p;
p=(SNODEPTR)malloc(sizeof(SNODE));
if (!p) return NULL;
p->info = key;
p->pnext = NULL;
return p;
}
//Them vao cuoi danh sach
void AddLast(list &l,SNODEPTR p)
{
if(!l.phead)
l.phead=l.ptail=p;
else
{
l.ptail->pnext=p;
l.ptail=p;
}
}
//Nhap danh sach lien ket tu ban phim
void nhap(list &l)
{
SNODEPTR p;
int t,v=1;
do {
printf("Nhap phan tu thu %d:",v);
scanf("%3d",&t);
if(t)
{
p=CreateSNODE(t);
if(!p) return;
AddLast(l,p);
}
v++;
} while(t);
}
//In danh sach
void in(list &l)
{
SNODEPTR p;
p=l.phead;
while(p)
{
printf("%5d",p->info);
p=p->pnext;
}
}
void main()
{
clrscr();
list l;
printf("Nhap cac phan tu lan luot & nhap 0 de ket thuc nhap\n");
nhap(l);
in(l);
getch();
}