An Ordered List in Which All Insertions and Deletions Are Made at One End Called The TOP. LIFO (Last in First Out)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

An ordered list in which all insertions and deletions are

made at one end called the TOP.

LIFO (Last In First Out) G

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;}

void push(stack S; int *top;elementtype item)


{
if(top==n-1)
stackfull;
else {
*top++;
S[*top] = item;
}
}
void pop(stack S; int *top; elementtype *item)
{ if(top==1) stackempty;
else{ *item = S[top];
*top--;}
}
elementtype s_top(stack S; int top)
{ if(top==-1) error_routine;
else return S[top];
}
int empty(int top)
{ if(top == 1) return 1;
else return 0;
}
void main()
{ create(&top1); create(&top2);
s_empty = empty(top1);
printf(“%d”, s_empty);
push(s1, &top1, 16);
s_empty = empty(top1);
printf(“%d”, s_empty);
push(s1, &top1, 10); push(s1, &top1, 9);
push(s1, &top1, 8); push(s1, &top1, 7);
j=s_top(s1, top1);
printf(“Top is %d”, j);
printf(“%d\n”, top1);
s_empty = empty(top2);
printf(“%d”, s_empty);
push(s2, &top2, 10); push(s2, &top2, 9);
push(s2, &top2, 8); push(s2, &top2, 7);
push(s2, &top2, 12); push(s2, &top2, 4);
pop(s1, &top1, &item);
pop(s2, &top2, &item);
j=s_top(s2, top2); printf(“Top is %d”, j);
printf(“%d\n”, top2);
}
of
Expression is made up of operands, operators and
delimiters.

Operands can be any legal variable names or constants


in programming languages.

Operations are described by operators:


Basic arithmetic operators: + - * /
Unary operators: - +
Relational operators: ==, >, <, >=, <=
of
Our concern: how the expressions are evaluated

The compiler accepts expressions and produce correct result by


reworking the expressions into postfix form. Other forms include
infix and prefix.

Prefix : <Operator> <Operand1> <Operand2>


Postfix : <Operand1> <Operand2> <Operator>
Infix : <Operand1> <Operator> <Operand2>
of
Expression: A + B
Prefix : +AB
Postfix: AB+

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.

IN-Coming Priority (ICP) – The priority of the operator


as current token.

SYMBOL ISP ICP


) -- --
^ 3 4
*, / 2 2
+,- 1 1
( 0 4
Rule:
Operators are taken out of the stack (POP) as long as
their ISP is greater than or equal to the ICP of the new
operator.

Note: ISP and ICP of # sign = -1


Algorithm:
void POSTFIX(expression E)
token x,y;

stack[0] = ‘#’; top = 0;


x = nexttoken(E); //takes the first token and remove it
from the original expression.
while x != ‘#’
{ if x is an operand printf(x);
else if x ==‘)’ {//unstack until ‘(’
while stack[top] != ‘(’
{ pop(y); printf(y);}
pop(y); //delete ‘(’
FAP:UC-BCF } 28
else{ while isp[stack[top]] >= icp[x]
{ pop(y); printf(y); }
push(x);
}
x = nexttoken(E);
}
if(!empty(stack))
{ while stack[top] != ‘#’
{ pop(y);
printf(y);
}
}

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

Front = Rear means queue is empty.

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:

#define n <constant value>


typedef <data type> elementtype;
typedef elementtype Queue[n];
typedef int FR
FR front, rear;
Queue Q;

FAP:UC-BCF 34
Queues
Example:
Processing of customers’ transactions (i.e. cashiers, bank-related)

Procedures for Queue Operations:


void createq(FR *front, FR *rear)
{ *front = *rear = -1 }

void insert(queue Q, FR *rear, elementtype item)


{ if(rear == n-1) queuefull;
else { (*rear)++;
Q[*rear] = item; }
}
FAP:UC-BCF 35
Queues
void delete(queue Q, FR *front, FR *rear, elementtype
*item)
{ if(*front == rear) queueempty;
else { (*front)++;
item = Q[front];}
}
elementtype qfront(queue Q, FR front, FR rear)
{ if(front == rear) errorroutine;
else return(Q[front + 1])
}
int quempty()
{ if(front == rear) return 1;
else return 0;
}
FAP:UC-BCF 36
Queues
Notes:
QUEUEFULL signal does not necessary imply that
there are N elements in the queue.

To solve this, move the entire queue to the left so that


the first element is again at Q[0] and front = -1.

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;}
}

void delete(queue Q, FR *front, FR rear, elementtype


*item)
{ if(front == rear) then cqueueempty;
else { *front = (*front+1)%n;
*item = Q[front]; }
}

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

You might also like