Logo 
Search:

C++ Programming FAQ

Submit Interview FAQ
Home » Interview FAQ » C++ ProgrammingRSS Feeds

Write an algorithm for Inserting a Node using Singly Linked List in dfs (data file structure).

  Shared By: Logan Bouchard    Date: Jan 18    Category: C++ Programming    Views: 12053

Answer:

PROCEDURE INSERT(T, KEY)
[Where ‘head’ pointer has been caught in pointer ‘T’ & the value in ‘key’]

1. [Checking for insertion at beginning and inserting]

if(key <= 0)
Call GETNODE (I)
DATA (I) <-- ‘xyz’
LINK (I) <-- T
T <-- I
return (T).

2. [Traversing to the destination node]

I <-- T
while (DATA (T) != key)
if (LINK(T) == 0)
return (0)
else
T <-- LINK(T))

3. [Inserting the node in the list]

if (LINK(T) == 0)
GETNODE (P)
LINK (P) <-- 0
LINK (T) <-- P
DATA (P) <-- ‘xyz’
return (I)
else
GETNODE (P)
C <-- LINK (LINK(T))
LINK (P) <-- C
LINK (T) <-- P
DATA (P) <-- ‘xyz’
return (I)

4. [FINISH]
return.

Share: 
 


Related Topics:

Your Comment
  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].


Tagged: