Go language Implementation Stack

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang

package stackimport  (     "errors") Type stack []interface{}func (Stack  *stack)  push (v interface{})  {    *stack = append (*Stack,  V)}func (stack *stack)  pop ()   (interface{}, error) {    if len (*stack)  == 0 {         return nil,  Errors. New ("Stack empty")     }       v :=  (* Stack) [Len (*stack)  - 1]    *stack =  (*stack) [: Len (*stack)  -  1]    return v, nil }func (Stack *stack)  Top ()   (interface{},  error)  {    if len (*stack)  == 0 {          return nil, errors. New ("Stack empty")     }       return  (*stack) [Len (*stack)  - 1], nil }func (stack *Stack)  len ()  int {    return len (*stack)}


The same idea is implemented in C:

Typedef struct my_stack_t my_stack_t;struct my_stack_t {    void Array of  **s;  /*  elements for void *  */    int use;    /*  the number of elements that have been deposited  */    int size;  /*  the number of allocations in memory  */     void  (*do_free) (void *);}; #define  DEFAULT_N 100INT STACK_INIT (my_stack_t *s, void  (*do_free) ())  {     assert (s != null);    s->s =  (void **) malloc (sizeof (void *)  * default_n);    s->use = 0;     s->size = DEFAULT_N;    s->do_free = do_free;     return 0;} Int stack_push (my_stack_t *s, void *v)  {    assert (s !=  null);     char *p;     if  (s->use == s->size)  {         p = realloc (s->s, s->size * 2);          if  (p == null)              return -1;         s->size * = 2;    }       s->s[s->use++] = v ;     return 0;} Void *stack_pop (my_stack_t *s)  {    assert (S != NULL);     if  (s->use == 0)  {         return null;    }       return s->s[--s- >use];} Void *stack_top (my_stack_t *s)  {    assert (s !=&nbsp NULL);    if  (s->use == 0)  {         return null;    }       return s- >s[s->use - 1]; }int stack_len (my_stack_t *s)  {     Return s->use;} Int stack_free (my_stack_t *s)  {    void *v;    if   (S->do_free == null)  {        return 0;     }    while  ((V = stack_pop (s))  != null)  {        s->do_free (s);    }     return 0;}


*interface{} in Golang is equivalent to void in C *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.