DSLKDOI
// DSLKDOI.cpp : Defines the entry point for the console application.
#include "stdafx.h"
struct Sach
{
int masach;
char tensach[30];
long giaban;
};
struct nodelist
{
Sach data;
nodelist *pNext;
nodelist *pPrev;
};
struct list
{
nodelist *pHead;
nodelist *pTail;
};
void Init(list &l)
{
l.pHead=l.pTail=NULL;
}
nodelist *GetNode(Sach s)
{
nodelist *p=new nodelist;
if(p==NULL)
return NULL;
p->data=s;
p->pNext=NULL;
p->pPrev=NULL;
return 0;
}
void AddHead(list &l, nodelist *p)
{
if(l.pHead==NULL)
l.pHead=l.pTail=p;
else
{
p->pNext=l.pHead;
l.pHead->pPrev=p;
l.pHead=p;
}
}
void AddTail(list &l, nodelist *p)
{
if(l.pHead==l.pTail==NULL)
l.pHead=l.pTail=p;
else
{
l.pTail->pNext=p;
p->pPrev=l.pTail;
l.pTail=p;
}
}
nodelist *GetHead(list &l)
{
if(l.pHead==NULL)
return NULL;
else if(l.pHead==l.pTail)
{
nodelist *p=new nodelist;
p=l.pHead;
p->pNext=p->pNext=NULL;
l.pHead=l.pTail=NULL;
return p;
}
else
{
nodelist *p=new nodelist;
p=l.pHead=l.pHead->pNext;
l.pHead->pPrev=NULL;
p->pNext=p->pPrev=NULL;
return p;
}
}
nodelist *GetTail(list &l)
{
if(l.pTail==NULL)
return NULL;
else if(l.pHead==l.pTail)
{
nodelist *p= new nodelist;
p=l.pTail;
p->pNext=p->pPrev=NULL;
l.pTail=l.pHead-NULL;
return p;
}
else
{
nodelist *p= new nodelist;
p=l.pTail;
l.pTail=l.pTail ->pPrev;
p->pPrev=NULL;
l.pTail->pNext=NULL;
return p;
}
}
void ReadFile(list &l)
{
FILE *f = fopen("input.txt","rt");
while(!feof(f))
{
Sach temp;
fscanf(f,"%d",&temp.masach);
fscanf(f,"%s",&temp.tensach);
fscanf(f,"%ld",&temp.giaban);
nodelist *p=GetNode(temp);
AddHead(l,p);
}
fclose(f);
}
void PrintReverse(list l)
{
nodelist*p;
for(p=l.pTail;p;p=p->pPrev)
{
Sach s=p->data;
printf("%d",s.masach);
printf("%s",s.tensach);
printf("%ld",s.giaban);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
list l;
Init(l);
ReadFile(l);
PrintReverse(l);
return 0;
}
Bạn đang đọc truyện trên: AzTruyen.Top