Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

List->Static list->Addition & Deletion of an element

Addition:-
Damn simple! I would say. Here, you can add an element to the array (i.e any static list) only if the current value of its index < its size and that the array doesn't have any not NULL value for that INDEX, otherwise, it will overflow. I will elaborate this using an algorithm...

Algorithm:-
Step-1: Check for the current index value, whether < the array size; and the array does not have any not NULL value for that INDEX
Step-2: If step-1 returns true then go to step-3 else output an error message "List overflow or data already exists" and go to step-4
Step-3: array[current_index]=element;
Step-4: Repeat from 1 to 4 for the next element and index values.

Let us see its C implementation:-

int array[listsize];
addelement(int index, int element)
{
if(index<listsize && array[index]!=NULL)
    array[index]=element;
else
    printf("List overflow or data already exists");
}

Note:- This algorithm is for inserting an element anywhere in the list and not just at the end. Kindly, try out that; its very simple!


Deletion:-
Quite similar to addition but this time you will have to check only one thing that is if the array has a NULL value for the given targeted INDEX, you will return "Data Underflow"; otherwise you will put a NULL value there, signifying - element has been deleted. Let me elaborate...

Algorithm:-
Step-1: Check whether the array has a NULL value for the given INDEX
Step-2: If step-1 returns a true value then go to step-3 else output an error message saying "List Underflow" and go to step-1
Step-3: deletedval=array[index]; array[index]=NULL; 
Step-4: Return the "deletedval". Repeat from 1 to 4 for more elements to be deleted

C implementation:-
int array[listsize];
int delelement(int index)
{
int deleteval;
if(array[index]!=NULL)
  {
  deleteval=array[index];
  array[index]=NULL; 
  } 
else
  printf("List underflow");
}

Note:- Same here, this algorithm for deletion is to delete an element from anywhere in the LIST. Please, try out the sequential deletion on your own. 


Related Operations:
Addition and Deletion of elements
Traversing of list
Searching for a particular Key
Sorting of list in ascending and descending order

 

Index || Doubts / Clarifications || Related Topics || Web Links