Sei sulla pagina 1di 5

BCA 2

Stack & Queue

Data Structure

Stack
Definition and Concept:
Stack is a linear data structure similar to an array. Stack allows insertion or deletion of an element to occur only at one end. The insertion operation is referred to as PUSH and the deletion operation is referred to as POP. The most and least accessible elements in a stack are known as the TOP and BOTTOM Since insertion and deletion operations are performed at one end of a stack, the elements can only be removed in the opposite order from that in which they were added to the stack. Thus, stack is frequently referred to as LIFO (Last-In-First-Out) list. The familiar example of stack of plates on a table, in which the plate placed last, is used first and the first placed plate is used at last. Stack can be of two types: Static Stack: A stack with finite size. (Ex. Stack using array) Dynamic Stack: A Stack with infinite size. (Ex. Stack using pointer or Linked List)

Initial Stack: Deletion Insertion

TOS PUSH 10:


10

TOS PUSH 20,30,40,50 one by one Final state:


10 20 30 40 50

TOS POP:
10 20 30 40

TOS Note: Weve removed last element from stack, but it will remain there in array. Weve removed it from graphical presentation for better understanding.

Page 1 of 5

BCA 2

Stack & Queue

Data Structure

Operation on Stack:
PUSH: Inserts an element to stack. It increases Top Of Stack by one, when new element is pushed. For finite or static stack, care should be taken for overflow. POP: Removes an element from stack. It decreases Top Of Stack by one, when new element is popped. For finite or static stack, care should be taken for underflow. PEEP: Returns current element stored at Top of Stack. Some books refer it as a function that returns any desired value from the stack. DISPLAY: Displays all the elements stored in a Stack. You can use any name for this function like list, show, etc.

Applications of Stack:
1) Stack in Recursion Stacks are an important way of supporting nested or recursive function calls. The recursion (a function that calls itself directly or indirectly) is based on LIFO mechanism. Example of factorial with explanation. This characteristic of recursion suggests that we can use stack to push (store) returned value by each step of recursion and then can pop (retrieve) them back at the end. 2) Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low-level block. For example: The calculation ((1+2 ) * 4) + 3 can be written down like 1 2 + 4 * 3 + using postfix notation with advantage of no precedence rules and parentheses required. The expression is evaluated from the left to right using a stack. a. Push when encountering an operand and b. Pop two operands and evaluate the value when encountering the operation c. Push the result Like the following way Input 1 2 + 4 * 3 + Operation Push operand push operand Add push operand Multiply Push operand Add Stack 1 1, 2 3 3, 4 12 12, 3 15

The final result lies on the top of stack at the end of calculation Page 2 of 5

BCA 2

Stack & Queue

Data Structure

3) Runtime memory management A number of programming languages are stack-oriented, meaning they define most basic operations (adding two numbers, printing a character) as taking their arguments from the stack, and placing any return values back on the stack. The programming language Forth (Similar to C) is referred to as a stack-based language. Many virtual machines are also stack-oriented, including the Java virtual machine used in Java Almost all computer runtime memory environments use a special stack (the "call stack") to hold information about procedure/function.

Q. 1. Q. 2. Q. 3. Q. 4. Q. 5. Q. 6. Q. 7.

Define stack. What is top of stack? Why stack is called LIFO list? What are the operations that can be performed on a stack?. What is empty stack? What causes underflow of stack? How it could be avoided? What is overflow in stack?

Queue:
Definition and Concept:
Queue is a linear data structure similar to an array and Stack. Queue allows the insertion to be performed from one end and deletion from other end. Here, the element that gets deleted in the order in which it was inserted. Thats why it is referred to as First In First Out (FIFO) or First Come First Served (FCFS) list. The insertion operation is referred to as INSERT and the deletion operation is referred to as DELETE. Here, we can use two different variables to keep track of front and rear position of a queue. The familiar example of Queue is a queue of persons waiting fro reservation on Railway Station or Cinema Hall. Another perhaps most relevant example of a queue can be found in time-sharing computers, or queue managed by printer to handle multiple requests. Initial Queue:

Insertion

Deletion

Operation on Queue:
INSERT / STORE / ENQUEUE: Inserts an element to queue. It increases front of queue (f) by one, when new element is inserted. For finite or static queue, care should be taken for overflow. DELETE / RETRIEVE / DEQUEUE: Removes an element from queue. It increases rear of queue(r) by one, when new element is deleted. Page 3 of 5

BCA 2

Stack & Queue

Data Structure

For finite or static queue, care should be taken for underflow.

DISPLAY / PRINT / LIST: Displays all the elements stored in a Queue. You can use any name for this function like list, show, etc.

Types of Queue:
Circular Queue: A simple queue has a limitation that it cannot enter new elements once it reaches Even if, we delete any element and make some room to accommodate new
element, it wont allow insertion due to its limitation. A circular queue is a queue that has no end and one can insert as many elements as a queue can accommodate. The circular queue can hold fixed number of elements at a time (MAX. SIZE) but can allow new element to be inserted after one or more elements are removed. to maximum size.

One can get more idea about circular from following fig.

q[2]

30

20 q[1]

q[3]

40

10 q[0]

F R The advantage of circular queue is that we can utilize static memory for unlimited number of elements.

Deque (Double Ended Queue): A Queue that is an array in which insertion and deletion are made to or from It is clear that a deque is more general than a stack or a queue. There are two variations of a deque, namely, the input-restricted deque and the
output restricted deque. The input-restricted deque allows insertion at only one end. While and outputrestricted deque permits deletion from only one end. Following figure illustrates deque concept. Deque Deletion Insertion Deletion Insertion Page 4 of 5 either end of the Queue.

Deletion

BCA 2

Stack & Queue

Data Structure

Note: Deque (Double Ended Queue) and Dequeue (Delete an element from Queue) both are different, and one should take care of their use.

Priority Queue: A Queue in which we are able to insert items or remove items from any position
based on some property (such as priority of the task to be processed) is often referred to as a priority queue. When elements are inserted, they are always added at the end of queue as determined by the priority. Retrieval (Deletion) of elements is based on priority and elements having high priority get first chance to be served even if they are not at the first position. Thus we can say, Priority queue doesnt follow FIFO but, its based on priority mechanism and element with highest priority gets served first and element having lowest priority gets last chance to be served. Following figures is an example of priority-based queue of jobs waiting for a computer processor.

Priority Queue

Insertion

Deletion (Served)

(Lowest Priority)

(Highest Priority)

Applications of Queue:
1) Time sharing computers use queue for all requests received. This is required when single processor or resources like printer, storage, data, etc is shared by more than one clients. Even though some machines use priority queue instead of simple queue. 2) Printers use queues to handle all the printing requests; they generally keep request in buffer (queue) and serve them on FIFO basis. 3) Circular queue can be used for circular buffer, in which elements are stored in buffer for temporary use.

Page 5 of 5

Potrebbero piacerti anche