5. What is Linear Data Structure?
In linear data structure, data is arranged in linear sequence.
Data items can be traversed in a single run.
In linear data structure elements are accessed or placed in
contiguous(together in sequence) memory location.
6. What is Stack?
A stack is called a last-in-first-out
(LIFO) collection. This means that the
last thing we added (pushed) is the first
thing that gets pulled (popped) off.
A stack is a sequence of items that are
accessible at only one end of the
sequence.
7. Operations that can be
performed on STACK
• PUSH: It is used to insert items into the
stack.
• POP: It is used to delete items from
stack.
• TOP: It represents the current location
of data in stack.
8. ALGORITHM OF INSERTION
IN STACK: (PUSH)
1. Insertion (a,top, item, max)
2. If top = max then
print 'Stack Overflow'
exit
else
3. top = top + 1
end if
4. a [top]= item
5. Exit
9. ALGORITHM OF DELETION
IN STACK: (POP) 1. Deletion(a,top,item)
2. If top=0 then
print ‘STACK
UNDERFLOW’
exit else
3. item=a[top] end if
4. top=top-1
5. Exit
11. APPLICATIONS OF
STACKS ARE:
Reversing Strings:
• A simple application of stack is
reversing strings.
To reverse a string , the characters of
string are pushed onto the stack one
by one as the string is read from left
to right.
• Once all the characters
of string are pushed onto stack, they
are popped one by one. Since the
character last pushed in comes out first,
subsequent pop operation results in the
reversal of the string.
For example:
To reverse the string ‘REVERSE’
the string isread from left to right
and its characters arepushed .
Like:
TOP
STACK
String Is:
REVERSE
E
S
R
E
V
E
R
12. What is Queue?
Queue is an ADT data structure similar to stack, except
that the first item to be inserted is the first one to be
removed.
This mechanism is called First-In-First-Out (FIFO).
Placing an item in a queue is called “insertion or enqueue”,
which is done at the end of the queue called “rear”.
Removing an item from a queue is called “deletion or
dequeue”, which is done at the other end of the queue
called “front”.
Some of the applications are : printer queue, keystroke
queue, etc.
13. Dqueue
It is a double-
ended queue.
Items can be
inserted and
deleted from either
ends.More
versatile data
structure than
stack or queue.
E.g. policy-based
application (e.g.
low priority go to
the end, high go to
the front)
In a case where
you want to sort
the queue once in
a while, What
sorting algorithm
will you use?
14. The operation of Queue:
1. Placing an item in a queue is called “insertion
or enqueue”, which is done at the end of
the queue called “rear”. Placing an item in a
queue is called “insertion or enqueue”, which is
done at the end of the queue called “rear”.
2. Removing an item from
a queue is called “deletion or dequeue”, which
is done at the other end of the queue
called “front".
15. Algorithm of Queue
1. If (rear = maxsize – 1)
print ("queue overflow") and return
2. Else
rear = rear + 1
queue [rear] = item
1. If (front = rear)
print ("queue empty") and return
2. Else
Front = Front + 1
item = queue [Front];
Return item
Q-INSERT Q-DELETE
16. Application of Queue
Real life examples
Waiting in line
Waiting on hold for
tech support
Applications
related to
Computer Science
Round robin
scheduling
Job scheduling
(FIFO Scheduling)
Key board buffer
17. Difference
between Stack
and Queue:
Stacks Queues
Stacks are based on the LIFO principle, i.e.,
the element inserted at the last, is the first
element to come out of the list.
Queues are based on the FIFO principle, i.e.,
the element inserted at the first, is the first
element to come out of the list.
Insertion and deletion in stacks takes place
only from one end of the list called the top.
Insertion and deletion in queues takes place
from the opposite ends of the list. The
insertion takes place at the rear of the list and
the deletion takes place from the front of the
list.
Insert operation is called push operation. Insert operation is called enqueue operation.
Delete operation is called pop operation. Delete operation is called dequeue operation.
In stacks we maintain only one pointer to
access the list, called the top, which always
points to the last element present in the list.
In queues we maintain two pointers to access
the list. The front pointer always points to the
first element inserted in the list and is still
present, and the rear pointer always points to
the last inserted element.
Stack is used in solving problems works on
recursion.
Queue is used in solving problems having
sequential processing.
18. Stack Push
Pop operation
#include <stdio.h>
int stack[8];
int top = -1,n;
int push(int data) {
if(top == n)
{
printf("nStack overflow");
}
else
{
top = top + 1;
stack[top] = data;
}
}
void pop()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
printf("n");
for (int i=top;i>=0;i--)
{
printf("%dn",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
int main() {
Printf ("Maximum index in stack:
");
Scanf ("%d",&n);
push(3);
push(5);
push(9);
push(1);
push(12);
push(15);
show();
printf("n PoP operation");
pop();
show();
return 0;
}
19. Queue
Enqueue() and
dequeue()
operation
#include<stdio.h>
int queue[100],n;
int front = -1;
int rear = -1;
void enqueue(int num)
{
if(rear==n)
{
printf("nQueue overflow");
}
else
{
if(front==-1)
{
front =0;
}
rear = rear+1;
queue[rear] = num;
showQueue();
}
}
void dequeue()
{
if(front ==-1 || front>rear)
{
printf("nQueue underflow");
}
else
{
front = front +1;
showQueue();
}
}
void showQueue()
{
printf("n");
if(front==-1)
{
printf("Queue is empty");
}
else
{
for(int i = front;i<=rear;i++)
{
printf("%d ", queue[i]);
}
printf(" Front = %d and Rear =
%d",front,rear);
}
}
int main()
{
printf("Maximum index of queue: ");
scanf("%d",&n);
dequeue();
enqueue(5);
enqueue(7);
enqueue(1);
enqueue(1);
enqueue(1);
dequeue();
dequeue();
dequeue();
dequeue();
}