Data Structure (2130702)

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

Q4) (a)

Consider singly linked storage structures,Write an algorithm which inserts a node into a linked linear list in a stack like manner.

If we insert any node at first position and delete from first in singly linked list, it becomes stack

Function: PUSH(X, FIRST)

  • Given X, a new element, this function inserts a new node at the first position of linked list.
  • FIRST is a pointer to the first element of a 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.
  • This function returns address of FIRST node
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 fields of new node and its link to the list]
  INFO (NEW) <-- X
  LINK (NEW) <-- FIRST
5 [Return address of new node]
  Return (NEW)
When INSERT is invoked it returns a pointer value to the variable FIRST
FIRST <-- INSERT (X, FIRST)

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. [Is Empty list?]
  If FIRST = NULL
  Then write (‘Underflow’)
    Return(0)
2. [Delete First Node]
  VALUE <-- INFO (FIRST)
  SAVE <-- FIRST
  FIRST <-- LINK (FIRST)
3. [Free Deleted Node]
  FREE (SAVE)
  Return (VALUE)