0% found this document useful (0 votes)
4 views40 pages

Lecture 3-Stack 071650

This document is a lecture on Data Structures and Algorithms, specifically focusing on linear lists and stacks. It covers the definition of stacks, their operations (push and pop), implementation methods, and applications in computing, including recursion and arithmetic expression evaluation. The document also includes algorithms and C code examples for stack operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views40 pages

Lecture 3-Stack 071650

This document is a lecture on Data Structures and Algorithms, specifically focusing on linear lists and stacks. It covers the definition of stacks, their operations (push and pop), implementation methods, and applications in computing, including recursion and arithmetic expression evaluation. The document also includes algorithms and C code examples for stack operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ICT 224

DATA STRUCTURES AND ALGORITHMS


(DSA)

Lecturer:
Matthew Cobbinah
0547900989
[email protected]

School Of Technology
Christ Apostolic University College
ICT 224– DATA STRUCTURES AND
ALGORITHMS, By Matthew Cobbinah 1
Lecture Three

LINEAR LISTS
(Stack)

ICT 224– DATA STRUCTURES AND


ALGORITHMS, By Matthew Cobbinah 2
Objectives:
At the end of this unit, you will be able to explain:
◼ Stack and its operations
◼ POP and PUSH operation in C
◼ Various stack applications
◼ Stack implementation using Arrays and Structure

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 3


Matthew Cobbinah
Introduction
◼ Linear lists are one of the simplest data
structures.
◼ An example is the one dimensional array.
◼ A linear list is a sequence of data elements or
group of related data elements whose structural
property of interest is a one dimensional
relationship expressed by positional adjacency,
hence the name linear.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 4


Matthew Cobbinah
Introduction
◼ Lists can be formed whenever the need for tables
of information arises. (E.g. lists of available
storage blocks, lists of user files, lists of return
addresses, etc).
◼ Limitations impose on how elements can be
added to and removed from the list give rise to
special types of linear structures, such as stacks
and queues

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 5


Matthew Cobbinah
STACKS
◼ A stack is a linked list in which insertions and
deletions are permitted only at one end, called
the top of the stack.
◼ Stack is based on the principle of Last in First
Out (LIFO).
◼ Two method of implementation of Stack
 Array Implementation
 Linked list implementation

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 6


Matthew Cobbinah
Operations on Stack
◼ This data structure supports two basic
operations:
 Pushing an element into the stack, an
 Popping an element off the stack.
◼ View / List Operation may also be allowed( List
the stack elements )
◼ Stack has a fixed and that size is called
STACK_SIZE.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 7


Matthew Cobbinah
Basic Operations…
◼ At the time of push operation, the stack full
condition must be checked.
 If the stack is full, called “Stack Overflow”, no
element can be inserted into the stack.
Otherwise, push the element into the stack.
◼ At the time of pop operation, the stack empty
condition must be checked.
 If the stack is empty called, “Stack
Underflow”, no element can be deleted from
the stack. Otherwise, pop the element from the
stack.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 8
Matthew Cobbinah
Push Operation
◼ So, let us assume that 5 items 30, 20, 25, 10 and
40 are to be placed on the stack. The items can be
inserted one by one as shown in following figure.

◼ After inserting 40 the stack is full (stack overflow)


 No new item can be inserted

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 9


Matthew Cobbinah
General Algorithm for Stack Operation
◼ TOP is a pointer which moves when PUSH and POP
operations are occurred.
◼ Initially, the pointer TOP =-1, implies no value is present
in the stack, that is stack empty.
 TOP = -1, implies stack underflow
◼ When PUSH operation occurred then
 TOP = TOP + 1
◼ …And when POP operation occurred then
 TOP = TOP - 1
◼ When
◼ TOP = STACK_SIZE -1, it means Overflow Item is the
value to be inserted or deleted
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 10
Matthew Cobbinah
Algorithm for PUSH Operation
◼ PUSH(stack, TOP, item)
◼ Step I : If TOP=STACK_SIZE - 1,
◼ then print “Stack Overflow” and
◼ Return [end of if structure]
◼ Step II: set TOP TOP+1
◼ Step III: set stack [TOP] item
◼ Step IV: Return

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 11


Matthew Cobbinah
Assuming three items are already added to the
stack and stack is identified by s as shown in
figure a.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 12


Matthew Cobbinah
◼ Here, the index top points to 30 which is the
topmost item. Here, the value of top is 2.
◼ Now, if an item 40 is to be inserted, first
increment top by 1 and then insert an item.
◼ The corresponding C statements are:
 top = top + 1;
 s[top] = item;
These two statements can also be written as
 s[+ + top] = item

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 13


Matthew Cobbinah
◼ when top reaches STACK_SIZE-l, stack results
in overflow condition and appropriate error
message has to be returned as shown below:
if (top == STACK_SIZE -1)
{
printf("Stack overflow\n");
return;
}

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 14


Matthew Cobbinah
◼ Here, STACK_SIZE should be defined and is
called symbolic constant the value of which
cannot be modified.
◼ If the above condition fails, the item has to be
inserted.
◼ C code for push operation:
if (top == STACK_SIZE -1)
{
printf("Stack overflow\n");
return;
}
s[ + + top] = item;
15
Function to insert an integer item
void push(int item, int *top, int s)
{
if (*top == STACK_SIZE -1)
{
printf("Stack overflow\n");
return;
}
s[+ +(*top)] = item; /* Increment top and then insert
an item */
}
Note: In the above example an item of integer
data type is inserted into the stack.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 16
Matthew Cobbinah
Function to insert a character item
Insertion of a character data into a stack:

void push(char item, int *top, char s )


{
if (*top == ST ACK_SIZE -1)
{
printf("Stack overflow\n");
return;
}
s[+ +(*top)] = item; /* Insert an item on the stack
*/
}
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 17
Matthew Cobbinah
POP OPERATION
◼ Deleting an element from the stack is called
POP operation.
◼ This can be achieved by first accessing the top
element s[top] and then decrementing top by
one as shown below:
◼ item = s[top--];
◼ Each time, the item is deleted, top is
decremented and finally, when the stack is
empty the top will be -1 and so, it is not possible
to delete any item from the stack.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 18


Matthew Cobbinah
POP Operation
◼ When an item is to be deleted, it should be deleted
from the top as shown in the following figure.

◼ The items deleted in order are 40, 10, 25, 20 and


30. Finally, when all items are deleted then TOP
points to bottom of stack.
◼ When the stack is empty, it is not possible to
delete any item and this situation is called stack
underflow.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 19
Matthew Cobbinah
POP(stack, TOP, item)
◼ Step I : If TOP=-1,
◼ then print “Stack Underflow”
◼ Return [end if Structure]
◼ Step II: set item stack[TOP]
◼ Step III: TOP TOP -1
◼ Step IV: Return

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 20


Matthew Cobbinah
The code to delete an item from stack:
if (top == -1)
{
return -1;
/* Indicates empty stack */
}
/* Access the item and delete */
item = s[top--];
return item;

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 21


Matthew Cobbinah
Display Items of a Stack
Assume that the stack contains three elements as
shown below:

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 22


Matthew Cobbinah
Code corresponding to this can take the
following form

for (i = 0; i <= top; i+ +)


{
printf("%d\n", s[i]);
}

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 23


Matthew Cobbinah
Function to display the contents of
the stack
void display(int top, int s) {
int i;
if(top= = -1)
{
printf("Stack is empty\n");
return;
}
printf("Contents of the stack\n");
for (i = 0; i <= top; i++)
{
printf("%d\n", s[i]);
}
} ICT 224– DATA STRUCTURES AND ALGORITHMS, By 24
Matthew Cobbinah
Stack Implementation
◼ The stack can be implemented by using arrays or
structures.
◼ Examples
 A railway system for shunting cars.
 A stack of plates in a restaurant.
 The magazines of some semi automatic riffle.
 A pile of office files waiting for action.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 25


Matthew Cobbinah
Use of the Stack in Computing
1. Subroutine linkage

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 26


Matthew Cobbinah
Use of the Stack in Computing
2. Use for processing languages with the
nested or block structure.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 27


Matthew Cobbinah
Use of the Stack in Computing
3. Used in compilers for parsing arithmetic
expressions when converting them from written
form (infix) into reverse polish (suffix) form which
is parenthesis-free.

4. Stacks are used frequently in connection with


recursive algorithms or procedures.

5. Stacks are very useful in managing


complicated sequences of interrupt or other
unpredictable operations.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 28


Matthew Cobbinah
SEQUENTIAL ALLOCATION OF
STORAGE FOR LINEAR LISTS

◼ The items of a linear list are stored one after the


other physically in memory so that location i.e.
LOC (x (i + 1) = LOC (x (i) + C, i ≥1, where
◼ LOC (x(i)) denotes the address of the item x(i), C
is the size of storage (in words, bytes, etc.)
occupied by each item.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 29


Matthew Cobbinah
In general, the address of any element
is determined by the ff scheme
◼ LOC (x(2) = LOC (x(1) + C
◼ LOC (x(3) = LOC (x(2) + C = LOC (x(1) + 2C
:
:
:
◼ LOC (x(i)) = LOC (x(i -1) + C
◼ = loc (x(1)) + ( i – 1) C
◼ = LO + (no. of items – 1) C
◼ where
◼ LO = LOC (x(1) is the address of the first item, called
the base address.
◼ For most of the illustrations we will take C to be 1.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 30
Matthew Cobbinah
Applications of the Stack in Arithmetic
Expressions

◼ Stacks are useful in evaluation of arithmetic


expressions.
◼ The process of writing the operators on an
expression either before operands or after
operands or in between them is called Polish
Notation,
 inhonour of its discoverer, the Polish Mathematician
Jan Luksiewiez.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 31


Matthew Cobbinah
3 types of notations for expressions.

◼ The standard form is known as the infix form.


The other two are postfix and prefix forms.
 Infix: operator is between operands [A + B]
 Postfix: operator follows operands [A B +]
 Prefix: operator precedes operands [+ A B]

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 32


Matthew Cobbinah
Example 1 (Infix to Postfix):
◼ a + b * c //[ Infix form ](precedence of * is higher
than of +)
 a + (b * c) convert the multiplication
 a + ( b c * ) convert the addition
 a (b c * ) + Remove parentheses
 a b c * + [ Postfix form ]

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 33


Matthew Cobbinah
Example 2 (Infix to Prefix):
◼ (A + B) * C Infix form
◼ (+ A B) * C Convert the addition
◼ * (+ A B) C Convert multiplication
◼ * + A B C [ Prefix form ]

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 34


Matthew Cobbinah
Algorithm for Converting Infix String
into Suffix String
◼ Assumptions/requirements
 A stack to hold characters that are not
operands.
 An output area to hold the suffix string
characters.
; is the input string delimiter.
 Single characters will be used as operators
and operands, the latter being alphabetic.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 35


Matthew Cobbinah
Operators
◼ The priorities of the operations highest to lowest
are:

 ^ exponentiation
 */ multiplication / division
 +- addition / subtraction
 = equals to
 ; semi colon

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 36


Matthew Cobbinah
Infix to Postfix Conversion

Examples:
Convert this from infix to Postfix
A = (B + C) * D

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 37


Matthew Cobbinah
USE OF STACKS IN RECURSION
◼ Recursion is the method of defining quantities in
terms of themselves.
◼ In computing there are several instances that
quantities are defined in terms of themselves and
likely to implement stack operations as well.
◼ This is very common in the use of mathematical
formulas such as finding the factorial of a
number.

ICT 224– DATA STRUCTURES AND ALGORITHMS, By 38


Matthew Cobbinah
Terminal Questions
1. Define Stack. Discuss the Push and Pop
Operations.
2. Write a C Program to implement the stack
operations using arrays.
3. Discuss the various STACK applications with a
suitable examples.
4. Explain how Stacks are useful in evaluation of
arithmetic expressions with an example.
5. Explain the procedure for evaluating a Postfix
Expression using a suitable example.
ICT 224– DATA STRUCTURES AND ALGORITHMS, By 39
Matthew Cobbinah
Q/A
T for Thanks
ICT 224– DATA STRUCTURES AND ALGORITHMS,
By Matthew Cobbinah 40

You might also like