Mail Archives: djgpp/2000/10/22/23:31:22
From: | "Graham Reeds" <grahamr AT dtn DOT ntl DOT com>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Link-List Problems
|
Lines: | 132
|
X-Priority: | 3
|
X-MSMail-Priority: | Normal
|
X-Newsreader: | Microsoft Outlook Express 5.50.4133.2400
|
X-MimeOLE: | Produced By Microsoft MimeOLE V5.50.4133.2400
|
Message-ID: | <1hOI5.7310$bL1.152326@news6-win.server.ntlworld.com>
|
Date: | Mon, 23 Oct 2000 04:30:41 +0100
|
NNTP-Posting-Host: | 62.254.80.4
|
X-Complaints-To: | abuse AT ntlworld DOT com
|
X-Trace: | news6-win.server.ntlworld.com 972271805 62.254.80.4 (Mon, 23 Oct 2000 04:30:05 BST)
|
NNTP-Posting-Date: | Mon, 23 Oct 2000 04:30:05 BST
|
Organization: | ntlworld News Service
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
I'm now working on my linked list code again, and I've hit a brick wall.
The code works insofar as it doesn't produce any warnings or errors. The
basics are:
- first iteration through: genarray() gets called to make 1000 elements in a
linked list.
- as each node is created its contents are echoed to the screen.
- as each node is added to the list, its contents are echoed to the screen
(again).
- after the list is created, it is run through to make sure it is working
right.
Unfortunately this is where the problems start. The list doesn't work -
there is no list. For some reason there is no elements or links or anything
there. For some inexplicable reason it is kaput. I have ran through it
several times, on several pieces of paper, and I cannot get it to work.
It either produces 0.0 0.00 for it's info or 1.9 2.69 and crashes with a
GPF.
Any ideas (sorry in advance for the amount of code)?
/* Function - AddNode()
** adds a node to the end of list
** if no nodes are present initialises list */
void addnode(record_t *record)
{
if (recordhead == NULL) /* empty list */
{
recordtail = recordhead = record;
printf("\t\t\tno head\n" \
"\t\t\trecord->x[0] = %5.1f\n" \
"\t\t\trecord->avgz = %5.2f\n",
record->x[0],
record->averagez);
}
else /* 1 or more nodes */
{
recordtail->next = record;
record->prev = recordtail;
record->next = NULL;
recordtail = record;
printf("\t\t\trecord->x[0] = %5.1f\n" \
"\t\t\trecord->avgz = %5.2f\n",
record->x[0],
record->averagez);
}
return;
}
/* Function (Private) - GenArray()
** generates a nonsorted linked-list of a given size */
void genarray(int numelements)
{
int i;
record_t *record;
printf("*Creating Array\n");
for (i=0; i<numelements; i++)
{
printf("\tcreating node no.:\t%i\n", i);
if (!(record = createnode())) /* create node - just a malloc()*/
{ printf("Error: Couldn't create node.\n\n"); exit(1); }
record->x[0] = i; /* just for debugging purposes... */
record->averagez = (float)(random() % 100000);
printf("\t\tnode %i record->x[0] = %5.1f\n" \
"\t\tnode %i record->avgz = %5.2f\n",
i, record->x[0],
i, record->averagez);
addnode(record); /* add node */
if (!(i % 25))
getch();
}
printf("*Finished Creating Array\n");
return;
}
/* Function - Main()
** RELEVANT PARTS ONLY
*/
int main()
{
int i, j, st, et;
FILE *fp;
record_t *recordptr;
fp = fopen("res_list.txt", "wt");
/* LOOP START */
for (i=0; i<1; i++)
{
printf("\n%s Sort:\n", sortmethods[i]);
fprintf(fp, "\n%s Sort:\n", sortmethods[i]);
sortfunction = (void *)((char *)sortfunctions[i]);
/* start sorting */
for (j=0; j<6; j++)
{
/* generate array */
genarray(arrayelements[j]);
/* DISPLAY LIST */
printf("*Displaying List\n");
recordptr = recordhead;
i = 0;
while (recordptr->next != NULL)
{
printf("\t%i:\t%5.1f\t%5.2f\n", i, recordptr->x[0],
recordptr->averagez);
if (!(i % 25))
getch();
recordptr = recordptr->next;
i ++;
}
...rest of code...
I've ran through it on paper several times - that's why there is all those
printf() statements so I can see what is going on, and what is not working.
The full source including makefile can be found at
http://homepage.dtn.ntl.com/grahamr/sortll.zip
Thanks for your time.
--
Stay Lucky, Graham "Mournblade" Reeds.
ICQ No.: 30514803
http://homepage.dtn.ntl.com/grahamr
- Raw text -