CmSc 250 Intro to Algorithms


Abstract Data Types (ADT); Lists and Stacks


  1. Abstract Data Types (ADT)
  2. An ADT is a set of objects together with a set of operations to process the objects.
    One and the same set of objects may be defined with different sets of operations,
    and then we will have different ADTs.

    ADTs are implemented using classes, thus the implementational details
    are hidden from the application programs.

  3. Lists
  4. 2. 1. List definition

    Definition: A linear configuration of elements, called nodes.

    Characteristics:

    • We can insert and delete nodes in any order.
    • The nodes are connected.
    • Each node has two components:
      Information (data)
      Link to the next node

    • The nodes are accessed through the links between them.

    For each node the node that is in front of it is called predecessor.
    The node that is after it is called successor.

    Terminology:

    • Head (front, first node): The node without predecessor, the node that starts the lists.
    • Tail (end, last node): The node that has no successor, the last node in the list.
    • Current node: The node being processed. From the current node we can access the next node.
    • Empty list: No nodes exist

    2. 2. List ADT

    Notation:

    L - list
    e - item of the same type as the information part of an element (a node) in the list
    b - boolean value

    Basic operations:

    1. To create/destroy a list
    2. InitList(L)
      Procedure to initialize the list L to empty.

      DestroyList(L)
      Procedure to delete all elements in the list L.

    3. To expand/shrink the list
    4. InsertBefore(L,e)
      Procedure to insert a node with information e before the current position,
      or if L is empty - as the only node in L.
      The new node becomes the current node.

      Assumptions: There is room. If the list is nonempty, the current position is not empty.

      InsertAfter(L,e)
      Procedure to insert a node with information e after the current position,
      or if L is empty - as the only node in L. In this case the current position indicates the new node.
      The current position is not changed.

      Assumptions: there is room. If the list is nonempty, the current position is not empty.

      Delete(L)
      Procedure to delete the current node in L and to have the current position indicate the next node.

      Assumptions: the current position is not empty .

    5. Read/Write operations
    6. StoreInfo(L,e)
      Procedure to update the information part of a node, assuming it is not empty.

      RetrieveInfo(L) à e
      Function to return the information in the current node. Assume it is not empty.

      RetrieveNextInfo(L) à e
      Function to return the information in the node after the current node.
      Assume the node is not empty, and the next node is not empty
      (the current position is not the last node)

    7. Changing the current node (moving along the list)
    8. Advance(L)à b
      Boolean function to make the current position indicate the next node.
      Returns FALSE if the current position is at the last node in the list and advance is not possible.
      Assume the current position is non empty in a nonempty list

      ToFirst(L)
      Procedure to make the first node become the current node.
      If the list is empty, the current position remains empty.

    9. To report where we are in the list
    10. AtFirst(L) à b
      Boolean function. Returns TRUE if the current node is the first node (the head).

      AtEnd(L) à b
      Boolean function. Returns TRUE if the current node is the last node (the tail)

    11. To report status of the list
    12. ListEmpty(L)à b
      Returns TRUE if the list is empty

      ListFull(L)à b
      Returns TRUE if the list is full (for dynamic implementation it is always
      FALSE - the list 'never' becomes full)

      CurlsEmpty(L) à b
      Returns TRUE if the current position is empty.

    2. 3. Insertion and Deletion

    To insert a node X between nodes A and B:.

      1. Create a link from X to B.
      2. Create a link from A to X.

    To delete a node X between A and B:

      1. Create a link from A to B.
      2. Remove node X

    2. 4. List implementation

    Node linking

    1. Single linked lists: Each node contains a link only to the next node
    2. Double linked lists: Each node contains two links - to the previous and to the next node.
    3. Circular lists: The tail is linked to the head.

    Two ways to implement a list:

    Static - using an array
    Dynamic - defining a struct 'node' and using pointers

    2. 5. Array implementation of lists

    Two parallel arrays are used:

    • Index array - the number stored in the i-th element shows the index of the "next" node
    • Data array - used to store the informational part of the nodes.

    Data[0] corresponds to the head of the list.

    Data [i] holds list elements, i > 0

    Index[i] holds the position in the Data array where the next node of Data[i] is stored.

    Thus, the next node of Data[i] is stored in Data[Index[i]].

     

    In this diagram, the fourth element in the Data array is a node that contains the letter "A".
    Its corresponding fourth element in the Index array contains 3 -

    this is the position of the next node in the Data array, containing the letter "L". The third element in the Index array holds 5 - the index of the next node in the Data array,
    containing the letter "I". Thus, following the links, we can read "ALIST".

  5. Stacks
  6. 3. 1. Stack definition

    Definition: A sequence of elements of same type.
    The last stored element is the first to be accessed.
    The structure is known also under the name LIFO - last in first out

    Basic operations

    Push: store a data item at the top of the stack

    Pop: retrieve a data item from the top of the stack

    3. 2. Stack ADT

    Notation:

    S - stack
    e - item of same type as the elements of S
    b - boolean value

    Operations:

    InitStack(S)
    Procedure to initialize S to an empty stack

    DestroyStack(S)
    Procedure to delete all elements ofS

    StackEmpty(S) à b
    Boolean function that returns TRUE if S is empty

    StackFull(S) à b
    Boolean function that returns TRUE if S is full.

    Push(S,e)
    Procedure to place an item e into S
    (if there is room, i.e. S is not full)

    Pop(S) à e
    Procedure to take the last item stored in S (the top element) if S is not empty

    3. 3. Example

    A procedure to replace the elements of a nonempty stack with their sum,
    assuming they are of type integers,

    Pre: A nonempty stack with elements of type integers.

    Post: S contains only one element - the sum of previously stored elements.

    Algorithm:

    1. element1 ¬ pop(S)
    2. while stack is not empty repeat the following
    3. 2.1. element2 ¬ pop(S)

      2.2. push(S,element1+element2)

      2.3. element1 ¬ pop(S)

    4. push(S,element1)

Back to Contents page
Created by Lydia Sinapova