Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book
{
char pIBSN[21];
char pAuthor[21];
char pTitle[58]; // Increase from 21 to 58 to avoid overlow condition
int published;
struct Book *pNext; //pointer to next book
}BookEntry; // This is needed to malloc structure
Book *FirstBook = NULL; // first book
Book *LastBook = NULL; // last book
struct Book Collection[18] =
{
{"1-800-423-0563","Adam Drozdek","Data structures and algorihtms in C++",2001},
{"1-56592-453-3","Kyle Loudon","Mastering algorithms with C",1999},
{"1-4815-1623-42","Hal Hanzo","The Dharma Foundation",1958},
{"2-454-3543-44","Michael Jackson","King Of Pop",1997},
{"2-244-44-21","Robert Zemeckiz","Forrest Gump",1994},
{"4-567-3-675","Steven Spielberg","Amistad",1997},
{"3-45-44-535","Michel Bay","Transformers",2007},
{"6-465-34565-33","Luc Besson","The Messenger - The Story Of Jon Of Arc", 1999},
{"1-455-6463-33","James Cameron","Terminator 2: Judgement Day",1991},
{"8-900-345-343","George Lucas","Star Wars: Episode III-Revenge Of The Sith",2005},
{"1-800-34-334","Michel Mann","Collateral",2004},
{"1-455-435-434","Terrence Malick","The Thin Red Line",1999},
{"1-445-3554-33","James Cameron","Avtar",2009},
{"1-800-566-346","Cameron Crow","Vanilla Sky",2001},
{"1-455-345-4345","Steven Spielberg","A.I. - Articifial Intelligenc",2001},
{"2-454-446-45","Stanley Kubick","Clockwork Orange",1972},
{"3-456-3545-565","Quentin Tarantino","Pulp Fiction",1994},
{"1-484-345-45","Oliver Stone","Natural Born Killers",1994}
};
Book *BookBubbleSort( Book *BookList)
{
Book *Blist, *Btemp = BookList, *Bprevious, *Bprevious1 = BookList;
int iIndex1, iIndex2, iTotalBooks = 0;
//determine total number of books
for (; Btemp->pNext; Btemp = Btemp->pNext)
iTotalBooks++;
for (iIndex1 = 0; iIndex1 < iTotalBooks-1; iIndex1++)
{
for (iIndex2 = 0, Blist = BookList;
Blist && Blist->pNext && (iIndex2 <= iTotalBooks-1-iIndex1);
iIndex2++)
{
if (!iIndex2)
{
//we are at beginning, so treat start book as previous book
Bprevious = Blist;
}
//compare the two books
if (strcmp(Blist->pNext->pIBSN, Blist->pIBSN) < 0)
{
//swap the books
Btemp = (Blist->pNext ? Blist->pNext->pNext :0);
if (!iIndex2 && (Bprevious == BookList))
{
//we do not have any special "marker" books
//so change beginning of the book list to point
//to the beginning alphabetical order
BookList = Blist->pNext;
}
Bprevious1 = Blist->pNext;
Bprevious->pNext = Blist->pNext;
Blist->pNext->pNext = Blist;
Blist->pNext = Btemp;
Bprevious = Bprevious1;
}
else
{
Blist = Blist->pNext;
if(iIndex2)
{
//just keep track of previous book,
//for swapping books this is required
Bprevious = Bprevious->pNext;
}
}
}
}
return BookList;
}
struct Book **CreateStructure(struct Book *pCollection, int n)
{
struct Book *NewBook ,**BookReturn = NULL;
int iIndex;
for(iIndex = 0; iIndex < n; iIndex++)
{
if((NewBook = (struct Book *) malloc(sizeof(BookEntry))) == NULL)
return NULL; // Out of memory
strcpy(NewBook->pIBSN, (*(pCollection+iIndex)).pIBSN);
strcpy(NewBook->pAuthor,(*(pCollection+iIndex)).pAuthor);
strcpy(NewBook->pTitle,(*(pCollection+iIndex)).pTitle);
NewBook->published = (*(pCollection+iIndex)).published;
NewBook->pNext = NULL; // this is the end
if (FirstBook == NULL) {
FirstBook = NewBook; // if this is the first node
LastBook = NewBook; // it's both the FirstBook and LastBook.
}
else LastBook->pNext = NewBook; // prev end points to NewBook
LastBook = NewBook; // new end of list
}
BookReturn = &FirstBook;
*BookReturn = BookBubbleSort(*BookReturn);
return BookReturn;
}
int main(void)
{
struct Book **myBooks = NULL;
myBooks = CreateStructure(Collection, 18);
for (; FirstBook != NULL; FirstBook = (**myBooks).pNext)
printf("%s %s %s %d\n", (**myBooks).pIBSN, (**myBooks).pAuthor, (**myBooks).pTitle, (**myBooks).published);
return 0;
}