Sunday 6 February 2011

data structure

• Function CREATE (FRONT)
This function is used to create list. FRONT is a NODE type pointer variable of structure LinkList having two member variables INFO which stores actual information and structure type pointer NEXT which store address of next node. Its last node contains address of first node. This function returns the address of first node.

Variable :
FIRST : is NODE type pointer which stores address of first node
CH : which is used to get whether user want to continue or not.

1. [Allocate the memory for first node]
FRONT <= NODE
2. [Read the information]
Read (INFO(FRONT))
3. [Temporary storage of head]
FIRST ← FRONT
4. [Check for continuity]
Read (CH)
5. [Iterate the loop]
Repeat through step 9 while CH ≠ ‘N’
6. [Allocate memory for next node]
NEXT(FRONT) <= NODE
7. [Move FRONT pointer to next node]
FRONT ← NEXT(FRONT)
8. [Read the information of new node]
Read (INFO(FRONT))
9. [Check for continuity]
Read (CH)
10. [Termination of loop]
NEXT(FRONT) ← FIRST
11. [Return address of first node]
Return FIRST


• Procedure PRINT (FRONT)
This procedure prints the information of all the nodes. Description as per create function.

Variable :
FIRST : Description as per create function.

1. [Temporary storage of head]
FIRST ← FRONT
2. [Iterate the loop]
Repeat through step 3 while NEXT(FRONT) ≠ FIRST
3. [Print information of current node]
Write (INFO(FRONT))
4. [Move FRONT pointer to next node]
FRONT ← NEXT(FRONT)
3. [Print information of last node]
Write (INFO(FRONT))
6. [Finish]
Exit

• Function INSERT(FRONT,KEY,VAL)
This function inserts value before the given value. Description as per create function. KEY is node before which we want to insert. VAL is variable which store the information of value to be inserted. This function returns address of first node.

Variable :
NEW : is a NODE type pointer which store information of new node to be inserted.
TEMP : is NODE type pointer which stores address of first node.

1. [Allocate the memory for first node]
NEW <= NODE
2. [Store information of new node]
INFO(NEW) ← VAL
3. [Store head into temporary variable]
TEMP ← FRONT
4. [Insertion as first node]
If INFO(FRONT) = KEY
Then
NEXT(NEW) ← FRONT
(iterate loop till last node)
Repeat while NEXT(FRONT) ≠ TEMP
FRONT ← NEXT(FRONT)
NEXT(FRONT) ← NEW
TEMP← NEW
Return TEMP
5. [Repeat loop up to last node]
Repeat while NEXT(FRONT) ≠ NULL
(Check for key value in next node)
If INFO(NEXT(FRONT)) = KEY
Then
NEXT(NEW) ← NEXT(FRONT)
NEXT(FRONT) ← NEW
Return TEMP
Else
(move first pointer on next node)
FRONT ← NEXT(FRONT)
6. [Prompt message key not found]
Write ‘Key value is not in list’
7. [Return head]
Return TEMP


• Function DELETE (FRONT,KEY)
This function is used to delete a node from the list. Description as per create function. KEY is node which is to be deleted. This function returns address of first node.

Variable :
FIRST : is NODE type pointer which stores address of first node
REMOVE : is NODE type pointer which stores the node to be deleted.

1. [Store head into temporary variable]
FIRST ← FRONT
2. [Deletion as first node]
If INFO(FRONT) = KEY
Then
REMOVE ← FRONT
(iterate loop till last node)
Repeat while NEXT(FRONT) ≠ TEMP
FRONT ← NEXT(FRONT)
NEXT(FRONT) ← NEXT(FIRST)
FIRST ← NEXT(FIRST)
(Free the memory of deleted node)
Restore REMOVE
Return FIRST
3. [Repeat loop up to last node]
Repeat while NEXT(FRONT) ≠ NULL
(Check for key value in next node)
If INFO(NEXT(FRONT)) = KEY
Then
REMOVE ← NEXT(FRONT)
NEXT(FRONT) ← NEXT(NEXT(FRONT))
(Free the memory of deleted node)
Restore REMOVE
Return FIRST
Else
(move first pointer on next node)
FRONT ← NEXT(FRONT)
4. [Prompt message key not found]
Write ‘Key value is not in list’
5. [Return head]
Return FIRST

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms