Thread: Stacks

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    41

    Stacks

    I need to write function retrieve to return the value at the top of the stack. The stack is not changed. Any suggestions.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char stack_element_t;
    
    typedef struct stack_node_s
    {
    	stack_element_t element;
    	struct stack_node_s *restp;
    }stack_node_t;
    
    typedef struct 
    {
    	stack_node_t *topp;
    }stack_t;
    
    
    void push (stack_t *sp, stack_element_t c);
    stack_element_t pop(stack_t *sp);
    int retrieve (stack_t s);
    
    int main (void)
    {
    	stack_t s = {NULL};
    
    	push(&s, '2');
    	push(&s, '3');
    	push(&s, '4');
    
    	printf("\nEmptying stack: \n");
    	while (s.topp !=NULL)
    	{ 
    		printf("%c\n", pop(&s));
    		printf("%c\n", top);
    	}
    
    return (0);
    }
    
    void push(stack_t *sp, stack_element_t c)
    {
    	stack_node_t *newp;
    
    	newp = (stack_node_t *) malloc (sizeof (stack_node_t));
    	newp->element = c;
    	newp->restp = sp->topp;
    	sp->topp = newp;
    }
    
    
    stack_element_t pop(stack_t *sp)
    {
    	stack_node_t *to_freep;
    	stack_element_t ans;
    	
    	to_freep = sp->topp;
    	ans = to_freep->element;
    	sp->topp = to_freep->restp;
    	free(to_freep);
    
    	return (ans);
    }
    
    
    int retieve(stack_t s)
    {
    	stack_t element_t top;
    	return (top);
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int retieve(stack_t s)
    {
    	stack_t element_t top;
    	return (top);
    }
    if yuo have written pop I do not understand your prolems with retrieve... Are you sure it is your code?

    Code:
    stack_element_t retieve(stack_t s)
    {
    	stack_element_t top = s.topp->element;
    	return (top);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    the program works minus retrieve function. i cant figure out how to write the retrieve function to do wat i want it to do

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Ok I figured that out. now i need to scan in a interger expression in postfix form and display its result. push each interger on the stsck. when an operand is encountered the top two operands are popped and the operation preformed. result is pushed back onto the stack. the final result should is the value remaning at the end when the expression is reached.

    I know how the postfix works but how do you write what to do when it encounters an operand?

    Any suggestions.

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