2. Stack
• It is an ordered group of homogeneous items of
elements.
• Elements are added to and removed from the top of the
stack (the most recently added items are at the top of the
stack).
• The last element to be added is the first to be removed
(LIFO: Last In, First Out).
3. • A stack is a non primitive linear data structure in
which addition of new element or deletion of
existing element takes place at the same end,
known as top of the stack.
• The stack is also called as Pushdown list.
• Stack is an ordered list of similar data type.
4.
5. Operations on stack
• The operation of insertion of an element into the
stack is called Push operation. The operation of
removing an element from the stack is called Pop
operation. Data is stored and retrieved in a Last In
First Out basis. It is called the LIFO principle.
That is the element which is inserted last will be
deleted first.
• Stack is said to be in Overflow state when it is
completely full and is said to be in Underflow state
if it is completely empty.
6. Stack implementation
A stack is a simple linear data structure. It can be
implemented in the following ways.
i) Static implementation ii) Dynamic Implementation
Static Implementation
Static implementation can be achieved by using arrays.
Limitations:
Once a size of the array is declared, it cannot be modified
during program execution.
If the reserved memory is more, then the memory will be
wasted.
Therefore this method is suitable only when we know exactly
the number of elements to be stored..
7.
8. Dynamic implementation
Pointers can be used to create stack. There is no
restriction on the number of elements.
It is possible to increase the size of the stack and
memory is utilized efficiently with the use of pointers.
9.
10. Creating a stack
#define size 5
int top=-1;
int stk[size];
// declare this globally so that all functions can
access.
17. Applications
The simplest application of a stack is to reverse a word.
You push a given word to stack - letter by letter - and
then pop letters from the stack.
Another application is an "undo" mechanism in text
editors; this operation is accomplished by keeping all
text changes in a stack.
Backtracking:
Consider a simple example of finding the correct path in a
maze. There are a series of points, from the starting point
to the destination. We start from one point. To reach the
final destination, there are several paths. Suppose we
choose a random path. After following a certain path, we
realise that the path we have chosen is wrong. So we
18. This can be done with the use of stacks. With the help
of stacks, we remember the point where we have
reached. This is done by pushing that point into the
stack. In case we end up on the wrong path, we can pop
the last point from the stack and thus return to the last
point and continue our quest to find the right path.
This is called backtracking
19. What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
20.
21.
22.
23.
24. Operations on Queue
a. Inserting an element into queue
b. Deleting an element from queue
c. Checking the status whether queue is empty
d. Checking the status whether queue is full
e. Displaying the elements of queue
• The first element inserted into a queue is the first
element to be removed. For this reason queue is
referred as first-in-first-out (FIFO) list.
25. /* Program to simulate the working of a Linear Queue
using an array*/
#include<stdio.h>
#include<conio.h>
void qinsert();
void qdelete();
void qdisplay();
int queue[10],front=0,rear=-1;
int max=5;
void main()
{
int ch;
do
26. { printf("nLINEAR QUEUE OPERATIONS n");
printf("1.Insert n");
printf("2.Delete n");
printf("3.Display n");
printf("4.Exit n");
printf("Enter your choicen");
scanf("%d",&ch);
switch(ch)
{
case 1:qinsert();
break;
case 2:qdelete(); break;
30. void qdisplay()
{
int item;
int p=front;
if(front==rear)
{
front=rear=-1
printf("Queue is empty");
}
else
{ printf("nQueue Elements");
while(p<=rear)
{
printf("%dt",queue[p]);
p++; } } }
31. Queue empty and Queue full Conditions
int qempty()
{
if(front == rear+1)
return 1;
else
return 0;
}
int qfull( )
{
if(rear = = max-1)
return 1;
else
return 0;
}
32. 09/10/08 33
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
(1) Insert 20. Now Rear = 5 and Front = 1
1 2 3 4 5 6 7
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
33. Circular Queue
Circular queue is a linear data structure. It follows FIFO
principle.
In circular queue the last node is connected back to the
first node to make a circle.
Circular linked list fallow the First In First Out principle
Elements are added at the rear end and the elements are
deleted at front end of the queue
Both the front and the rear pointers points to the
beginning of the array.
It is also called as “Ring buffer”.
34. 35
Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front = 0.
2. Insert 10, Rear = 1, Front = 1.
3. Insert 50, Rear = 2, Front = 1.
4. Insert 20, Rear = 3, Front = 0.
5. Insert 70, Rear = 4, Front = 1.
6. Delete front, Rear = 4, Front = 2.
Rear
Rear
Rear
Rear
Rear
Front
Front
Front
Front
Front
35. 36
7. Insert 100, Rear = 5, Front = 2.
8. Insert 40, Rear = 1, Front = 2.
9. Insert 140, Rear = 1, Front = 2.
As Front = Rear + 1, so Queue overflow.
10. Delete front, Rear = 1, Front = 3.
Front
Rear
Front
Rear
Rear
Rear
Front
Front
11. Delete front, Rear = 1, Front = 4.
12. Delete front, Rear = 1, Front = 5.
Rear
Rear
Front
Front
36.
37. Double-Ended Queue
• A double-ended queue is an abstract data type similar
to an simple queue, it allows you to insert and delete
from both sides means items can be added or deleted
from the front or rear end.
38.
39. There are two variations of a deque namely,
1. Input – restricted deque
2. Output – restricted deque
The input – restricted deque allows insertions at only one
end.
While an output – restricted deque permits deletions
from only at one end.
46. Priority Queue
A priority queue is different from a "normal" queue,
because instead of being a "first-in-first-out" data
structure, values come out in order by priority.
Rules
1.Element of higher priority is processed before any
element of lower priority.
2.Two elements with same priority are processed
according to the order in which they are added in to the
queue.
47. ASCENDING-->
Items are entered arbitrarily & only the smallest item may
be removed.
DESCENDING-->
Items are entered arbitrarily & only the largest item may
be removed. A descending priority queue is similar but
allows deletion of only the largest item.