Anjana S (20104014) Experiment No. 2 DS Laboratory

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Ex NO: 1 ARRAY IMPLEMENTATION OF LIST ADT Anjana S

DATE :15th Sep 2021 (20104014)

Aim: To write a ‘C’ program to implement List ADT using arrays.

Algorithm:
Insert Element to nth Position of the LIST
1. Check whether the List is full or not

2. If List is full return error message ”List is full. Can't Insert”.

3. If List is not full.

4. If List is Empty, Insert element at Position 1.

5. If (nth Position<0 or nth Position > Current Size)

6. Return message “nth Position Not available in List” else

7. Free the nth Position of the list by moving all Elements to one position forward except n-

1,n-2, ... 1 Position i.e move only from n to current size position Elements. i.e New

Position=Current Position + 1.

8. Insert the element to the nth Position

9. Increase the Current Size by 1 i.e. Current Size=Current Size+1

Delete Element from nth Position of the LIST


1. Check that weather the List is Empty or not

2. If List is Empty return error message ”List is Empty.”

3. If List is not Empty.

4. If (nth Position > Current Size)

5. Return message “nth Position Not available in List”

6. If (nth Position == Current Size - 1)

7. Delete the element from nth Position


8. Decrease the Current Size by 1 i.e. Current Size=Current Size-1

9. If (nth Position < Current Size)

10. Move all the Elements to one position backward except n,n-1,n-2,... 1 Position i.e

move only from n+1 to current size position Elements. i.e New Position=Current

Position - 1.

11. After the previous step, nth element will be deleted automatically.

12. Decrease the Current Size by 1 i.e. Current Size=Current Size-1

Search Element in the LIST


1. Check that weather the list is empty or not.

2. If List is empty, return error message “List is Empty”.

3. If List is not Empty

4. Find the Position where the last element available in the List by

5. Last Position = Current Size

6. For( Position 1 to Last Position)

7. If(Element @ Position== Search Element)//If Element matches the search element

8. return the Position by message “Search Element available in Position”

9. Else return message “Search Element not available in the List”

Program:
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define LIST_SIZE 30

void main()

int *element=NULL;

int ch,i,j,n;
int insdata,deldata,moddata,found;

int top=-1;

element=(int*)malloc(sizeof(int)* LIST_SIZE);

while(1)

fflush(stdin);

printf("\n\n basic Operations in a Linear List......");

printf("\n 1.Create New List \t 2.Modify List \t 3.View List");

printf("\n 4.Insert First \t 5.Insert Last \t 6.Insert Middle");

printf("\n 7.Delete First \t 8.Delete Last \t 9.Delete Middle");

printf("\nEnter the Choice 1 to 10 : ");

scanf("%d",&ch);

switch(ch)

case 1:

top=-1;

printf("\n Enter the Limit (How many Elements):");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter The Element [%d]:",(i+1));

scanf("%d",&element[++top]);

break;

case 2:

if(top==-1)

printf("\n Linear List is Empty:");

break;
}

printf("\n Enter the Element for Modification:");

scanf("%d",&moddata);

found=0;

for(i=0;i<=top;i++)

if(element[i]==moddata)

found=1;

printf("\n Enter The New Element :");

scanf("%d",&element[i]);

break;

if(found==0)

printf("\n Element %d not found",moddata);

break;

case 3:

if(top==-1)

printf("\n \n Linear List is Empty:");

else if(top==LIST_SIZE -1)

printf("\n Linear LIst is Full:");

for(i=0;i<=top;i++)

printf("\n Element[%d]is-->%d",(i+1),element[i]);

break;

case 4:

if(top==LIST_SIZE-1)

printf("\n Linear List is Full:");


break;

top++;

for(i=top;i>0;i--)

element[i]=element[i-1];

printf("\n Enter the Element:");

scanf("%d",&element[0]);

break;

case 5:

if(top==LIST_SIZE-1)

printf("\n Linear List is Full:");

break;

printf("\n Enter the Element:");

scanf("%d",&element[++top]);

break;

case 6:

if(top==LIST_SIZE-1)

printf("\n Linear List is Full:");

else if(top==-1)

printf("\n linear List is Empty.");

else

found=0;

printf("\n Enter the Element after which the insertion is to be made:");

scanf("%d",&insdata);

for(i=0;i<=top;i++)

if(element[i]==insdata)
{

found=1;

top++;

for(j=top;j>i;j--)

element[j]=element[j-1];

printf("\n Enter the Element :");

scanf("%d",&element[i+1]);

break;

if(found==0)

printf("\n Element %d Not Found",insdata);

break;

case 7:

if(top==-1)

printf("\n Linear List is Empty:");

break;

printf("\n Deleted Data-->Element :%d",element[0]);

top--;

for(i=0;i<=top;i++)

element[i]=element[i+1];

break;

case 8:

if(top==-1)

printf("\n Linear List is Empty:");

else

printf("\n Deleted Data-->Element :%d",element[top--]);


break;

case 9:

if(top==-1)

printf("\n Linear List is Empty:");

break;

printf("\n Enter the Element for Deletion :");

scanf("%d",&deldata);

found=0;

for(i=0;i<=top;i++)

if(element[i]==deldata)

found=1;

printf("\n Deleted data-->Element :%d",element[i]);

top--;

for(j=i;j<=top;j++)

element[j]=element[j+1];

break;

if(found==0)

printf("\n Element %d Not Found ",deldata);

break;

default:

free(element);

printf("\n End Of Run Of Your Program.........");

exit(0);

}
}

Output:
Result:
Thus, the ‘C’ program to implement the list ADT using arrays has been executed and the output is

verified successfully.

You might also like