Thread: Stack

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Stack

    I want to implement satck in a c program ,i get the functions but they can't run properly same case for queue also anyone help?
    the function are as
    Code:
    typedef struct t_stack *stack;
    
    stack ConsStack( int max_items, int item_size );
    /* Construct a new stack
       Pre-condition: (max_items > 0) && (item_size > 0)
       Post-condition: returns a pointer to an empty stack
    */
    
    void Push( stack s, void *item );
    /* Push an item onto a stack
       Pre-condition: (s is a stack created by a call to ConsStack) &&
                      (existing item count < max_items) &&
                      (item != NULL)
       Post-condition: item has been added to the top of s
    */
    
    void *Pop( stack s );
    /* Pop an item of a stack
       Pre-condition: (s is a stack created by a call to 
                      ConsStack) &&
                      (existing item count >= 1)
       Post-condition: top item has been removed from s
    */
    AbHHinaay

  2. #2
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    stack again...

    i get the stack function correctly and make a .h file of that now i want to implement them in main program .what should i do?
    my program is :
    Code:
    #include<stdio.h>
    #include "mystack.h"
    
    void push(Stack *S, float val)
    {
        S->v[ S->top ] = val; 
       (S->top)++;    
    /*  Equivalent to: S->v[ (S->top)++ ] = val;   */
    }
    
    float pop(Stack *S)
    {
        (S->top)--;
        return (S->v[S->top]);
    /*  Equivalent to: return (S->v[--(S->top)]);  */
    }
    
    void init(Stack *S)
    {
        S->top = 0;
    }
    
    int full(Stack *S)
    {
        return (S->top >= 20);
    }
    
    void MyStackPrint(Stack *S)
    {
        int i;
        if (S->top == 0)
           printf("Stack is empty.\n");
        else
        {
           printf("Stack contents: ");
           for (i=0;i<S->top;i++)
           {
              printf("%g  ",S->v[i]); 
           }
           printf("\n");
        }
    }
    http://cboard.cprogramming.com/misc....bbcode#buttons
    Fixed by Salem
    AbHHinaay

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do you mean you want to use it in the main program? Ok, so stick it in there and use it. I'm not sure what the question is. You made a stack, assuming it works, where's the problem? Whenever you need a stack, throw it in, create and instance, and call the functions at the needed times.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM