Thread: Stacks

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    46

    Question Stacks

    Hi, Im getting this error message :
    error C2129: static function 'int emptyStack(STACK *)' declared but not defined

    What im I doing wrong , here im clueless.

    Code:
    #include "header.h"
    #define EOF (-1)
    
    int  main(void)
    {
    /*Local delcarations*/
    int returned = 1;
    STACK *stack;
    void *ptr;
    void *number;
    /*Statements*/
    stack = createStack();
    printf("Enter a number");
    scanf("%d",&number);
    /*Fill stack*/
    while((number != -1) && (returned != 0)){
      returned = pushStack(stack,number);
      printf("Enter next number:<EOF> to stop");
      scanf("%d",&number);
    }
    /*Now print numbers in reverse*/
    while (!emptyStack(stack)){
      ptr = popStack(stack);
      printf("%d",ptr);
    }
    stack = destroyStack(stack);
    
    }
    Code:
    /*header.h*/
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct node
    {
      void *dataPtr;
      struct node *link;
    }STACK_NODE;
    
    typedef struct
    {
      int count;
      STACK_NODE *top;
    }STACK;
    
     STACK *createStack(void);
    int pushStack(STACK *stack,void *dataInPtr);
     void *popStack(STACK *stack);
     int emptyStack(STACK *stack);
     STACK *destroyStack(STACK *stack);
    Code:
    *createStack*/
    /*This algorithm creates an empty stack.
      Pre nothing
      Post returns pointer to a null stack.
           -or- NULL if overflow.
    */
    #include "header.h"
    
     STACK *createStack(void)
    {
     /* local declarations*/
     STACK *stack;
    
     /*statements*/
     stack = (STACK *) malloc(sizeof (STACK));
     if (stack)
     {
    	 stack->top = NULL;
    	 stack->count = 0;
     }/*if*/
     return stack;
    
    }/*createStack*/
    Code:
    #include "header.h"
    
    /*====pushStack====*/
    /*This function pushes an item onto the stack.
      Pre stack is a pointer to the stack
          dataPtr is a pointer to the data to be inserted
      Post returns 1 if success; 0 if heap overflow.
    */
    
     int pushStack(STACK *stack,void *dataInPtr)
    {
    /*local declarations*/
    STACK_NODE *newPtr;
    
    /*statements*/
    newPtr = (STACK_NODE *) malloc(sizeof(STACK_NODE));
    if(!newPtr)
      return 0;
    newPtr->dataPtr = dataInPtr;
    newPtr->link = stack->top;
    stack->top = newPtr;
    
    (stack->count)++;
    return 1;
    }/*pushStack*/
    Code:
    #include "header.h"
    /*=====popStack=====*/
    /*This function pops the item on the top of the stack.
      Pre stack is a pointer to the stack.
      Post returns pointer to user data if successfull
           NULL if underflow
    */
    
     void *popStack(STACK *stack)
    {
    /*local declarations*/
      void *dataOutPtr;
      STACK_NODE *temp;
    
    /*statements*/
      if (stack->count == 0)
    	  dataOutPtr = NULL;
      else 
      {
         temp = stack->top;
    	 dataOutPtr = stack->top->dataPtr;
    	 stack->top = stack->top->link;
    	 free(temp);
    	 (stack->count)--;
      }/*else*/
      return dataOutPtr;
    }/*popStack*/
    Code:
    #include "header.h"
    
    /*===emptyStack====*/
    /*This function determines if a stack is empty 
      Pre stack is a pointer to the stack
      Post returns 1 if empty; 0 if data are in the stack
    */
    
     int emptyStack(STACK *stack)
    {
    /*statements*/
      return (stack->count == 0);
    }/*emptyStack*/
    Code:
    #include "header.h"
    
    /*====destroyStack====*/
    /*This function releases all nodes to the heap
      Pre A stack
      Post returns null pointer
    */
    
     STACK *destroyStack(STACK *stack)
    {
    /*local declarations*/
      STACK_NODE *temp;
    /*statements*/
      if (stack)
      {
        /*Delete all nodes in stack*/
        while (stack->top != NULL)
    	{
    	/*Delete data entry*/
    	  free (stack->top->dataPtr);
    	  temp = stack->top;
    	  stack->top = stack->top->link;
    	  free(temp);
    	}/*while*/
    	/*stack now empty.Destroy stack head node.*/
    	free(stack);
      }/*if stack */
      return NULL;
    }/*destroyStack*/
    Im studying at home , correspondence, so I can't ask anyone else really.
    Thanks.
    Last edited by Cmuppet; 10-11-2004 at 02:21 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You want to drop the static keyword from your function prototypes and definitions. Unless all those code blocks are actually in the same file...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Thanks
    I did that , & now im only getting an error while debugging, when I type a zero , when asked to enter a number ?
    I'll try to figure it out myself first ,if I can't , I'll be back

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void main()
    Go read the FAQ as to why this is wrong to do. If your teacher insists on using it, use it in class. Do not use it here, because it's wrong, and you'll be reminded it is wrong every single time you use it. Eventually, it'll get beyond reminding, and become an angry mob. Trust me, noone likes an angry mob.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    I've made a few changes, but now it's stuck in an endless printloop ?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your changes.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    I did , I edited the code

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Can somebody maybe please tell me why the program is stuck in an endless printloop ?
    Thanks

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'll play with the code when I get home tonight, but as far as design goes: each function doesn't need to go into its own file. You might want to consider creating a stack.c file with all of your stack-handling functions, a stack.h file with structs and function prototypes that are involved with stack.c, and then creating a separate file for the program that uses your stack code. Then you can turn your stack code into a library that you can use over and over, but a different file for each function is really over the top.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void *number;
    Why is number a pointer? Why not make it an int?
    int number;

    Otherwise you must allocate some space for it (using malloc()).

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    Im still getting a endless printloop?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What's the latest code ???
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    i'll edit it again

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    46
    for some reason i can't edit it again.
    the only chance is void *number should be int number.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pasting a new copy is better than editing the old one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM