Data Structure (2130702)

BE | Semester-3   Summer-2017 | 05/31/2017

Q4) (a)

Consider singly linked storage structures,Write an algorithm which performs an insertion at the end of a linked linear list.

Function: InsertEnd(X, FIRST)

  • This function inserts a new node at the last position of linked list.
  • This function returns address of FIRST node.
  • X is a new element to be inserted.
  • FIRST is a pointer to the first element of a Singly linked linear list.
  • Typical node contains INFO and LINK fields.
  • AVAIL is a pointer to the top element of the availability stack.
  • NEW is a temporary pointer variable.

Function: POP( FIRST)

  • This algorithm deletes and returns First Node from Singly Linked List.
  • FIRST is a pointer to the first element of a linked linear list.
  • Typical node contains INFO and LINK fields.
1. [Underflow?]
  If AVAIL = NULL
  Then Write (“Availability Stack Underflow”)
    Return(FIRST)
2. [Obtain address of next free Node]
  NEW<--AVAIL
3. [Remove free node from Availability Stack]
  AVAIL<--LINK(AVAIL)
4. [Initialize field of NEW node]
  INFO (NEW) <-- X
  LINK (NEW) <-- NULL
5. [Is the list empty?]
  If FIRST = NULL
  Then Return (NEW)
6. [Initialize search for a last node]
  SAVE <-- FIRST
7. [Search for end of list]
  Repeat while LINK (SAVE) ? NULL
    SAVE <-- LINK (SAVE)
8. [Set link field of last node to NEW]
  LINK (SAVE) <-- NEW
9. [Return first node pointer]
  Return (FIRST)
When INSERTEND is invoked it returns a pointer value to the variable FIRST
FIRST <-- INSERTEND (X, FIRST)