This document discusses stacks and queues as linear data structures. It defines stacks as last-in, first-out (LIFO) collections where the last item added is the first removed. Queues are first-in, first-out (FIFO) collections where the first item added is the first removed. Common stack and queue operations like push, pop, insert, and remove are presented along with algorithms and examples. Applications of stacks and queues in areas like expression evaluation, string reversal, and scheduling are also covered.
What is LinearData 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.
4.
WHAT Is
Astack 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.
PUSH : Itis 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 INSERTIONIN
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 DELETIONIN
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
10.
ALGORITHM OF DISPLAYIN
STACK:
1.Display(top,i,a[i])
2.If top=0 then
Print ‘STACK EMPTY’
Exit
Else
3.For i=top to 0
Print a[i]
End for
4.exit
11.
APPLICATIONS OF STACKSARE:
I. 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.
12.
For example:
To reversethe string ‘REVERSE’ the string is
read from left to right and its characters are
pushed . LIKE:
onto a stack.
13.
II. Checking thevalidity of an expression
containing nested parenthesis:
• Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
III. Evaluating arithmeticexpressions:
INFIX notation:
The general way of writing arithmetic
expressions is known as infix notation.
e.g, (a+b)
PREFIX notation:
e.g, +AB
POSTFIX notation:
e.g: AB+
CURRENT
SYMBOL
ACTION
PERFORMED
STACK STATUS POSTFIX
EXPRESSION
(PUSH C C 2
2 2
+ PUSH + (+ 2
( PUSH ( (+( 24
4 24
- PUSH - (+(- 241
1 POP 241-
) (+ 241-
* PUSH * (+* 241-
3 241-3
POP * 241-3*
POP + 241-3*+
)
CONVERSION OF INFIX INTO POSTFIX
2+(4-1)*3 into 241-3*+
18.
Queue isan 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.
19.
1.To insert anelement in
queue
2.Delete an element from
queue
20.
Placing an itemin a queue is called
“insertion or enqueue”, which is done
at the end of the queue called “rear”.
Front
Rear
21.
Removing an itemfrom a queue is
called “deletion or dequeue”, which
is done at the other end of the
queue called “front”.
Front
Rear
1.If (front =rear)
print“queue empty” and return
2. Else
Front = front + 1
item = queue [front];
Return item
24.
Real lifeexamples
Waiting in line
Waiting on hold for tech support
Applications related to Computer Science
Round robin scheduling
Job scheduling (FIFO Scheduling)
Key board buffer
To solvethis problem, queues implement
wrapping around. Such queues are called
Circular Queues.
Both the front and the rear pointers wrap
around to the beginning of the array.
It is also called as “Ring buffer”.
Items can inserted and deleted from a
queue in O(1) time.
28.
+Queue()
+insert() : void
+remove(): long
+peekFront() : long
+isEmpty() : bool
+isFull() : bool
+size() : int
-maxSize : int
-queueArray [] : long
-front : int
-rear : int
-nItems : int
Queue
QueueApp
Interface1
It isa 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?
32.
More specializeddata structure.
Similar to Queue, having front and rear.
Items are removed from the front.
Items are ordered by key value so that the
item with the lowest key (or highest) is
always at the front.
Items are inserted in proper position to
maintain the order.
Let’s discuss complexity
33.
+Queue()
+insert() : void
+remove(): long
+peekMin() : long
+isEmpty() : bool
+isFull() : bool
-maxSize : int
-queueArray [] : long
-nItems : int
PrioityQ
PriorityQApp
Interface1
34.
Used inmultitasking operating system.
They are generally represented using “heap”
data structure.
Insertion runs in O(n) time, deletion in O(1)
time.
C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsRe
aderFilesChap04PriorityQpriorityQ.java
35.
2 +3
2 + 4 * 5
((2 + 4) * 7) + 3* (9 – 5))
Infix vs postfix
Why do we want to do this
transformation?
• 2 3 +
• 2 4 5 * +
• 2 4 + 7 * 3 9 5 - * +
36.
Read chfrom input until empty
◦ If ch is arg , output = output + arg
◦ If ch is “(“, push ‘(‘;
◦ If ch is op and higher than top push ch
◦ If ch is “)” or end of input,
output = output + pop() until empty or top is “(“
◦ Read next input
C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderPrograms
ReaderFilesChap04Postfixpostfix.java
37.
5 +2 * 3 -> 5 2 3 * +
Algorithm
◦ While input is not empty
◦ If ch is number , push (ch)
◦ Else
Pop (a)
Pop(b)
Eval (ch, a, b)
C:Documents and SettingsboxMy
DocumentsCSCSC220ReaderProgramsR
eaderFilesChap04Postfixpostfix.java