Thread: Stacks

  1. #1
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    Stacks

    Code:
    #define SIZE 10
    
    struct student 
    {
        int id;
        char surname[20];
    };
    
    struct stack
    {
        struct student array[SIZE];
        int top;
    };

    from the above data structures i need to implement the following:-
    1. void initialise(struct stack *s);
    2. int full(struct stack *s);
    3. struct student *pop(struct stack *s);


    Please check if it below is correct:-
    Code:
    void initialise(struct stack *s)
    {
        s->top == -1;
    }
    
    int full(struct stack *s)
    {
        return(s->top == SIZE - 1);
    }
    
    /*im not confident if the function below is right*/
    struct student *pop(struct stack *s)
    {
        if(empty(s))
        {
            fprintf(stderr, "Stack underflow\n");
            exit(1);
         }
         return &s->array[(s->top)--];
    }

    Please advice. Thank you
    -hermit-
    Last edited by hermit; 06-16-2003 at 02:44 AM.
    - - fUnKy F3m@le - -

  2. #2
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    hmm...got abit de-railed there, that wasnt my question. I wanted to know if the pop function is ok?

    Sorry that was a typo, you are right it should be:-

    Code:
    {
     .....
         if(empty(s))
         {
            fprintf(stderr, "stack underflow\n");
            exit(1);
         }
    }
    Last edited by hermit; 06-16-2003 at 02:52 AM.
    - - fUnKy F3m@le - -

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not really, as the non-pointer version creates a copy.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    more stacks

    Lets, say that STACK is implemented as dynamic linked list. Below are the stuctures:-
    Code:
    struct student
    {
         int id;
         char surname[20];
    };
    
    struct data
    {
        struct student *stud;
        struct data *next;
    };
    
    struct stack
    {
        struct data *top;
        int size;
    };
    how would i write the following functions?

    void initialise(struct stack *s);
    struct student *stack_top(struct stack *s);
    struct student *pop(struct stack *s);
    - - fUnKy F3m@le - -

  5. #5
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213

    maybe i should take a guess?

    let me take a guess? Im abit confused with this one:-

    Code:
    void initialise(struct stack *s)
    {
          s->top == NULL;
          s->size = -1;
    }
    Really need some help here.... thank you guys
    Last edited by hermit; 06-16-2003 at 07:35 AM.
    - - fUnKy F3m@le - -

  6. #6
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    what about the other two functions?
    - - fUnKy F3m@le - -

  7. #7
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    Code:
    struct student *stack_top(struct stack *s)
    {
          if(s->top == NULL)
               return NULL;
    
         return (s->top->stud);
    }
    Please advice. THANKS
    - - fUnKy F3m@le - -

  8. #8
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    Hmm... i cant write, therefore i look for help. At least im really trying

    You dont have to write the function, just give me some hints

  9. #9
    Registered User hermit's Avatar
    Join Date
    Apr 2002
    Posts
    213
    Code:
    struct student *pop(struct stack *s)
    { 
            struct student *student;    
            struct data * data;  
      
            student = stack_top(s);  
      
           if (student == NULL)  
          {  
             ....  
          }  
           data = s->top;     
           s->top = data->next;  
           free(data);  
           return student;  
    }
    i have worked it out too
    Last edited by hermit; 06-17-2003 at 07:27 PM.
    - - fUnKy F3m@le - -

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