Thread: Stack Implementation and Exceptions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Stack Implementation and Exceptions

    I have this code:

    Code:
    typedef struct
    {
       int *data;
    
       size_t initSize; //initial size
       size_t multSize; //size multiplier
    
       size_t pointer;  //current pointer
    
    } Stack, *PStack;
    
    int createStack(PStack stack, size_t size)
    {
    
       (*stack).data = NULL;
    
       (*stack).initSize = size;
       (*stack).multSize = 0;
    
       (*stack).pointer = 0;
    
       //allocate stack for the first time
       return enlargeStack(&(*stack));
    }
    
    int enlargeStack(PStack stack)
    {
       (*stack).multSize++;
    
    
       return ((*stack).data = realloc((*stack).data,
             (*stack).initSize * (*stack).multSize * sizeof(int))) != NULL;
    }
    
    int pushStack(PStack stack, int data)
    {
       if((*stack).pointer >= ((*stack).initSize * (*stack).multSize))
          if(!enlargeStack(&(*stack)))
             return 0;
    
       (*stack).data[(*stack).pointer] = data;
    
       (*stack).pointer++;
    
       return 1;
    }
    
    int peekStack(const PStack stack, int *buffer)
    {
       if((*stack).pointer == 0)
          return 0;
    
       *buffer = (*stack).data[(*stack).pointer - 1];
    
       return 1;
    }
    
    int popStack(PStack stack, int *buffer)
    {
       if((*stack).pointer == 0)
          return 0;
    
       int temp;
    
       if(!peekStack(&(*stack), &temp))
          return 0;
    
       *buffer = temp;
    
       (*stack).pointer--;
    
       return 1;
    }
    
    void freeStack(PStack stack)
    {
       free((*stack).data);
    
       (*stack).data = NULL;
    
       (*stack).initSize = 0;
       (*stack).multSize = 0;
    
       (*stack).pointer = 0;
    }
    And...

    Code:
    int main()
    {
       Stack stack;
    
       if(!createStack(&stack, 10))
          exit(EXIT_FAILURE);
    
       int buffer;
    
       if(popStack(&stack, &buffer)) //won't work
          printf("pop: %d\n", buffer);
    
       if(peekStack(&stack, &buffer)) //won't work
          printf("peek: %d\n", buffer);
    
       pushStack(&stack, 2);
       pushStack(&stack, 3);
    
       if(peekStack(&stack, &buffer)) //works
          printf("peek: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //works
          printf("pop: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //works
          printf("pop: %d\n", buffer);
    
       if(popStack(&stack, &buffer)) //won't work
          printf("pop: %d\n", buffer);
    
       int i;
    
       for(i=0; i<100; i++)
       {
          pushStack(&stack, i);
       }
    
       for(i=0; i<100; i++)
       {
          if(popStack(&stack, &buffer)) //won't work
             printf("pop[%d]: %d\n",i, buffer);
       }
    
       if(popStack(&stack, &buffer)) //still won't work :)
          printf("pop: %d\n", buffer);
    
       freeStack(&stack);
    
       return 0;
    }
    Is there an easy way to handling exceptions in C? So we don't need to returning int from functions like the APIs...
    Last edited by audinue; 06-21-2008 at 12:33 PM. Reason: bad english :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  2. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 08:00 AM
  3. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 11:20 AM
  4. Exceptions. (This should be easy)
    By skanxalot in forum C++ Programming
    Replies: 11
    Last Post: 03-04-2003, 10:44 PM