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