Chaitu Tech Bits

what is difference between array and stack and queue ? what is

Wednesday, December 15, 2010


You can think of a queue like a line at the bank. The first person to get there will get to the teller first. If a bunch of people come while all the tellers are busy, they stand in line in the order in which they arrived. That is to say, new people (items) are added to the end of the line and the first person in line is the only one who is called to a teller. In real life this is known as "first come, first served." In programming terms it's known as first-in-first-out, or FIFO.

You can think of a stack like a deck of cards. You can put down cards into a pile on your table one at a time, but if you want to draw cards, you can only draw them from the top of the deck one at a time. Unlike a queue, the first card to be put down is the last card to be used. This is known as first-in-last-out, or FILO (also called LIFO for last-in-first-out). 


A queue is a first-in-first-out data structure. When you add an element to the queue you are adding it to the end, and when you remove an element you are removing it from the beginning.

A stack is a first-in-last-out data structure. When you add an element to a stack you are adding it to the end, and when you remove an element you are removing it from the end.



An array is data structure (type of memory layout) that stores a collection of individual values that are of the same data type. Arrays are useful because instead of having to separately store related information in different variables (named memory locations), you can store them—as a collection—in just one variable. It is more efficient for a program to access and process the information in an array, than it is to deal with many separate variables.

All of the items placed into an array are automatically stored in adjacent memory locations. As the program retrieves the value of each item (or "element") of an array, it simply moves from one memory location to the very next—in a sequential manner. It doesn't have to keep jumping around to widely scattered memory locations in order to retrieve each item's value.

Imagine if you had to store—and later retrieve—the names of all of the registered voters in your city. You could create and name hundreds of thousands of distinct variable names to store the information. That would scatter hundreds of thousands of names all over memory. An alternative is to simply create one variable that can will store the same information, but in sequential memory locations.

Share/Bookmark

Related Posts Plugin for WordPress, Blogger...