Thread: Stacks

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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