🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Need help with Stacks

#2094
From: Jens M Andreasen
Date: Tue, 27 Feb 1996 01:01
108 lines
2960 bytes

On 25 Feb 1996, Arlyn Chesley wrote:

> Need help with getting data onto and off of stacks
> 
First of all, change your options to _C_ (Not cpp!)

> Main program is not supposed to change.  Functions are supposed to
> work with main.  right now I always get a 2 off of the stack no matter
> what character, number or mixed string I put onto the two stacks.

If this is ever gonna work you'll _have_ to change main() since there is 
a bug placed there ... Or use the workaround below. 

#include <stdlib.h>

#define RC_OK            0    /* Operation successful*/
#define RC_OVERFLOW      1    /* Stack overflow condition encountered*/
#define RC_UNDERFLOW     2    /* Stack underflow condition encountered*/
#define RC_MALLOC_ERROR  3    /* Memory allocation error occcurred*/

/*#include "d:\advanc_c\source_c\p1.cpp"*/


/*-- your file --------------------------------------------------------*/

static struct stack{
  int tos;
  int size;
  int count;
  char *data;
  struct stack *next;
  } *stackhead;



int stack_new(int size, int count, long *key){    

  static long number = 0;
  static struct stack *tail;
  struct stack *new;

  if(!(new = malloc(sizeof(struct stack)))){ 
     return RC_MALLOC_ERROR;
   
  }else{
   
     if(!(new->data = malloc((size_t)size*count) )){ 
        free (new);
     	return RC_MALLOC_ERROR;
     }else{
        new->next=NULL;
        new->tos=-1;
        new->count=count;
        new->size=size;
     }
     *key = number ++;
     if(!stackhead){
        stackhead=tail=new;
     }else{
        tail->next=new;
     }
  }   
  return RC_OK;
}

int stack_push(long key,char *data){     

  struct stack *S=stackhead;
  
  while(key--){
     S=S->next;
  }
  
  if(S->tos++ < S->count){ 
     S->data [S->tos] =  *data;
     return RC_OK;
  }else{
     return RC_OVERFLOW;
  }
}


int stack_pop(long key,char *data){     

struct stack *S=stackhead;
  
  while(key--){
     S=S->next;
  }
  
  if(S->tos != -1){ 
     *data = S->data [S->tos--];
     return RC_OK;
  }else{
     return RC_UNDERFLOW + 42; /*  Weired bug in main!!! 
                                *             ^^^^^^^
                                *  if(rc == RC_UNDERFLOW)
                                *  should be
                                *  if(rc != RC_UNDERFLOW) */
  }
}
/* This function left as an excersize to the reader :-)) 
 */

int stack_free(long key){   /* I-Key to the stack to operate upon  */
}
Technical Details
Message-ID: <4gtl62$cic@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4gq8pl$or2@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Jens M Andreasen <jens-and@dsv.su.se>
Newsgroups: comp.lang.c.moderated
Subject: Re: Need help with Stacks
Date: 26 Feb 1996 19:01:54 -0600
Organization: Digital Solutions
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4gtl62$cic@solutions.solon.com>
References: <4gq8pl$or2@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com