Richard said:
Phred Phungus wrote:
I think the classification for what is a singly-linked list must be
fairly broad. In this, there aren't new nodes created dynamically.
['this' is an illustration of the linked-list concept that uses an array
as the container]
Right. The concept of "linked list" depends purely on each item having a
link to some other item (or items) in the list so that a logical linear
progression exists. There's nothing in the rules that says the links
have to be pointers, or that the list has to be capable of arbitrary
expansion, or anything else. As far as the *concept* is concerned,
that's fluff. Highly important and significant fluff from a practical
point of view, but fluff nonetheless from the perspective of what
actually constitutes a linked list.
You have probably already noted that, in your source text, the author of
your example points out that the array implementation is "naive", and
shortly moves on to present pointer-based lists.
That's what I was thinking as I fell asleep last night, having failed on
most things. The reason I wasn't finding it was that the driver was in
a different place, but that place is covered on p 381.
First of all, I'd like to apologize to Alan for reposting code he didn't
want to see again. Alan is both helpful and knowledgable. I think I
have the C program I was looking for with this thread, which appears on
the source disc for unleashed:
dan@dan-desktop:~/source/unleashed/ch11$ gcc ll1.o -D_GNU_SOURCE -Wall
-Wextra sl2.c -o out
dan@dan-desktop:~/source/unleashed/ch11$ ./out
Read UNIX Unleashed, by Burk and Horvath
Read Builder Unleashed, by Calvert
Read C: How to Program, by Deitel & Deitel
Read DOS Programmers Reference, by Dettmann & Johnson
Read C : A Reference Manual, by Harbison & Steele
Read C Unleashed, by Heathfield & Kirby
Read Linux Unleashed, by Husain and Parker
Read C Programming Language, by Kernighan & Ritchie
Read Data Structures & Algorithms, by Lafore
Read C++ Unleashed, by Liberty
Read The Standard C Library, by Plauger
Read Teach Yourself BCB, by Reisdorph
Read Algorithms in C, by Sedgewick
Read C++ Programming Language, by Stroustrup
Read C Programming FAQs, by Summit
Read Expert C Programming, by van der Linden
Removing title C++ Unleashed
Read UNIX Unleashed, by Burk and Horvath
Read Builder Unleashed, by Calvert
Read C: How to Program, by Deitel & Deitel
Read DOS Programmers Reference, by Dettmann & Johnson
Read C : A Reference Manual, by Harbison & Steele
Read C Unleashed, by Heathfield & Kirby
Read Linux Unleashed, by Husain and Parker
Read C Programming Language, by Kernighan & Ritchie
Read Data Structures & Algorithms, by Lafore
Read The Standard C Library, by Plauger
Read Teach Yourself BCB, by Reisdorph
Read Algorithms in C, by Sedgewick
Read C++ Programming Language, by Stroustrup
Read C Programming FAQs, by Summit
Read Expert C Programming, by van der Linden
dan@dan-desktop:~/source/unleashed/ch11$ cat sl2.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "sllist.h"
typedef struct BOOK
{
char Title[30];
char Author[30];
} BOOK;
typedef struct FIELD_INFO
{
int TitleWidth;
int AuthWidth;
} FIELD_INFO;
int PrintBook(int Tag, void *Memory, void *Args)
{
BOOK *b = Memory;
FIELD_INFO *f = Args;
assert(Tag == 0);
printf("Read %*s, by %*s\n",
f->TitleWidth,
b->Title,
f->AuthWidth,
b->Author);
return 0;
}
int main(void)
{
BOOK Book[] =
{
{"Expert C Programming", "van der Linden"},
{"C Programming FAQs", "Summit"},
{"C++ Programming Language", "Stroustrup"},
{"Algorithms in C", "Sedgewick"},
{"Teach Yourself BCB", "Reisdorph"},
{"The Standard C Library", "Plauger"},
{"C++ Unleashed", "Liberty"},
{"Data Structures & Algorithms", "Lafore"},
{"C Programming Language", "Kernighan & Ritchie"},
{"Linux Unleashed", "Husain and Parker"},
{"C Unleashed", "Heathfield & Kirby"},
{"C : A Reference Manual", "Harbison & Steele"},
{"DOS Programmers Reference", "Dettmann & Johnson"},
{"C: How to Program", "Deitel & Deitel"},
{"Builder Unleashed", "Calvert"},
{"UNIX Unleashed", "Burk and Horvath"}
};
SLLIST *List = NULL;
SLLIST *Removed = NULL;
BOOK *Data;
FIELD_INFO FldInfo = { 30, 30};
size_t NumBooks = sizeof Book / sizeof Book[0];
size_t i;
/* Populate the list */
for(i = 0; i < NumBooks; i++)
{
if(SL_SUCCESS !=
SLFront(&List, 0, Book + i, sizeof(BOOK)))
{
puts("Couldn't allocate enough memory.");
SLDestroy(&List);
exit(EXIT_FAILURE);
}
}
/* Print the list */
SLWalk(List, PrintBook, &FldInfo);
/* Remove one item */
Removed = List;
for(i = 0; i < NumBooks / 2; i++)
{
Removed = Removed->Next;
}
Data = SLGetData(Removed->Next, NULL, NULL);
printf("\nRemoving title %s\n\n", Data->Title);
SLDeleteNext(Removed);
/* Print the list again to confirm deletion */
SLWalk(List, PrintBook, &FldInfo);
/* Destroy the list */
SLDestroy(&List);
return 0;
}
// gcc ll1.o -D_GNU_SOURCE -Wall -Wextra sl2.c -o out
dan@dan-desktop:~/source/unleashed/ch11$ cat sllist.h
/* sllist.h - header for single linked list lib
*
* SLLIST - Single-Linked List Library
*
* Copyright (C) 2000 Richard Heathfield
* Eton Computer Systems Ltd
* Macmillan Computer Publishing
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 675 Mass Ave,
* Cambridge, MA 02139, USA.
*
* Richard Heathfield may be contacted by email at:
* (e-mail address removed)
*
*/
#ifndef SLLIST_H__
#define SLLIST_H__
#define SL_SUCCESS 0
#define SL_NO_MEM 1
#define SL_ZERO_SIZE 2
typedef struct SLLIST
{
int Tag;
struct SLLIST *Next;
void *Object;
size_t Size;
} SLLIST;
/* Add new item immediately after current item */
int SLAdd(SLLIST **Item,
int Tag,
void *Object,
size_t Size);
/* Add item to front of list. Care: if you pass
* this function any node other than the first,
* you will get Y-branches in your list:
*
* oldroot-->-
* \
* >->-passeditem-->-->--oldnext
* /
* newitem-->-
*
* This would be a Bad Thing.
*/
int SLFront(SLLIST **Item,
int Tag,
void *Object,
size_t Size);
/* Add new item right at the end of the list */
int SLAppend(SLLIST **Item,
int Tag,
void *Object,
size_t Size);
/* Replace existing data */
int SLUpdate(SLLIST *Item,
int NewTag,
void *NewObject,
size_t NewSize);
/* Retrieve data from this node */
void *SLGetData(SLLIST *Item,
int *Tag,
size_t *Size);
/* Delete this item. Returns pointer to
* next item - caller's responsibility
* to maintain list integrity.
*/
SLLIST *SLDeleteThis(SLLIST *Item);
/* Delete item immediately following
* the one passed in. List integrity
* maintained automatically.
*/
void SLDeleteNext(SLLIST *Item);
/* Destroy the entire list */
void SLDestroy(SLLIST **List);
/* Call func(Tag, ThisItem, Args) for each item */
int SLWalk(SLLIST *List,
int(*Func)(int, void *, void *),
void *Args);
#endif
dan@dan-desktop:~/source/unleashed/ch11$
With a listing like this, I think one needs to the the header, so that
you know how the functions are declared, but the definitions are just a
mile of code at this point, given that compile, link, and test.