Tuesday, February 26, 2013

Data Structures - For my Ninth Grader – Part 1



In Computer science two concepts forms foundation: Data Structures and Algorithms. Let us discuss few important data structures in this post.

Queue: Have you ever stand in a line (for USA persons)/ queue (for British) for any service like in post office, bank, airport, bus stand, railway station, theater, etc. If answer is YES, then you have experienced a queue first hand. Queue can be assumed as linear collection of items/elements in which they enter from one end and leave from another. The pictorial representation of queue:

 Now, what operations one can do with a queue


1.      Enter an item/element in the queue from one end (rear) >>> enqueue

2.      Take out an item/element from the queue from another end (front) >>> dequeue

3.      Look into last item/element in the queue at front (without removing from queue)>>> peek

4.      Clear the contents of the queue >>> clear

5.      Find out, if queue has any item/element >>> isEmpty

6.      Find out how items/elements in the queue >>> size

Queue is ordered (items/elements succeeding in order of enqueue) collection of items in which items enter from rear and exit from front - one by one. As an item/element enters the queue it starts at the rear and makes its way toward the front, as items/elements ahead of it get removed one by one.

Queue is follows FIFO (first in first out).

Stack: Make a pile of books placed one on top of another. You have stack.

 One can remove or add book at the top of the stack. The pictorial representation of stack:

  One can do following operations on a stack:

  1. Add an item/element at top of stack >>> push
  2. Remove an item/element from top of stack >>> pop
  3. Find out, if stack has any item/element >>> isEmpty
  4. Find out how items/elements in the stack >>> size
  5. Look into top item/element in the stack (without removing from stack) >>> peek

Stack is ordered (items/elements succeeding in order of push) collection of items in which items enter from rear and exit from top only.

Stack is follows LIFO (last in first out). 

Deque (pronounced “deck”): Deque is double ended queue. In deque one can add item/element from rear as well as front. Similarly one can remove items/elements from both front and rear. The pictorial representation:   

One can do following operations on a deque:


1.    Add an item/element at front >>> addFront
2.    Add an item/element at rear >>> addRear
3.    Remove an item/element from front >>> removeFront
4.    Remove an item/element from rear >>> removeRear
5.    Find out, if deque has any item/element >>> isEmpty
6.    Find out how items/elements in the deque >>> size
7.    Look into last item/element in the queue at front (without removing from queue) >>> peek


Deque is ordered (items/elements succeeding in order of adding) collection of items in which items can enter from both ends (front and rear).


Deque is hybrid of Queue and Stack.

No comments:

Post a Comment