Program of List using Array

In the array implementation of lists, we will use array to hold the entries and  a separate counter to keep track of the number of positions are occupied. 

 

In the Array implementation of lists, elements are stored in continuous locations. Arrays are data structures(homogeneous) of fixed size(MAX 20, int arr[MAX]). Insertion and deletion involves reshuffling(inter change the position) of array elements.

Program of list using array ..........

# include<stdio.h>

# define MAX 20

int arr[MAX];

int no;  /*Total number of elements in the list */

main()

{

                int  choic,iteem,posit;

                while(1)

                {

                           printf("Press-1...Input list\n");

                           printf("Press-2...Insert\n");

                           printf("Press-3...Search\n");

                           printf("Press-4...Delete\n");

                           printf("Press-5...Display\n");

                           printf("Press-6...Quit\n");

                           printf("Enter your choiceeee : ");

                                scanf("%d",&choic);

                                switch(choic)

                                {

                                 case 1:

                                                printf("Pls Enter the no. of elements: ");

                                                scanf("%d",&no);

                                                input(no);

                                                break;

                                 case 2:

                                                insert();

                                                break;

                                 case 3:

                                                printf("Pls Insert the  1 element for searching: ");

                                                scanf("%d", &iteem);

                                                posit = search(iteem);

                                                if(posit >= 1)

                                                                printf("%d found at positition %d\n",iteem,posit);

                                                else

                                                                printf("Element not found\n");

                                                break;

                                 case 4:

                                                del();

                                                break;

                                 case 5:

                                                display();

                                                break;

                                 case 6:

                                                exit();

                                                break;

                                 default:

                                                printf("Wrong choic\n");

                                } /* switch End */

                }/* while End */

}/* main() End*/

input()

{

                int ii;

                for(ii = 0; ii< no ; ii++)

                {

                                printf("Input value for element %d : ", ii+1);

                                scanf("%d", &arr[ii]);

                }

}/*End of input()*/

int search(int iteem)

{

                int ii;

                for(ii=0; ii < no; ii++)

                {

                                if(iteem == arr[ii])

                                                return(ii+1);

                }

                return(0);  /* If element not found */

}/*search() End*/

insert()

{

                int temp,iteem,positition;

                if(no == MAX)

                {

                                printf("List overflow\n");

                                return;

                }

                printf("Enter positition for insertion : ");

                scanf("%d", &positition);

                printf("Enter the value : ");

                scanf("%d",&iteem);

                if(positition > no+1 )

                {

                                printf("Enter positition less than or equal to %d\n",no+1);

                                return;

                }

                if( positition == no+1 )  /*Insertion at the end */

                {

                                arr[no] = iteem;

                                no = no+1;

                                return;

                }

                /* Insertion in between */

                temp=no-1;

                while( temp >= positition-1)

                {

                                arr[temp+1] = arr[temp];  /* shifting right */

                                temp --;

                }

                arr[positition-1] = iteem;

                no = no +1 ;

}/* insert() End*/

del()

{

                int temp,positition,iteem;

                if(no == 0)

                {

                                printf("List underflow\n");

                                return;

                }

                printf("Enter the element to be deleted : ");

                scanf("%d",&iteem);

                if(iteem==arr[no-1]) /*Deletion at the end*/

                {

                                no = no-1;

                                return;

                }

                positition=search(iteem);

                if(positition==0)

                {

                                printf("Sorry...!Element not present \n");

                                return;

                }

                /*Deletion in between */

                temp=positition-1;

                while(temp <= no-1)

                {

                                arr[temp] = arr[temp+1];  /* Shifting left */

                                temp ++;

                }

                no = no - 1 ;

}/*del() End*/

display()

{

                int ii;

                if(no==0)

                {

                                printf("List is empty\n");

                                return;

                }

                for(ii = 0; ii< no; ii++)

                                printf("Value at positition %d : %d\n", ii+1, arr[ii]);

}/*display() End   */

Output....

list using array

 

 

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @