Stack implementation with array












0















I am trying to implement stack with array in C. But I guess my push function is not correct.(Maybe there are some other mistakes) Because when I run the code, it prints "Stack is empty!" two times.



How can I solve this problem and is this implementation logic is true?



#include <stdio.h>

#define SIZE 10

typedef struct stack
{
int top;
int items[SIZE];
}stack;

void push(int a, stack st)
{
if((st.top + 1) != SIZE)
{
st.top++;
st.items[st.top] = a;
}
else
{
printf("nStack is full!");
}
}

void pop(stack st)
{
if(st.top != -1)
{
st.top--;
}
else
{
printf("nStack is empty!");
}
}

void printList(stack st)
{
int i;
for(i = 0; i < st.top + 1; i++)
{
printf("%d -> ", st.items[i]);
}
puts("");
}

int main(void)
{
stack stack1;
stack1.top = -1;

stack stack2;
stack2.top = -1;

push(3, stack1);
push(5, stack1);
push(7, stack1);

printList(stack1);

pop(stack1);
printList(stack1);

pop(stack1);
printList(stack1);
}









share|improve this question


















  • 5





    There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

    – Some programmer dude
    Nov 16 '18 at 0:20






  • 1





    Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

    – Jonathan Leffler
    Nov 16 '18 at 0:27






  • 2





    Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

    – Craig Estey
    Nov 16 '18 at 0:34


















0















I am trying to implement stack with array in C. But I guess my push function is not correct.(Maybe there are some other mistakes) Because when I run the code, it prints "Stack is empty!" two times.



How can I solve this problem and is this implementation logic is true?



#include <stdio.h>

#define SIZE 10

typedef struct stack
{
int top;
int items[SIZE];
}stack;

void push(int a, stack st)
{
if((st.top + 1) != SIZE)
{
st.top++;
st.items[st.top] = a;
}
else
{
printf("nStack is full!");
}
}

void pop(stack st)
{
if(st.top != -1)
{
st.top--;
}
else
{
printf("nStack is empty!");
}
}

void printList(stack st)
{
int i;
for(i = 0; i < st.top + 1; i++)
{
printf("%d -> ", st.items[i]);
}
puts("");
}

int main(void)
{
stack stack1;
stack1.top = -1;

stack stack2;
stack2.top = -1;

push(3, stack1);
push(5, stack1);
push(7, stack1);

printList(stack1);

pop(stack1);
printList(stack1);

pop(stack1);
printList(stack1);
}









share|improve this question


















  • 5





    There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

    – Some programmer dude
    Nov 16 '18 at 0:20






  • 1





    Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

    – Jonathan Leffler
    Nov 16 '18 at 0:27






  • 2





    Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

    – Craig Estey
    Nov 16 '18 at 0:34
















0












0








0








I am trying to implement stack with array in C. But I guess my push function is not correct.(Maybe there are some other mistakes) Because when I run the code, it prints "Stack is empty!" two times.



How can I solve this problem and is this implementation logic is true?



#include <stdio.h>

#define SIZE 10

typedef struct stack
{
int top;
int items[SIZE];
}stack;

void push(int a, stack st)
{
if((st.top + 1) != SIZE)
{
st.top++;
st.items[st.top] = a;
}
else
{
printf("nStack is full!");
}
}

void pop(stack st)
{
if(st.top != -1)
{
st.top--;
}
else
{
printf("nStack is empty!");
}
}

void printList(stack st)
{
int i;
for(i = 0; i < st.top + 1; i++)
{
printf("%d -> ", st.items[i]);
}
puts("");
}

int main(void)
{
stack stack1;
stack1.top = -1;

stack stack2;
stack2.top = -1;

push(3, stack1);
push(5, stack1);
push(7, stack1);

printList(stack1);

pop(stack1);
printList(stack1);

pop(stack1);
printList(stack1);
}









share|improve this question














I am trying to implement stack with array in C. But I guess my push function is not correct.(Maybe there are some other mistakes) Because when I run the code, it prints "Stack is empty!" two times.



How can I solve this problem and is this implementation logic is true?



#include <stdio.h>

#define SIZE 10

typedef struct stack
{
int top;
int items[SIZE];
}stack;

void push(int a, stack st)
{
if((st.top + 1) != SIZE)
{
st.top++;
st.items[st.top] = a;
}
else
{
printf("nStack is full!");
}
}

void pop(stack st)
{
if(st.top != -1)
{
st.top--;
}
else
{
printf("nStack is empty!");
}
}

void printList(stack st)
{
int i;
for(i = 0; i < st.top + 1; i++)
{
printf("%d -> ", st.items[i]);
}
puts("");
}

int main(void)
{
stack stack1;
stack1.top = -1;

stack stack2;
stack2.top = -1;

push(3, stack1);
push(5, stack1);
push(7, stack1);

printList(stack1);

pop(stack1);
printList(stack1);

pop(stack1);
printList(stack1);
}






c data-structures stack






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 0:17









H.OzerH.Ozer

495




495








  • 5





    There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

    – Some programmer dude
    Nov 16 '18 at 0:20






  • 1





    Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

    – Jonathan Leffler
    Nov 16 '18 at 0:27






  • 2





    Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

    – Craig Estey
    Nov 16 '18 at 0:34
















  • 5





    There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

    – Some programmer dude
    Nov 16 '18 at 0:20






  • 1





    Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

    – Jonathan Leffler
    Nov 16 '18 at 0:27






  • 2





    Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

    – Craig Estey
    Nov 16 '18 at 0:34










5




5





There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

– Some programmer dude
Nov 16 '18 at 0:20





There must be thousands of duplicates of this. All will tell you that C passes arguments by value, meaning they are copied into the functions argument variables. Modifying a copy will not modify the original. That's why you will see passing structures in other code being passed as pointers (which is used to emulate pass by reference).

– Some programmer dude
Nov 16 '18 at 0:20




1




1





Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

– Jonathan Leffler
Nov 16 '18 at 0:27





Please end messages with newlines; you don't normally need newlines at the start. Output may not appear until there is a newline, so newline at the beginning can lead to confusion.

– Jonathan Leffler
Nov 16 '18 at 0:27




2




2





Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

– Craig Estey
Nov 16 '18 at 0:34







Change all functions to accept stack_t *st instead of stack_t st and change (e.g.) st.top into st->top in them. In main, change all calls to stack functions to be (e.g.) push(3, &stack1) This is because of the pass-by-value [which you don't want] to [an emulation of] pass-by-reference [which you do want].

– Craig Estey
Nov 16 '18 at 0:34














1 Answer
1






active

oldest

votes


















1














Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.



On gdb you can see



In main
gdb) p &stack1
$4 = (stack *) 0x7fffffffddf0



In push fn



(gdb) p &st
$3 = (stack *) 0x7fffffffdd90



both are different.



correct code is given below.



#include <stdio.h>

#define SIZE 10

typedef struct stack
{
int top;
int items[SIZE];
}stack;

void push(int a, stack *st)
{
if((st->top + 1) != SIZE)
{
st->top++;
st->items[st->top] = a;
}
else
{
printf("nStack is full!");
}
}

void pop(stack *st)
{
if(st->top != -1)
{
st->top--;
}
else
{
printf("nStack is empty!");
}
}

void printList(stack *st)
{
int i;
for(i = 0; i < st->top + 1; i++)
{
printf("%d -> ", st->items[i]);
}
puts("");
}

int main(void)
{
stack stack1;
stack1.top = -1;

stack stack2;
stack2.top = -1;

push(3, &stack1);
push(5, &stack1);
push(7, &stack1);

printList(&stack1);

pop(&stack1);
printList(&stack1);

pop(&stack1);
printList(&stack1);
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329675%2fstack-implementation-with-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.



    On gdb you can see



    In main
    gdb) p &stack1
    $4 = (stack *) 0x7fffffffddf0



    In push fn



    (gdb) p &st
    $3 = (stack *) 0x7fffffffdd90



    both are different.



    correct code is given below.



    #include <stdio.h>

    #define SIZE 10

    typedef struct stack
    {
    int top;
    int items[SIZE];
    }stack;

    void push(int a, stack *st)
    {
    if((st->top + 1) != SIZE)
    {
    st->top++;
    st->items[st->top] = a;
    }
    else
    {
    printf("nStack is full!");
    }
    }

    void pop(stack *st)
    {
    if(st->top != -1)
    {
    st->top--;
    }
    else
    {
    printf("nStack is empty!");
    }
    }

    void printList(stack *st)
    {
    int i;
    for(i = 0; i < st->top + 1; i++)
    {
    printf("%d -> ", st->items[i]);
    }
    puts("");
    }

    int main(void)
    {
    stack stack1;
    stack1.top = -1;

    stack stack2;
    stack2.top = -1;

    push(3, &stack1);
    push(5, &stack1);
    push(7, &stack1);

    printList(&stack1);

    pop(&stack1);
    printList(&stack1);

    pop(&stack1);
    printList(&stack1);
    }





    share|improve this answer




























      1














      Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.



      On gdb you can see



      In main
      gdb) p &stack1
      $4 = (stack *) 0x7fffffffddf0



      In push fn



      (gdb) p &st
      $3 = (stack *) 0x7fffffffdd90



      both are different.



      correct code is given below.



      #include <stdio.h>

      #define SIZE 10

      typedef struct stack
      {
      int top;
      int items[SIZE];
      }stack;

      void push(int a, stack *st)
      {
      if((st->top + 1) != SIZE)
      {
      st->top++;
      st->items[st->top] = a;
      }
      else
      {
      printf("nStack is full!");
      }
      }

      void pop(stack *st)
      {
      if(st->top != -1)
      {
      st->top--;
      }
      else
      {
      printf("nStack is empty!");
      }
      }

      void printList(stack *st)
      {
      int i;
      for(i = 0; i < st->top + 1; i++)
      {
      printf("%d -> ", st->items[i]);
      }
      puts("");
      }

      int main(void)
      {
      stack stack1;
      stack1.top = -1;

      stack stack2;
      stack2.top = -1;

      push(3, &stack1);
      push(5, &stack1);
      push(7, &stack1);

      printList(&stack1);

      pop(&stack1);
      printList(&stack1);

      pop(&stack1);
      printList(&stack1);
      }





      share|improve this answer


























        1












        1








        1







        Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.



        On gdb you can see



        In main
        gdb) p &stack1
        $4 = (stack *) 0x7fffffffddf0



        In push fn



        (gdb) p &st
        $3 = (stack *) 0x7fffffffdd90



        both are different.



        correct code is given below.



        #include <stdio.h>

        #define SIZE 10

        typedef struct stack
        {
        int top;
        int items[SIZE];
        }stack;

        void push(int a, stack *st)
        {
        if((st->top + 1) != SIZE)
        {
        st->top++;
        st->items[st->top] = a;
        }
        else
        {
        printf("nStack is full!");
        }
        }

        void pop(stack *st)
        {
        if(st->top != -1)
        {
        st->top--;
        }
        else
        {
        printf("nStack is empty!");
        }
        }

        void printList(stack *st)
        {
        int i;
        for(i = 0; i < st->top + 1; i++)
        {
        printf("%d -> ", st->items[i]);
        }
        puts("");
        }

        int main(void)
        {
        stack stack1;
        stack1.top = -1;

        stack stack2;
        stack2.top = -1;

        push(3, &stack1);
        push(5, &stack1);
        push(7, &stack1);

        printList(&stack1);

        pop(&stack1);
        printList(&stack1);

        pop(&stack1);
        printList(&stack1);
        }





        share|improve this answer













        Hi your stack implementation is wrong.Using gdb you can verify this.You are passing structure as value you should pass as address.



        On gdb you can see



        In main
        gdb) p &stack1
        $4 = (stack *) 0x7fffffffddf0



        In push fn



        (gdb) p &st
        $3 = (stack *) 0x7fffffffdd90



        both are different.



        correct code is given below.



        #include <stdio.h>

        #define SIZE 10

        typedef struct stack
        {
        int top;
        int items[SIZE];
        }stack;

        void push(int a, stack *st)
        {
        if((st->top + 1) != SIZE)
        {
        st->top++;
        st->items[st->top] = a;
        }
        else
        {
        printf("nStack is full!");
        }
        }

        void pop(stack *st)
        {
        if(st->top != -1)
        {
        st->top--;
        }
        else
        {
        printf("nStack is empty!");
        }
        }

        void printList(stack *st)
        {
        int i;
        for(i = 0; i < st->top + 1; i++)
        {
        printf("%d -> ", st->items[i]);
        }
        puts("");
        }

        int main(void)
        {
        stack stack1;
        stack1.top = -1;

        stack stack2;
        stack2.top = -1;

        push(3, &stack1);
        push(5, &stack1);
        push(7, &stack1);

        printList(&stack1);

        pop(&stack1);
        printList(&stack1);

        pop(&stack1);
        printList(&stack1);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 5:50









        coddygeekcoddygeek

        609




        609
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329675%2fstack-implementation-with-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma