🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2177

Re: Question on DATA STRUCTURES

#2177
From: walter briscoe
Date: Tue, 05 Mar 1996 03:24
84 lines
4050 bytes
In article <4hdqop$8sd@solutions.solon.com>
           titan@pegasus.odyssee.net "Molle Anthony" writes:

> 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!!

I find the gratuitous gender stereotyping offensive.  Please take care
to avoid such irrelevances.  I am unable to say why it does not work
because you have omitted to say what it is supposed to do.  I have added
comments to your code which I hope are helpful.  It might be reasonable
to design a program to read a line of text, form it into a queue of
letters implemented as a linked list and output the line that was input.
In most cases programs have inputs, processing and outputs.

#include <stdio.h>
#include <conio.h> /* You don't use anything from this file. */
#include <alloc.h> /* I assume you use this to get malloc. More portable
                   ** to use stdlib.h */
#define VERS 1.0   /* You don't use this. Put a prologue in the file
                   ** giving the purpose and history of the file */

int stack1_top; /* It is helpful to comment the purpose of every entity
                ** or set of entities  in your program for the benefit
                ** of any new reader - including yourself in a month */
int stack2_top;
char letter_retrieved;
int *stack1_ptr = &stack1_top; /* You make no real use of this datum */
int *stack2_ptr = &stack2_top; /* You do not write to this datum */
void push(char letr);


typedef struct letter_tag
{
   char element;
   struct letter_tag *next;
} LETTER_TYPE; /* I have found that list pointers are better placed
               ** at the beginning of structs than the end. */

main() /* main is implicitly int here and you say nothing about its
       ** arguments. Better to use int main ( void ) which complies with
       the standard, section 5.1.2.2.1 */
{
   do
   {
      letter_retrieved= getchar();
      /* You have a problem here. getchar returns either a char or
      ** EOF. EOF does not fit in a char variable. */
      push(letter_retrieved);
   } while ( letter_retrieved != '\n' ) ;

   /* I dislike do { } while loops. The control is at the wrong end. I
   ** prefer while getchar result not '\n' nor EOF at top of loop */
   return; /* You need to supply a value. return 0 indicates success */
}


void push(char letr)
{
   LETTER_TYPE *temp_ptr;
   temp_ptr=(LETTER_TYPE*)malloc(sizeof(LETTER_TYPE));
   /* In C, (LETTER_TYPE*) is redundant.  Where is the free() calls? */
   if (temp_ptr==NULL)             /* GOOD!! You lnow malloc can fail */
   {
      printf("\nNot Enough Ram."); /* Usual to write errors to stderr */
      return;                      /* Mild response, I prefer abort() */
   }
   temp_ptr->element = letr;
   temp_ptr->next = temp_ptr; /* I took out the extra * in
                              ** temp_ptr->*next = temp_ptr; */
   stack1_ptr = temp_ptr;     /* This is a type mismatch */
   stack1_ptr++;              /* void of any obvious purpose. It
                              ** results in setting stack1_ptr to point
                              ** at an integer whose address is offset by
                              ** one from the data area pointed at by
                              ** temp_ptr */

   return;
}

--
walter briscoe
Technical Details
Message-ID: <4hgc4i$98q@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4hdqop$8sd@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: walter briscoe <walter@wbriscoe.demon.co.uk>
Newsgroups: comp.lang.c.moderated
Subject: Re: Question on DATA STRUCTURES
Date: 4 Mar 1996 21:24:02 -0600
Organization: Digital Solutions
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4hgc4i$98q@solutions.solon.com>
References: <4hdqop$8sd@solutions.solon.com>
Reply-To: walter@wbriscoe.demon.co.uk
NNTP-Posting-Host: solutions.solon.com
X-NNTP-Posting-Host: wbriscoe.demon.co.uk
X-Newsreader: Demon Internet Simple News v1.30
X-Mail2News-Path: wbriscoe.demon.co.uk