Data Structures
V.PARTHIPAN
AP/CSE ,
SAVEETHA UNIVERSITY
Unit II LINEAR DATA STRUCTURE
 Abstract Data Structure
List ADT
Stack ADT
Queue ADT
Abstract data type
 Mathematical model for a certain class of data
structures that have similar behavior
 Specify memory needed to store data & type of data
that will be stored in that memory location
 Defined only by the operations that may be
performed on it & by mathematical constraints of
operation
 Often implemented as modules; declares procedure
that corresponds to ADT operations
List adt
 List is an ordered set of elements
 General form of the list is A1, A2,A3…., An
 A1 : first element of list
 AN: last element of list
 N: size of list
 If elements position is Ai, successor is Ai+1 &
predecessor Ai-1
List adt
 Various operations on the list
 Insert (x,5): insert element x after position 5
 Delete(x): element x deleted
 Find(x): returns position of x
 Next(i): returns the position of its successor element i+1
 Previous(i): returns the position of its predecessor element i-1
 printlist: contents of the list is displayed
 Makeempty: makes the list empty
List adt
 Implementation
 Array implementation
 Linked List implementation
 Cursor implementation
List adt
 Array Implementation
 Collection of specific no. of data stored in a consecutive memory
locations
 Insertion & deletion are expensive as it requires more data
elements
 Find & printlist operations takes constant time
 Max. size of list considerably wastes the memory wastes the
memory space
List adt
 Linked list Implementation
 Consists of series of nodes
 Singly Linked List
 Doubly Linked List
 Circular Linked List
List adt
 Cursor Implementation
 Useful where linked list concept has to be implemented without
pointers
 Data are stored in a global array of structures. Here array index is
considered as address
 Maintains a list of free cells called cursor space
 Slot 0 is considered as a header and next is equivalent to the pointer
which points to the next slot
List adt
 Cursor Implementation
List adt
 Cursor Implementation
List adt
 Cursor Implementation Declaration
List adt
 Cursor Implementation
List adt
 Cursor Implementation
List adt
 Cursor Implementation
List adt
 Cursor Implementation
List adt
 Cursor Implementation
STACK adt
 Linear data structure which follows Last In First Out
(LIFO)
 Both insertion and deletion occur at only one end of the
list called TOP
 Eg: Pile of coins, stack of tray in cafeteria
STACK adt
 Operations on Stack
 Push
 Pop
STACK adt
 Push
 Process of inserting new element to the top of the stack
 For every push operation the top is incremented by 1
STACK adt
 Pop
 Process of deleting an element from the top of the stack
 For every push operation the top pointer is decremented by 1
STACK adt
 Exceptional conditions
 Overflow
 Attempt to insert an element when the stack is full
 Underflow
 Attempt to delete an element when the stack is empty
STACK adt
 Implementation
 Array
 Pointers
 Array Implementation
 Each stack is associated with top pointer which is -1 for empty
stack
 To push Element X onto the stack, Top pointer is incremented and
set stack[top]=X
 To pop an element, the stack[top] value is returned and top pointer
is decremented
 Pop an empty stack or push on a full stack will exceed the array
bounds
STACK adt
Routine to Push an element into stack
STACK adt
Routine to Pop an element from stack
STACK adt
Routine to return top element of the stack
STACK adt
Linked List Implementation of the stack
• Push operation is performed by inserting an element at the front
of the list
• Pop operation is done by deleting an element at the front of the
list
• Top operation returns the element at the front of the list
STACK adt
Declaration for Linked List Implementation of the stack
STACK adt
Routine to check whether stack is empty
STACK adt
Routine to create an empty stack
STACK adt
Routine to push element onto a stack
STACK adt
Routine to return top element in a stack
STACK adt
Routine to pop element from a stack
STACK adt
Applications of stack
 Evaluating arithmetic expressions
 Balancing the symbols
 Towers of Hannoi
 Function calls
 8 Queen Problem
STACK adt
Types of notations to represent arithmetic Expression
 Infix notation
 Prefix notation
 Postfix notation
Infix notation
 Arithmetic operator appears between the two
operands
 Eg. A+B/C
Postfix notation
 Arithmetic operator appears directly after two
operands
 Also called reverse polish notation
STACK adt
Prefix notation
 Arithmetic operator is placed before the two
operands
 Also called polish notation
 ((A/B)+C)  +/ABC
STACK adt
Towers of Hanoi
 One of the example illustrating the Recursion
technique
 Moving collection of N disks of decreasing size
from one pillar to another pillar
 Movement restricted by following rules
 Only one disk could be moved at a time
 No larger disk could ever reside on a pillar on top of a
smaller disk
 A 3rd pillar can be used as an intermediate to store one
or more disks, while they were being moved from
STACK adt
STACK adt
Towers of Hanoi – Recursive solution
 N – represents the no. of disks
 Steps
 If N=1, move disk from A to C
 If N=2, move the 1st disk from A to B, then move the 2nd disk
from A to C, then move the 1st disk from B to C
 If N=3, repeat the step (2) to move the first 2 disks from A
to B using C as intermediate
then 3rd disk is moved from A to C. then repeat the step (2)
to move 2 disks from B to C using A as intermediate
In general, to move N disks. Apply the recursive technique to
move N-1 disks from A to B using C as an intermediate. Then
move the Nth disk from A to C. Then again apply the
recursive technique to move N-1 disks from B to C using A as
STACK adt
Towers of Hanoi – Recursive solution
STACK adt
STACK adt
Function Calls
 to keep track of the point to which each active
subroutine should return control when it finishes
executing
 active subroutine is one that has been called but is yet
to complete execution after which control should be
handed back to the point of call

Data structures1

  • 1.
  • 2.
    Unit II LINEARDATA STRUCTURE  Abstract Data Structure List ADT Stack ADT Queue ADT
  • 3.
    Abstract data type Mathematical model for a certain class of data structures that have similar behavior  Specify memory needed to store data & type of data that will be stored in that memory location  Defined only by the operations that may be performed on it & by mathematical constraints of operation  Often implemented as modules; declares procedure that corresponds to ADT operations
  • 4.
    List adt  Listis an ordered set of elements  General form of the list is A1, A2,A3…., An  A1 : first element of list  AN: last element of list  N: size of list  If elements position is Ai, successor is Ai+1 & predecessor Ai-1
  • 5.
    List adt  Variousoperations on the list  Insert (x,5): insert element x after position 5  Delete(x): element x deleted  Find(x): returns position of x  Next(i): returns the position of its successor element i+1  Previous(i): returns the position of its predecessor element i-1  printlist: contents of the list is displayed  Makeempty: makes the list empty
  • 6.
    List adt  Implementation Array implementation  Linked List implementation  Cursor implementation
  • 7.
    List adt  ArrayImplementation  Collection of specific no. of data stored in a consecutive memory locations  Insertion & deletion are expensive as it requires more data elements  Find & printlist operations takes constant time  Max. size of list considerably wastes the memory wastes the memory space
  • 8.
    List adt  Linkedlist Implementation  Consists of series of nodes  Singly Linked List  Doubly Linked List  Circular Linked List
  • 9.
    List adt  CursorImplementation  Useful where linked list concept has to be implemented without pointers  Data are stored in a global array of structures. Here array index is considered as address  Maintains a list of free cells called cursor space  Slot 0 is considered as a header and next is equivalent to the pointer which points to the next slot
  • 10.
    List adt  CursorImplementation
  • 11.
    List adt  CursorImplementation
  • 12.
    List adt  CursorImplementation Declaration
  • 13.
    List adt  CursorImplementation
  • 14.
    List adt  CursorImplementation
  • 15.
    List adt  CursorImplementation
  • 16.
    List adt  CursorImplementation
  • 17.
    List adt  CursorImplementation
  • 18.
    STACK adt  Lineardata structure which follows Last In First Out (LIFO)  Both insertion and deletion occur at only one end of the list called TOP  Eg: Pile of coins, stack of tray in cafeteria
  • 19.
    STACK adt  Operationson Stack  Push  Pop
  • 20.
    STACK adt  Push Process of inserting new element to the top of the stack  For every push operation the top is incremented by 1
  • 21.
    STACK adt  Pop Process of deleting an element from the top of the stack  For every push operation the top pointer is decremented by 1
  • 22.
    STACK adt  Exceptionalconditions  Overflow  Attempt to insert an element when the stack is full  Underflow  Attempt to delete an element when the stack is empty
  • 23.
    STACK adt  Implementation Array  Pointers  Array Implementation  Each stack is associated with top pointer which is -1 for empty stack  To push Element X onto the stack, Top pointer is incremented and set stack[top]=X  To pop an element, the stack[top] value is returned and top pointer is decremented  Pop an empty stack or push on a full stack will exceed the array bounds
  • 24.
    STACK adt Routine toPush an element into stack
  • 25.
    STACK adt Routine toPop an element from stack
  • 26.
    STACK adt Routine toreturn top element of the stack
  • 27.
    STACK adt Linked ListImplementation of the stack • Push operation is performed by inserting an element at the front of the list • Pop operation is done by deleting an element at the front of the list • Top operation returns the element at the front of the list
  • 28.
    STACK adt Declaration forLinked List Implementation of the stack
  • 29.
    STACK adt Routine tocheck whether stack is empty
  • 30.
    STACK adt Routine tocreate an empty stack
  • 31.
    STACK adt Routine topush element onto a stack
  • 32.
    STACK adt Routine toreturn top element in a stack
  • 33.
    STACK adt Routine topop element from a stack
  • 34.
    STACK adt Applications ofstack  Evaluating arithmetic expressions  Balancing the symbols  Towers of Hannoi  Function calls  8 Queen Problem
  • 35.
    STACK adt Types ofnotations to represent arithmetic Expression  Infix notation  Prefix notation  Postfix notation Infix notation  Arithmetic operator appears between the two operands  Eg. A+B/C Postfix notation  Arithmetic operator appears directly after two operands  Also called reverse polish notation
  • 36.
    STACK adt Prefix notation Arithmetic operator is placed before the two operands  Also called polish notation  ((A/B)+C)  +/ABC
  • 37.
    STACK adt Towers ofHanoi  One of the example illustrating the Recursion technique  Moving collection of N disks of decreasing size from one pillar to another pillar  Movement restricted by following rules  Only one disk could be moved at a time  No larger disk could ever reside on a pillar on top of a smaller disk  A 3rd pillar can be used as an intermediate to store one or more disks, while they were being moved from
  • 38.
  • 39.
    STACK adt Towers ofHanoi – Recursive solution  N – represents the no. of disks  Steps  If N=1, move disk from A to C  If N=2, move the 1st disk from A to B, then move the 2nd disk from A to C, then move the 1st disk from B to C  If N=3, repeat the step (2) to move the first 2 disks from A to B using C as intermediate then 3rd disk is moved from A to C. then repeat the step (2) to move 2 disks from B to C using A as intermediate In general, to move N disks. Apply the recursive technique to move N-1 disks from A to B using C as an intermediate. Then move the Nth disk from A to C. Then again apply the recursive technique to move N-1 disks from B to C using A as
  • 40.
    STACK adt Towers ofHanoi – Recursive solution
  • 41.
  • 42.
    STACK adt Function Calls to keep track of the point to which each active subroutine should return control when it finishes executing  active subroutine is one that has been called but is yet to complete execution after which control should be handed back to the point of call