Anda di halaman 1dari 46

Data Structures and Algorithms

(DSA – 03)

Stacks and Queues

Lists and Sequences


Stacks
 
Stack
Ordered collection of data items
into which new items may be inserted
and from which data items may be deleted at
one end (Top of the stack).

Elements are removed from a stack


in the reverse order of that in which
they were inserted in to the stack.

Last-In First-Out (LIFO) lists

Piles

Push-down Lists
Stacks (Contd …)
 
Examples:
Pile of plates in a cafeteria.
Coin stacker.
Railway system for shunting cars.

The Last–in element is always the First–out element.

Operates on a Last–In First–Out (LIFO) basis.

Most commonly used as a place to  store


Local Variables Parameters
Return Addresses
when a Function is called.
Stacks (Contd …)
 
Representation of a Stack

6 F TOP
5 E
4 D
3 C
2 B
1 A
Stacks (Contd …)
 

Basic Terminology associated with Stacks:

Stack  Pointer (TOP)


Keeps track of the current position on the stack.

Overflow
Occurs when we try to insert (push) more information
on a stack than it can hold
Underflow
Occurs when we try to delete (pop) an item off a stack,
which is empty.
Stacks (Contd …)
 

Basic operations associated with Stacks:

Insert (Push) an item into the Stack.

Delete (Pop) an item from the Stack.


Algorithm for inserting an item into the stack S

Algorithm PUSH (S, MAX, TOP, ITEM)


S Array
MAX Stack Size
TOP Stack Pointer
ITEM Value in a cell
1. {Check for Stack Overflow}
If TOP ≥ MAX then
Print (‘Stack Overflow’)
Return

2. {Increment Stack pointer (TOP)}


TOP ← TOP + 1

3. {Insert ITEM at top of the Stack}


S[TOP] ← ITEM
Return
Algorithm for deleting an item from the stack S

Algorithm POP (S, TOP)


S Array
TOP Stack Pointer

1. {Check for Stack Underflow}


If TOP = 0 then
Print (‘Stack Underflow’)
Return

2. {Return former top element of stack}


ITEM ← (S[TOP])

3. {Decrement Stack pointer TOP}


TOP ← TOP – 1
Return
Applications of Stacks

Infix, Postfix and Prefix Expressions

Recursion
Infix, Postfix and Prefix Expressions

Infix Expression

Operator comes between two operands

<operand> < operator> <operand>

Example:
A+B
      Prefix / Polish Expression

Operator precedes both operands

<operator> <operand> <operand>

Example:
+AB
      Postfix / Suffix/ Reverse Polish Expression

Operator follows both operands

<operand> <operand> <operator>

Example:
AB+
      Infix operator precedence

Highest
Parentheses ( ) Left to Right

Multiplication *, Division / Left to Right

Addition +, Subtraction – Left to Right


Lowest
Give the Prefix and Postfix forms for the following Infix 
expressions.

1. a +b – c

2. a + b * c

3. (a + b) * c

4. a + (b / c) * d

5. ((a + b * c) / d / e / f * g + h) / i

6. a + (b – c * d / e) – (f / g)
Evaluation of Postfix expressions

Evaluate the Postfix expression a b c / d * +


for the values of a = 5, b = 4, c = 2 and d = 2.

Operand Scanned                  Contents of stack
a 5
b 54
c 542
/ 52
d 522
* 54
+ 9
Conversion of Infix expression to Postfix expression
Convert the Infix expression (a + b * c * d) * (e + f / d)
to Postfix form.

     Scanned     Contents of          Output postfix      


                                      Symbol          Stack           
Expression
( ( -
a ( a
+ (+ a
b (+ ab
* (+* ab
c (+* abc
* (+* abc*
d (+* abc*d
) abc*d*+
Conversion of Infix expression to Postfix expression
Convert the Infix expression (a + b * c * d) * (e + f / d)
to Postfix form.

Scanned     Contents of          Output postfix      


                                 Symbol          Stack           
Expression
     *
      ) * abc*d*+
( *( abc*d*+
e *( abc*d*+e
+ *(+ abc*d*+e
f *(+ abc*d*+ef
/ *(+/ abc*d*+ef
d *(+/ abc*d*+efd
) * abc*d*+efd/+
abc*d*+efd/+*
Recursion 

 Technique of

defining a function or a process

in terms of itself.
Examples

1.      Factorial of N
factorial(N) = 1, if N = 0
= N * factorial(N–1), if N > 0
 For N = 5
factorial(5) = 5 * factorial(4)
= 20 * factorial(3)
= 60 * factorial(2)
= 120 * factorial(1)
= 120 * factorial(0)
= 120
2. Calculation of Fibonacci numbers

fibonacci(n)
= 1, for n = 0 or n = 1
= fibonacci(n–1) + fibonacci(n–2), for n >= 2                
    

 For n=4, fibonacci(4)


= fibonacci(3) + fibonacci(2)
= fibonacci(3) + fibonacci(1) + fibonacci(0)
= fibonacci(3) + 1+ 1
= fibonacci(2) + fibonacci(1) + 2
= fibonacci(2) + 1 + 2
= fibonacci(1) + fibonacci(0) + 3
= 1+ 1+ 3
=5
3. Euclidean Algorithm (to find GCD of two integers)

GCD(m, n) = GCD(n, m), if n > m


= m, if n = 0
=GCD(n, MOD(m, n)), otherwise
where MOD(m, n) is m modulo n.

 GCD(24, 10)
= GCD(10, 4)
= GCD(4, 2)
= GCD(2, 0)
=2
4. Ackerman’s function

Acker(M, N) = N+1, if M = 0
= Acker(M–1, 1), if N = 0
= Acker(M–1, Acker(M, N–1), otherwise

Acker(1, 1)
= Acker(0, Acker(1, 0))
= Acker(0, Acker(0, 1))
= Acker(0, 2)
=3
Queues

Queue 
Ordered collection of data such that
data is inserted at one end
and deleted from another end.

First element inserted is the first element to be deleted.

Collection of items to be processed on


First-In First-Out (FIFO) /
First Come First Served (FCFS) basis.

Examples:
Structure where many people line up for few resources.

Process Queue in a time-sharing operating system.


Queues

Representation of Queue

Ascending order of memory

deletion insertion

Front Rear
Operations on Queues
 Inserting an element into a queue Q

Algorithm QINSERT (Q, MAX, F, R, ITEM)


Q Array
MAX Size of Queue
F Front Pointer of queue
R Rear Pointer of queue
ITEM Information to be inserted at the rear of queue.
At the time of creation of Q, F = R = 0;
1. {Check for queue overflow}
If R >= MAX then Print(‘Queue overflow’)
Return
2. {Increment Rear pointer}
R← R+1
3. {Insert new element at rear end of queue}
Q[R] ← ITEM
4. {If initially, the queue is empty, adjust the front pointer}
If F = 0, then F ← 1
Operations on Queues
 Deleting an element from a queue Q

Function QDELETE (Q, F, R)


Q Array of size MAX
F Pointer to the front of queue
R Pointer to the rear of queue
ITEM Temporary variable
1. {Check for queue underflow}
If F = 0 then Print (‘Queue Underflow’)
Return
2. {Delete the queue element at front end and store it into item}
ITEM ← Q[F]
3. {If queue is empty after deletion, set front and rear pointers to 0}
If F = R then
F← 0 R← 0
{Otherwise increment front pointer}
Else F ← F+1
4. Return (ITEM)
Lists

List

Linear  collection  of  data  items / nodes


explicitly ordered by a field,
which indicates the successor node in the list and
it is a flexible structure.

Items can be inserted and deleted dynamically and easily.

Facilitates dynamic allocation of nodes (Variable size).


Node

consists of two fields INFO and LINK.

INFO LINK
NODE

INFO information about the item being stored in the list.

LINK address/pointer to the next item in the list.


Function to allocate a node and to initialise it

INFO LINK
X ^
NEW

struct node *list_node (int x)


{
struct node *new;
new = (struct node *) malloc(sizeof(struct node));
new → info = x;
new → link = NULL;
return (new);
}
Linked / Singly Linked List

consists of an ordered set of nodes.

Representation

FIRST

FIRST Address/Pointer which gives


the location of the first node of the list.

/(NULL) Signals the end of the list.

→(Arrow) Indicates the successor node.


Example

A 2010 B 2002 C 2012 D 2006 E ^

First = 2000 2010 2002 2012 2006


Doubly linked lists

Have two pointers LPTR, RPTR.

Allows traversing in both left and right directions.

Node in a doubly linked list

LPTR INFO RPTR


Node

INFO Information field of a node


LPTR left link pointer field of a node
(points to the preceding node)
RPTR right link pointer field of a node
(points to the succeeding node)
Inserting an item in a Doubly Linked List 
Algorithm DOUBINS (L, R, M, X)
L left node address
R Right node address
M Address of a node to which insertion is to be performed
X New item to be inserted

1. [Obtain new node from availability nodes stack]


NEW ⇐ AVAIL

2. [Assign information field]


INFO(NEW) ← X
INFO
X

NEW
3. [Insertion into an empty list?]

if (R = NULL) then
LPTR(NEW) ← NULL
RPTR(NEW) ← NULL
L ← NEW
R ← NEW
Return

LPTR INFO RPTR


L→ ^ X ^ ←R

NEW
4. [Left most insertion]
if M = L then
LPTR(NEW) ← NULL
RPTR(NEW) ← M
LPTR(M) ← NEW
L ← NEW
Return
Before
L→ ^ X Y ^ ←R

NEW
After

L→ ^ W X Y ^ ←R

NEW M
5. [Right most Insertion]
if M = R then
RPTR(NEW) ← NULL
LPTR(NEW) ← M
RPTR(M) ← NEW
R ← NEW
Return 
Before
L→ ^ X Y ^ ←R

NEW
After

L→ ^ X Y W ^ ←R

M NEW
6. [Insert in middle]
LPTR(NEW) ← LPTR(M)
RPTR(NEW) ← M
RPTR(LPTR(M)) ← NEW
LPTR(M) ← NEW
Return

Before
L→ ^ X Y ^ ←R

LPTR(M) M
W

NEW

After

L→ ^ X W Y ^
←R

NEW M
Deleting an item from a doubly linked list
Algorithm DOUBEL (L, R, OLD)
L Address of leftmost node
R Address of rightmost node
OLD Address of the node to be deleted
LPTR Left link of the node
RPTR Right link of the node

1. [Check for underflow]


if R = NULL then
print(‘underflow’)
return
if (L = R) then [Single node in list]
L ← NULL
R ← NULL

Before
LPTR INFO RPTR
L→ ^ X ^ ←R

After
else if (OLD = L) then [Leftmost node to be deleted]
L ← RPTR(L)
LPTR(L) ← NULL

Before

L→ ^ X Y Z ^ ←R

RPTR(L)

After

L→ ^ Y Z ^ ←R
else if (OLD = R) then [Rightmost node to be deleted]
R ← LPTR(R)
RPTR(R) ← NULL

Before

L→ ^ X Y Z ^ ←R

LPTR(R)

After

L→ ^ X Y ^ ←R
else [middle node to be deleted]
RPTR(LPTR(OLD)) ← RPTR(OLD)
LPTR(RPTR(OLD)) ← LPTR(OLD)

Before

L→ ^ X Y Z ^ ←R

LPTR(OLD) OLD RPTR(OLD)

After

L→ ^ X Z ^ ←R
3. [Return deleted node]

AVAIL ← OLD
Sequences

Linear Sequence(S)
Contains n elements.

Each element e of S
uses an integer in the range [0, n–1].
Rank of an element e in S

No. of elements that are before e in S.

Element in a sequence Rank


First 0
Last n–1

Specifies where to
insert a new element
and to delete an old element.
Iterator

Software Design pattern.

Process of scanning through a collection of elements


one element at a time.

Consists of a
Sequence S
Current position in S
Way of stepping to the next positioning S
and making it the current position.
Returns the elements
according to their linear ordering.

Anda mungkin juga menyukai