Stack and Queue

Discussion in 'C Programming' started by raghu, Oct 25, 2006.

  1. raghu

    raghu Guest

    // Program to implement both stack and queue on an array

    int main(void)
    {
    int a[5],i;
    for(i=0;i<5;i++)
    scanf("%d",&a); // here i'm filling (pushing) the elements
    for(i=4;i>=0;i--)
    printf("%d",a); // here i'm poping out the elements
    // now for queue
    for(i=0;i<5;i++)
    scanf("%d",&a);
    for(i=0;i<5;i++)
    printf("%d",a);
    return;
    }
    My logic for stack is which ever item is stored in the last should be
    taken out first and so on...so I have filled the array and printing
    them from the last....Am I correct?????

    As for the Queue FIFO logic.So the item which is stored first is
    printed first and so on...

    Is this program correct? Please help..

    Thanks a lot in advance.

    Regards,
    Raghu
     
    raghu, Oct 25, 2006
    #1
    1. Advertising

  2. raghu said:

    > // Program to implement both stack and queue on an array
    >
    > int main(void)
    > {
    > int a[5],i;
    > for(i=0;i<5;i++)
    > scanf("%d",&a); // here i'm filling (pushing) the elements
    > for(i=4;i>=0;i--)
    > printf("%d",a); // here i'm poping out the elements
    > // now for queue
    > for(i=0;i<5;i++)
    > scanf("%d",&a);
    > for(i=0;i<5;i++)
    > printf("%d",a);
    > return;
    > }
    > My logic for stack is which ever item is stored in the last should be
    > taken out first and so on...so I have filled the array and printing
    > them from the last....Am I correct?????
    >
    > As for the Queue FIFO logic.So the item which is stored first is
    > printed first and so on...
    >
    > Is this program correct? Please help..


    No, the program is not correct. It exhibits undefined behaviour because you
    are calling variadic functions without a valid function prototype in scope.

    Even if that is corrected, your program will still exhibit undefined
    behaviour if it is presented with poorly-formed input.

    Note, also, that you should return a value from main.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 25, 2006
    #2
    1. Advertising

  3. raghu

    Guest

    raghu wrote:
    > // Program to implement both stack and queue on an array
    >
    > int main(void)
    > {
    > int a[5],i;
    > for(i=0;i<5;i++)
    > scanf("%d",&a); // here i'm filling (pushing) the elements
    > for(i=4;i>=0;i--)
    > printf("%d",a); // here i'm poping out the elements
    > // now for queue
    > for(i=0;i<5;i++)
    > scanf("%d",&a);
    > for(i=0;i<5;i++)
    > printf("%d",a);
    > return;
    > }
    > My logic for stack is which ever item is stored in the last should be
    > taken out first and so on...so I have filled the array and printing
    > them from the last....Am I correct?????
    >
    > As for the Queue FIFO logic.So the item which is stored first is
    > printed first and so on...
    >
    > Is this program correct? Please help..
    >
    > Thanks a lot in advance.


    Generally speaking, i guess your definition of stack vs. queue is
    correct. If you want to know if it is correct, why dont you just
    create the program and run it? I did, and here are the results:

    1
    2
    3
    4
    5
    54321
    1
    2
    3
    4
    5
    12345


    >
    > Regards,
    > Raghu
     
    , Oct 25, 2006
    #3
  4. raghu

    Malcolm Guest

    "raghu" <> wrote in message
    news:...
    > // Program to implement both stack and queue on an array
    >
    > int main(void)
    > {
    > int a[5],i;
    > for(i=0;i<5;i++)
    > scanf("%d",&a); // here i'm filling (pushing) the elements
    > for(i=4;i>=0;i--)
    > printf("%d",a); // here i'm poping out the elements
    > // now for queue
    > for(i=0;i<5;i++)
    > scanf("%d",&a);
    > for(i=0;i<5;i++)
    > printf("%d",a);
    > return;
    > }
    > My logic for stack is which ever item is stored in the last should be
    > taken out first and so on...so I have filled the array and printing
    > them from the last....Am I correct?????
    >
    > As for the Queue FIFO logic.So the item which is stored first is
    > printed first and so on...
    >
    > Is this program correct? Please help..
    >
    > Thanks a lot in advance.
    >
    > Regards,
    > Raghu
    >

    You are not really implementing these data structures.

    What you need to do is to write functions which will pop and push, in the
    case of the stack, and append and remove in the case of the queue.

    Stacks are easier to implement.

    Be generous and allow 100 integers maximum for the stack

    int stack[100]; /* global stack */

    Now we need a stack top. Initially is is zero

    int stacktop = 0; /* the top of the stack */

    Now implent

    /*
    push an integer onto the stack. Abort program with an error message if
    the stack overflows.
    */
    void push(int x)
    {
    }

    /*
    return the top item from the stack and remove it.
    You decide what to do if the user tries to pop and empty stack - do you
    want to abort or silently return zero ? Whatever you do, don't crash the
    program by trying to access memory at location -1.
    */
    int pop(void)

    Once you've done that, we can start on the queue.


    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
     
    Malcolm, Oct 25, 2006
    #4
  5. raghu

    CBFalconer Guest

    Malcolm wrote:
    >

    .... snip ...
    >
    > What you need to do is to write functions which will pop and push,
    > in the case of the stack, and append and remove in the case of the
    > queue.


    If you want both implement a deque, with operations:

    init()
    push()
    pop()
    dequeue()
    empty()
    full()

    The last two return booleans.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Oct 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    719
    Russell Warren
    Jun 27, 2006
  2. Kceiw
    Replies:
    3
    Views:
    1,042
    Jim Langston
    Mar 14, 2006
  3. Frank Millman
    Replies:
    3
    Views:
    365
    Frank Millman
    Feb 9, 2010
  4. Kris
    Replies:
    0
    Views:
    529
  5. bintom
    Replies:
    6
    Views:
    968
    Öö Tiib
    Nov 3, 2012
Loading...

Share This Page