1.

An array X[n] stores names only. The first position of the array does not store a name rather it stores the number of available free spaces in the array. Write an algorithm to insert or delete an ITEM (accept it from the users) in the given array.

Answer»

Insert an ITEM:

1. ctr=0   /*Initialize the counter */

2. If LST=n then { print "Overflow:" Exit from program } 

3. if X[ctr]>ITEM then

pos=1

else

4. Repeat steps 5 and 6 until ctr>=U

5. if X[ctr]<=ITEM and ITEM<=X[ctr+1] then

{ pos=ctr+1

break 

}

6. ctr=ctr+1

7. ctr=n then

pos=n+1

 }   /* end of if step 3*/ 

/* shift the elemets to create space */

8. ctr=10   /*Initialize the counter */ 

9. while ctr>=pos perform steps 10 through 11

10. { X[ctr+1]=X[ctr]

11. ctr=ctr-1

}

12. X[pos]=ITEM  /* Insert the elements */ 

13.  END.

Delete an ITEM 

1. ctr=0

2. If n=0

{  print "Underflow" 

Exit from program

}

3. Repeat steps 4 and 5 until ctr<n

4. if(X[ctr]==ITEM) return ctr

return -1

5. if(pos!=-1)

X[pos]=0;

print "Zero (0) signifies the deleted element"

/*After this the free space will be put in the end of array */ 

6. ctr=pos

7.  Repeat steps 6 and 7 until ctr>=5

8.  X[ctr]=X[ctr+1] 

9.  ctr=ctr+1

 /* End of Repeat*/

10.  END



Discussion

No Comment Found