Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stack vs queuing?

Tags:

stack

php

queue

hello im still a student and im a bit confused about stacking and queuing ? first question is,

  1. what is the main diffrence between them two ?

  2. btw there is circular queuing beside normal queuing how about that ? how do they work ? is there any different ways to queuing?

  3. im useing php, is there a simple ( very simple or easy to read ) sample code that i can learn on ( links are okay too. )?

  4. there is pop, push and etc ( stacking and queuing ), is there anything like that in php ?

Thank you very much for looking in.

like image 252
Adam Ramadhan Avatar asked Oct 01 '10 07:10

Adam Ramadhan


People also ask

What is the difference between a queue and a stack?

Stacks are based on the LIFO principle, i.e., the element inserted at the last, is the first element to come out of the list. Queues are based on the FIFO principle, i.e., the element inserted at the first, is the first element to come out of the list.

What is better stack or queue?

Use a queue when you want to get things out in the order that you put them in. Use a stack when you want to get things out in the reverse order than you put them in.

What is difference between stack array and queue?

Stack has a dynamic and fixed size. Queue can contain elements of different data type. Array contains elements of same data type. The stack can contain elements of the different data types.


2 Answers

1: While with stacks the insert/removal operations both work on the same end of the data structure (top)

with queues the insertion takes place at one end (rear) and the removal at the other end (front).

(Both images are from the respective wikipedia entries)

2: see http://en.wikipedia.org/wiki/Circular_buffer

3: and 4: see SplStack and SplQueue

like image 142
VolkerK Avatar answered Nov 03 '22 00:11

VolkerK


In php you would use an array() to hold your data for both stacks and queues and use the array_* functions to manipulate them. Take a look at array functions at php.net

You have

  • array_push - put a new element at end of array
  • array_pop - remove an element from end of array
  • array_shift - remove an element from the beginning of array
  • array_unshift - put a new element onto the beginning of array.

  • For a stack you'd use array_push and array_pop

  • For a queue you'd use array_push and array_shift

A circular buffer I would implement as a standalone object.

like image 38
thomasmalt Avatar answered Nov 02 '22 22:11

thomasmalt