Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #2176
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail |
|---|---|
| NNTP-Posting-Date | Thu, 06 Sep 2012 19:52:16 -0500 |
| Message-ID | <50494540.4AA5@mindspring.com> (permalink) |
| Date | Thu, 06 Sep 2012 20:52:16 -0400 |
| From | pete <pfiland@mindspring.com> |
| Reply-To | pfiland@mindspring.com |
| Organization | PF |
| X-Mailer | Mozilla 3.04Gold (WinNT; I) |
| MIME-Version | 1.0 |
| Newsgroups | comp.programming |
| Subject | Re: linked list |
| References | <k2b694$et0$1@speranza.aioe.org> <50492222.5F6D@mindspring.com> <k2b82r$j0u$1@speranza.aioe.org> |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| Lines | 154 |
| X-Usenet-Provider | http://www.giganews.com |
| NNTP-Posting-Host | 4.154.217.228 |
| X-Trace | sv3-KphdIt8Z0NHGrDAU2dOMkYP1ZF1Js9dQ05oDKpkkhyX9csGx/fOE6vkWfKeAz/dEH40KL3uNzMleDLn!AGYAKzzjewsDE2wH8+0l2XSuyy5t/DPingUi/oTaMH8LXGjkeMxG4mvLbux320vzvphfdr1Gbh2F!xAzjsC7JJ0E= |
| X-Abuse-and-DMCA-Info | Please be sure to forward a copy of ALL headers |
| X-Abuse-and-DMCA-Info | Otherwise we will be unable to process your complaint properly |
| X-Postfilter | 1.3.40 |
| X-Original-Bytes | 4294 |
| Xref | csiph.com comp.programming:2176 |
Show key headers only | View raw
Bill Cunningham wrote:
>
> pete wrote:
> >
> > /* BEGIN d_append.c */
> > /*
> > ** Demonstration of use of int list functions.
> > */
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > #define NUMBERS 15,14,13,7,20,9,8,12,11,6
> >
> > #define NMEMB(A) (sizeof (A) / sizeof *(A))
> >
> > struct d_node {
> > struct d_node *next;
> > int data;
> > };
> >
> > typedef struct d_node d_type;
>
> Wow if this is simple I'd hate to see complex.
> Whew. One step at a time
> I guess.
>
> > int d_fprintf(const d_type *node, FILE *stream);
> > d_type *d_append(d_type **head, d_type *tail, int data);
>
> I've never worked with pointers to pointers.
> Except in the command line.
> What's head and tail for?
head points to the first node of the linked list.
tail points to the last node of the linked list.
>I only used next.
What can you do with a linked list,
by only using next?
I simplified it:
/* BEGIN d_start.c */
/*
** Demonstration of use of int list functions.
** d_start
** d_append_0
*/
#include <stdio.h>
#include <stdlib.h>
#define NUMBERS 15,14,13,7,20,9,8,12,11,6
#define NMEMB(A) (sizeof (A) / sizeof *(A))
struct d_node {
struct d_node *next;
int data;
};
int d_fprintf(const struct d_node *node, FILE *stream);
struct d_node *d_start(int data);
struct d_node *d_append_0(struct d_node *tail, int data);
void d_free(struct d_node *node);
int
main(void)
{
struct d_node *head;
struct d_node *tail;
int numbers[] = {NUMBERS};
int *ptr = numbers;
int *const after = numbers + NMEMB(numbers);
puts("/* BEGIN d_start.c output */");
puts("\nOriginal order of list of ints:");
head = tail = d_start(*ptr);
if (head != NULL) {
while (++ptr != after) {
tail = d_append_0(tail, *ptr);
if (tail == NULL) {
puts("malloc trouble!");
break;
}
}
} else {
puts("malloc trouble!");
}
d_fprintf(head, stdout);
d_free(head);
puts("\n/* END d_start.c output */");
return 0;
}
int
d_fprintf(const struct d_node *node, FILE *stream)
{
int rc = 0;
while (node != NULL) {
if (0 > (rc = fprintf(stream, "%d\n", node -> data))) {
break;
}
node = node -> next;
}
return rc;
}
struct d_node *
d_start(int data)
{
struct d_node *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = data;
}
return node;
}
struct d_node *
d_append_0(struct d_node *tail, int data)
{
struct d_node *node;
node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = data;
tail -> next = node;
}
return node;
}
void
d_free(struct d_node *node)
{
struct d_node *next_node;
while (node != NULL) {
next_node = node -> next;
free(node);
node = next_node;
}
}
/* END d_start.c */
--
pete
Back to comp.programming | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 17:58 -0400
Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 18:04 -0400
Re: linked list pete <pfiland@mindspring.com> - 2012-09-06 18:22 -0400
Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-06 18:28 -0400
Re: linked list pete <pfiland@mindspring.com> - 2012-09-06 20:52 -0400
Re: linked list "Bill Cunningham" <nospam@nspam.invalid> - 2012-09-07 17:25 -0400
Re: linked list pete <pfiland@mindspring.com> - 2012-09-08 09:35 -0400
Re: linked list pete <pfiland@mindspring.com> - 2012-09-08 14:49 -0400
Re: linked list Pascal J. Bourguignon <pjb@informatimago.com> - 2012-10-06 01:32 +0000
csiph-web