15 Oct
15Oct

Stack is a container of an object that are removed or inserted based on the LIFO principle(Last In First Out) means The last element to enter in to the stack is the first element to be removed 


Queue is a container of an object that are removed or inserted based on the FIFO principle(First In First Out) means The First element to enter in to the queue is the first element to be removed 

Stacks and Queues

Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle.

Queue is a container of objects (a linear collection) that are inserted and removed according to the first-in first-out (FIFO) principle.


Overview


Stack:

In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack. A stack is a limited access data structure - elements can be added and removed from the stack only at the top. push adds an item to the top of the stack, pop removes the item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.



Queue:

An excellent example of a queue is a line of students in the food court of the UC. New additions to a line made to the back of the queue, while removal (or serving) happens in the front. In the queue only two operations are allowed enqueue and dequeue. Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. The picture demonstrates the FIFO access. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.


Fig 1: This shows the process of adding (enqueue) and deleting (dequeue) letters a, b, and c from a queue.



FIFO & LILO and LIFO & FILO Principles

Queue: First In First Out (FIFO): The first object into a queue is the first object to leave the queue, used by a queue.

Stack: Last In First Out (LIFO): The last object into a stack is the first object to leave the stack, used by a stack

OR

Stack: Last In First Out (FILO): The First object or item in a stack is the last object or item to leave the stack.

Queue: Last In First Out (LILO): The last object or item in a queue is the last object or item to leave the queue.




Easy and simple explanation on stacks and queues

Download PDF 


Stack C Code:

// Arup Guha
// 6/20/07
// Written in COP 3502 to illustrate an array implementation of a stack.

#include 

// The array will store the items in the stack, first in
// index 0, then 1, etc. top will represent the index
// to the top element in the stack. If the stack is 
// empty top will be -1.

#define SIZE 10
#define EMPTY -1

struct stack {
    int items[SIZE];    int top;   };

void initialize(struct stack* stackPtr);
int full(struct stack* stackPtr);
int push(struct stack* stackPtr, int value);
int empty(struct stack* stackPtr);
int pop(struct stack* stackPtr);
int top(struct stack* stackPtr);

int main() {    int i;    struct stack mine;
    // Set up the stack and push a couple items, then pop one.    initialize(&mine);    push(&mine, 4);    push(&mine, 5);    printf("Popping %d\n", pop(&mine));        // Push a couple more and test top.    push(&mine, 22);    push(&mine, 16);    printf("At top now = %d\n", top(&mine));        // Pop all three off.    printf("Popping %d\n", pop(&mine));    printf("Popping %d\n", pop(&mine));    printf("Popping %d\n", pop(&mine));        // Checking the empty function.    if (empty(&mine))        printf("The stack is empty as expected.\n");            // Fill the stack.    for (i = 0; i<10; i++)        push(&mine, i);        // Check if full works.    if (full(&mine))        printf("This stack is full as expected.\n");        // Pop everything back off.    for (i = 0; i<10; i++)        printf("popping %d\n",pop(&mine));            system("PAUSE");    return 0;
}

void initialize(struct stack* stackPtr) {     stackPtr->top = -1;
}

// If the push occurs, 1 is returned. If the
// stack is full and the push can't be done, 0 is
// returned.
int push(struct stack* stackPtr, int value) {        // Check if the stack is full.    if (full(stackPtr))        return 0;        // Add value to the top of the stack and adjust the value of the top.    stackPtr->items[stackPtr->top+1] = value;    (stackPtr->top)++;    return 1;
}

// Returns true iff the stack pointed to by stackPtr is full.
int full(struct stack* stackPtr) {    return (stackPtr->top == SIZE - 1);
}

// Returns true iff the stack pointed to by stackPtr is empty.
int empty(struct stack* stackPtr) {    return (stackPtr->top == -1);
}

// Pre-condition: The stack pointed to by stackPtr is NOT empty.
// Post-condition: The value on the top of the stack is popped and returned.
// Note: If the stack pointed to by stackPtr is empty, -1 is returned.
int pop(struct stack* stackPtr) {        int retval;        // Check the case that the stack is empty.    if (empty(stackPtr))        return EMPTY;        // Retrieve the item from the top of the stack, adjust the top and return    // the item.    retval = stackPtr->items[stackPtr->top];    (stackPtr->top)--;    return retval;
}

// Pre-condition: The stack pointed to by stackPtr is NOT empty.
// Post-condition: The value on the top of the stack is returned.
// Note: If the stack pointed to by stackPtr is empty, -1 is returned.
int top(struct stack* stackPtr) {        // Take care of the empty case.    if (empty(stackPtr))        return EMPTY;        // Return the desired item.    return stackPtr->items[stackPtr->top];
}        



Queue C Code:

// Arup Guha
// 6/26/07
// Example of how to implement a queue with an array.
#include 

#define EMPTY -1
#define INIT_SIZE 10

// Stores our queue.
struct queue {    int* elements;    int front;    int numElements;    int queueSize;
};

void init(struct queue* qPtr);
int enqueue(struct queue* qPtr, int val);
int dequeue(struct queue* qPtr);
Comments
* The email will not be published on the website.