Data Structure in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • # include

    #1

    Data Structure in C

    you can construct stack in easy way in C as following
    #include<stdio. h>
    # define MaxStack 10
    int top=0;
    void clearstack(int stack[MaxStack]);
    int emptystack(int stack[MaxStack]);
    int fullstack(int stack[MaxStack]);
    void pushstack(int stack[MaxStack],int newelements);
    void popstack(int stack[MaxStack],int *element);

    void main()
    {
    int x,i,stack[MaxStack];
    clearstack(stac k);
    for(i=0;i<MaxSt ack;i++)
    {scanf("%d",&x) ;
    pushstack(stack ,x);
    }
    for(i=0;i<MaxSt ack;i++)
    {
    apopstack(stack ,&x);
    printf("%d\n",x );
    }

    }
    void clearstack(int stack[MaxStack])
    {
    stack[top]=0;
    return;
    }
    int emptystack(int stack[MaxStack])
    {return (stack[top]==0);
    }
    int fullstack(int stack[MaxStack])
    {
    return (stack[top]==MaxStack)
    ;
    }

    void pushstack(int stack[MaxStack],int newelement)
    {
    stack[top]=stack[top]+1;
    stack[stack[top]]=newelement;
    }
    popstack(int stack[MaxStack],int *element)
    {
    *element =stack[stack[top]];
    stack [top]=stack[top]-1;
    }

  • Richard Heathfield

    #2
    Re: Data Structure in C

    # include said:
    [color=blue]
    > you can construct stack in easy way in C as following
    > #include<stdio. h>
    > # define MaxStack 10
    > int top=0;
    > void clearstack(int stack[MaxStack]);
    > int emptystack(int stack[MaxStack]);
    > int fullstack(int stack[MaxStack]);
    > void pushstack(int stack[MaxStack],int newelements);
    > void popstack(int stack[MaxStack],int *element);
    >
    > void main()[/color]

    At this point, you reveal your ignorance of C. I see little point in looking
    through the rest of your code. You need a better C book.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Jaspreet

      #3
      Re: Data Structure in C

      # include wrote:[color=blue]
      > you can construct stack in easy way in C as following
      > #include<stdio. h>
      > # define MaxStack 10
      > int top=0;
      > void clearstack(int stack[MaxStack]);
      > int emptystack(int stack[MaxStack]);
      > int fullstack(int stack[MaxStack]);
      > void pushstack(int stack[MaxStack],int newelements);
      > void popstack(int stack[MaxStack],int *element);
      >
      > void main()
      > {
      > int x,i,stack[MaxStack];
      > clearstack(stac k);
      > for(i=0;i<MaxSt ack;i++)
      > {scanf("%d",&x) ;
      > pushstack(stack ,x);
      > }[/color]

      <snip code snippet>

      Why have you pasted this program ? Is there something wrong in it that
      you want someone to help you or you just want a confirmation from us ?

      BTW, main() **should** only return int. Your program may compile but
      it is bound to give undefined behavior since your main is returning
      void.

      Thats the reason for the post by Richard.

      Comment

      • Keith Thompson

        #4
        Re: Data Structure in C

        "# include" <wam_alqudsi@ya hoo.com> writes:[color=blue]
        > you can construct stack in easy way in C as following
        > #include<stdio. h>
        > # define MaxStack 10[/color]

        It's conventional (but not required) to use all-caps for macro names:

        #define MAXSTACK 10
        [color=blue]
        > int top=0;
        > void clearstack(int stack[MaxStack]);
        > int emptystack(int stack[MaxStack]);
        > int fullstack(int stack[MaxStack]);
        > void pushstack(int stack[MaxStack],int newelements);
        > void popstack(int stack[MaxStack],int *element);[/color]

        MaxStack in each of these function declarations is silently ignored.
        The first argument of each of these functions is really an int*.

        [color=blue]
        > void main()[/color]

        No, main() returns int.
        [color=blue]
        > {
        > int x,i,stack[MaxStack];
        > clearstack(stac k);
        > for(i=0;i<MaxSt ack;i++)
        > {scanf("%d",&x) ;
        > pushstack(stack ,x);
        > }
        > for(i=0;i<MaxSt ack;i++)
        > {
        > apopstack(stack ,&x);
        > printf("%d\n",x );
        > }[/color]
        [snip]

        Your code is very difficult to read. Proper use of indentation and
        whitespace would help tremendously. There may be problems beyond what
        I've pointed out, but it's not worth the pain of reading your code to
        find them.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        Working...