An Ordered List in Which All Insertions and Deletions Are Made at One End Called The TOP. LIFO (Last in First Out)
An Ordered List in Which All Insertions and Deletions Are Made at One End Called The TOP. LIFO (Last in First Out)
A B C D E F
TOP
A B C D E F G
TOP
Operations:
Create(top) – Create an empty stack
Push(Stack, top, item) – Inserts an element item into the stack
Pop(Stack, top, item) – Removes the top element of the stack
and stores the value in item
S_top(Stack, top) – Returns the top element of the stack
Empty(top) – Determines whether the stack is empty or not.
Stack full: Top = n-1
Stack empty: Top = -1
Representation:
One-dimensional array
A B C D E F
TOP
Singly linked-list
A B C D
TOP
Declaration
#define n <constant value>
typedef <data type> elementtype;
typedef elementtype Stack[n];
Example:
Processing of procedure calls and their terminations
Procedures for Stack Operations:
void create(int *top)
{*top = -1;}
Expression: (A+B*C)/D
Prefix : /+A*BCD
Postfix: ABC*+D/
IN-Stack Priority (ISP) – The priority of the operator as
an element of the stack.
FAP:UC-BCF 29
Queues
An ordered list which all insertions take place at one
end, called the REAR, while all deletions take place at
the other end, called the FRONT.
FIFO (First-In-First-Out)
Elements are processed in the same order as they were
received. The first element inserted in the queue will be
the first one to be removed.
FAP:UC-BCF 30
Queues
Conventions for FRONT and REAR:
Front is always 1 less than the actual front of the queue.
Rear always points to the last element in the queue.
Initial value: Front = Rear = -1
Operations:
Createq(Front, Rear) – creates an empty queue
Insert(Queue, Rear, Item) – inserts the element item to the rear
of the queue.
Delete(Queue, Front, Rear, Item) – removes the front element
from the queue and assigns is to variable item.
Qfront(Queue, Front, Rear) – returns the front element of the
queue
FAP:UC-BCF 31
Queues
Qempty(Front, Rear) – determines if the queue is empty or not
Returns 1 if true
Otherwise return 0
FAP:UC-BCF 32
Queues
Representation
One-dimensional Array
A B C D E F G
Front Rear
Singly linked-list
A B C D
Rear Front
FAP:UC-BCF 33
Queues
Declaration:
FAP:UC-BCF 34
Queues
Example:
Processing of customers’ transactions (i.e. cashiers, bank-related)
FAP:UC-BCF 37
.. n-1
.. 0
4 1
3 2
FAP:UC-BCF 38
void insert(queue *Q, FR front, FR *rear, elementtype
item)
{ if(front == (rear+1)%n) queuefull;
else { *rear = (*rear+1)%n;
Q[*rear] = item;}
}
FAP:UC-BCF 39
elementtype cqfront(queue Q, FR front, FR rear)
{ if(front == rear)
errorrouting; else
return(Q[front+1]%n);
}
int cquempty()
{ if(front == rear)
return 1; else
return 0;
}
FAP:UC-BCF 40