BCS2122DCS2192 Data Structure and Algorithm - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

BCS2122DCS2192 Data Structure and Algorithm

Description:

A data structure in which all deletions and insertions are allowed only on one ... Yasmin. 2000. 2000. Zuriati. NULL. 1ST NODE. 2ND NODE. First part: - Data field ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 27
Provided by: notesU
Category:

less

Transcript and Presenter's Notes

Title: BCS2122DCS2192 Data Structure and Algorithm


1
BCS2122/DCS2192Data Structure and Algorithm
  • Module 5 Stack
  • Muhammed Ramiza Ramli

2
Stack
  • A data structure in which all deletions and
    insertions are allowed only on one end, called
    the top.
  • The last entry that is inserted is the first to
    be removed (LIFO)
  • A stack is open at one
  • end (the top) only. You
  • can push entry onto the
  • top, or pop the top entry
  • out of the stack.

3
Example
  • Reverse a string
  • Check nesting structure
  • Undo operation in many editor tools.
  • Brackets balancing in compiler
  • Stack frame of procedure call
  • Hardware stack in CPU

4
Undo operation
  • In an Excel file, input your data in row
  • 100, 200, 300, 400
  • Find something wrong, use undo, return to
    previous state
  • Operation lists

5
LIFO
6
(No Transcript)
7
Operation with Stack
  • Operations on a stack
  • pop( ) // remove top
  • push( ) // push element
  • isEmpty( ) // is it empty?
  • For a limited storage
  • isFull() // is it full?
  • Other needed operations
  • Create() //create a stack

8
Stack issues
  • Implementation issues
  • contiguous memory (array)
  • Limited size
  • Top element can be accessed by referring to
    integer variable
  • linked memory (linked list)
  • Unlimited size
  • Top element can be accessed by referring to the
    pointer

9
Stack in Array
  • Stack is limited to the size of the array
  • Operation can be done on the last element of the
    array.
  • Need two field
  • Data
  • Top index
  • Solution Using structure
  • typedef struct _stackArray
  • int top
  • float mark20
  • STACK

10
Stack in Array create
  • Create stack
  • Newly created stack should contains no element
    top must be set to non used value like -1
  • To create a stack
  • STACK markah
  • create_stack(markah)
  • Function
  • void create_stack(STACK s)
  • s-gttop -1

11
Stack in Array isEmpty
  • Its compulsory to check the stack either empty
    or full before pop or push can be done.
  • Is it empty function
  • int isEmpty(STACK s)
  • if(s-gttop lt 0)
  • return 1
  • else
  • return 0
  • isEmpty function will return integer value 1 if
    the stack is empty so no pop operation can be
    done.

12
Stack in Array isFull
  • Is it full function
  • int isFull(STACK s,int size)
  • if(s-gttop size -1)
  • return 1
  • else
  • return 0
  • isFull function will return integer value 1 if
    the stack is full and no more data can be push
    into stack.

13
Stack in Array push
  • To add a new item into stack
  • Its can be done only if the the stack is not
    full.
  • Push function
  • void push(STACK s, float item)
  • s-gttop s-gttop 1
  • s-gtmarks-gttop item

14
Stack in Array pop
  • To remove/get an item from the stack
  • Its can be done only if the the stack is not
    empty.
  • pop function
  • float pop(STACK s)
  • float x
  • x s-gtmarktop
  • s-gttop s-gttop - 1
  • return x

15
Stack in a linked list
  • Data item in the stack is not limited due to the
    node created in linked list dynamic.
  • Each stack item is represent in a form of linked
    list node.
  • Each node will contain 2 field data and link
    pointer.
  • Push or pop can be done in one end of the list
  • Top item can be referred by using head pointer.

16
Linked list
  • Each node / element contains at least 2 fields
  • Data
  • Link
  • Each node contain 1 pointer field
  • Link field contains address of the successor node
    (except the last node)

17
Illustration of node in linked-list
First part - Data field - Information of element
1ST NODE
2ND NODE
18
How to linked list look like in C
  • Consist of several connected node
  • one node pointer as a header of the list
  • nodes
  • Head pointer

1st node
2nd node
3rd node
19
Declaration
  • Declaration of linked list will be using struct
    construct.
  • typedef struct node
  • int data
  • struct node next
  • NODE
  • we create a new data type NODE as our linked
    lists node
  • any new node can be created like normal data type
  • NODE x NODE y10 NODE ptr

20
Creating a linked list
  • Linked list were declared by a head pointer of
    the list
  • This pointer will then point to the first node on
    the list and so on.
  • If there is no node on the list, head pointer
    will point to NULL (during initialization)

21
Node declaration
  • Uses static location
  • NODE x10
  • NODE head, ptr
  • Uses dynamic location
  • malloc()
  • e.g
  • NODE ptrN
  • ptrN(NODE)malloc(sizeof(NODE))

22
Stack in a linked list
  • Using structure
  • Typedef struct _stackLL
  • int data
  • struct _stackLL next
  • STACK_LL
  • Stack can be created same as linked list
  • Declare a head pointer
  • STACK_LL stackHead

23
Stack in a linked list create
  • Every newly created stack should contain no item
  • So
  • stackHead NULL
  • Or using function
  • createLLstack(stackHead)
  • Function
  • Void createLLstack(STACK_LL s)
  • s NULL

24
Stack in a linked list isempty
  • In linked list we can push how many item we want
    but still need to check the emptiness of the
    stack for pop operation.
  • Is it empty function
  • int isempty(STACK_LL s)
  • if(s NULL)
  • retrun 1
  • else
  • return 0

25
Stack in a linked list push
  • To add a new item into stack
  • No need to check either the stack is full or not
  • Push function
  • void push(STACK_LL s, int item)
  • STACK_LL newNode
  • newNode (STACK_LL)malloc(sizeof(STACK_LL)
  • newNode-gtdata item
  • newNode-gtnext s
  • s newNode

26
Stack in a linked list pop
  • To get/remove an item from stack
  • Check if the stack is not empty then proceed with
    pop operation
  • pop function
  • int pop(STACK_LL s)
  • STACK_LL delNode
  • int data
  • delNode s
  • s delNode-gtnext
  • data delNode-gtdata
  • free(delNode) //delete(delNode)
  • return data
Write a Comment
User Comments (0)
About PowerShow.com