Article View: comp.lang.c.moderated
Article #2174Re: Question on DATA STRUCTURES
From: Miguel Carrasquer Vidal
Date: Mon, 04 Mar 1996 16:48
Date: Mon, 04 Mar 1996 16:48
88 lines
2718 bytes
2718 bytes
titan@pegasus.odyssee.net (Molle Anthony) wrote:
>Hi it's me again, the new guy to "C".
>Here's a data structures problem. Can someone tell me why the code
>below does not work. Please be kind -- I get enough abuse from my
>wife hahahaha!!
Please compare with your original code.
================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct letter_tag
{
char element;
struct letter_tag *next;
} LETTER_TYPE;
/* is actually a list_ptr */
LETTER_TYPE *stack1_ptr=NULL;
/* is actually a list_add() */
void push(char letr);
main()
{
char letter_retrieved;
/* I added the following variables */
/* It's not really a requirement they be single letter names... */
/* I'd use `c' though, instead of `letter_retrieved' */
LETTER_TYPE *t;
int i;
printf("You are supposed to type something in now...\n");
do
{
/* you should never store the result of getchar()
in a `char': type ^Z (MS-DOS, right?) to see why... */
letter_retrieved = getchar();
push(letter_retrieved);
} while (letter_retrieved != '\n');
/* test it */
printf("\n");
for(i=0, t=stack1_ptr; t ; t=t->next, i++)
printf("element %d is `%c'\n", i, t->element);
printf("end of elements: anything beyond here is plutonium...\n");
/* aha: void main() in diguise!
I'd recommend "return 0;" */
return;
}
void push(char letr)
{
LETTER_TYPE *temp_ptr;
temp_ptr=(LETTER_TYPE*)malloc(sizeof(LETTER_TYPE));
if (temp_ptr==NULL)
{
printf("Not Enough Ram.\nErm, I mean: \"Out of swapspace\"\n");
return;
}
temp_ptr->element = letr;
/* "next" will actually point to the previous element */
/* This is the easiest way, but elements are stored in
reverse order... */
/* Real "next" pointers followable from a "list_head"
are left as an exercise for the reader... */
temp_ptr->next = stack1_ptr;
stack1_ptr = temp_ptr;
return;
}
==
Miguel Carrasquer Vidal ~ ~
Amsterdam _____________ ~ ~
mcv@pi.net |_____________|||
========================== Ce .sig n'est pas une .cig
Technical Details
Message-ID:
<4hf6si$1jj@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4hdqop$8sd@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: mcv@pi.net (Miguel Carrasquer Vidal) Newsgroups: comp.lang.c.moderated Subject: Re: Question on DATA STRUCTURES Date: 4 Mar 1996 10:48:18 -0600 Organization: Planet Internet Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4hf6si$1jj@solutions.solon.com> References: <4hdqop$8sd@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com X-Newsreader: Forte Free Agent 1.0.82