You are on page 1of 7

EX.

NO:1 AIM:
ARRAY IMPLEMENTATION OF LIST ADT
DATE:
To implement the Linked List data structure using the array.

DESCRIPTION:

An abstract data type or abstract data structure is a mathematical model for a certain class of data
structures that have similar behavior or data types of one or more programming languages that have
similar semantics. An array is a data structure consisting of a collection of elements (values or
variables), each identified by one or more integer indices, stored so that the address of each element can
be computed from its index tuple by a simple mathematical formula. Arrays are often used to implement
tables, especially lookup tables. Many databases, small and large, consist of (or include) one-
dimensional arrays whose elements are records. Arrays are used to implement other data structures,
such as heaps, hash tables, deques, queues, stacks, strings, and VLists. Insertion and deletion operation
are expensive, as it requires more data movement. A find and printlist operation takes constant time.

ALGORITHM:

Step 1: Declare the Global Variables like i, q, pos and structure array names.

Step 2: Declare the Modules used in the program, i.e. Prototype the user defined functions.

Step 3: Display the List of choice of operation using the printf function in the
main module.

Step 4: Get the choice of Operation and call the sub-modules accordingly using the Multiway
branching statement, i.e., Switch ()-case.

Step 5: Perform the operation in the respective module and print the output.

PROGRAM :

#include<stdio.h>
#include<stdlib.h>
#define max 100
struct array
{
int list[max];
int last;
};
struct array p;
int i,q,t,x,pos,n;
int main()
{
void create();
void insert(int,int);
void display();
void del();
int search();
while(1)
{
printf("1.Creation\n2.Insertion\n3.Display\n4.Search\n5.Deletion\n6.Exit\n");
printf("Enter your choice\n");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
printf("\nEnter the Element to be inserted\n");
scanf("%d",&x);
printf("\nEnter the Position of the Element to be Inserted\n");
scanf("%d",&pos);
insert(x,pos);
break;
case 3:
display();
break;
case 4:
t=search();
if(t==1)
printf("\nElement found");
else
printf("\nElement not found");
break;
case 5:
del();
break;
case 6:
exit(0);
break;
}}
return(0);
}
void create()
{
printf("\nEnter the length of the list");
scanf("%d",&p.last);
printf("Enter the elements\n");
for(q=0;q<p.last;q++)
{
scanf("%d",&n);
p.list[q]=n;
}
}
void insert(int x,int pos)
{
if((pos>max)||(pos<0))
printf("Error\n");
else
if(pos==max)
printf("Error\n");
else

for(q=p.last;q>=pos;q--)
p.list[q+1]=p.list[q];

p.list[pos]=x;
p.last=p.last+1;
}
void display()
{
for(q=0;q<p.last;q++)
printf("%d\n",p.list[q]);
}
void del()
{
printf("\nEnter the position of the element to be deleted\n");
scanf("%d",&n);
if((n>max)||(n<0))
printf("Error\n");
else
p.last=p.last-1;

for(q=n;q<p.last;q++)
p.list[q]=p.list[q+1];
}
int search()
{
printf("\nEnter the element to be searched\n");
scanf("%d",&n);
for(q=0;q<p.last;q++)
{
if(p.list[q]==n)
return 1;
else
continue;
}
return 0;
}

SAMPLE INPUT AND OUTPUT:

[csea01@ksrctitp ~]$ cc array.c


[csea01@ksrctitp ~]$ ./a.out

1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
1
Enter the length of the list
5
Enter the elements
1
2
3
4
5

1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
2
Enter the Element to be Inserted
555
Enter the position of the Element to be Inserted
2

1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
3
1
2
555
3
4
5

1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
4
Enter the Element to be Searched
3
Element found
1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
4
Enter the element to be Searched
8
Element not found
1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit
Enter your choice
5
Enter the position of the element to be deleted
2
1. Creation
2. Insertion
3. Display
4. Search
5. Deletion
6. Exit

Enter your choice 6

PROBLEMS:
1. Write a C program using array implementation of list to implement polynomial addition and
subtraction.
2. Write a C program using array implementation of list to implement polynomial multiplication.
3. Write a C program to create lists L1, L2 and L3 and combine into a single list L and display the
elements.

RESULT:

Marks Marks
Details Status
Allotted Awarded
Preparation 40
Error Free Compilation 20
Execution 20
Result 10
Viva – Voce 10
Total 100

Signature of the faculty


SAMPLE VIVA QUESTIONS

1. Define abstract data type.


2. List the advantages of modularity.
3. Define list ADT.
4. Mention the operations performed by the list.
5. List the various ways of implementation list.
6. Mention the limitations of array implementation list ADT.
7. What do you mean by linear and nonlinear list?
8. Annotate Data Structure.
9. Give an example for linear data structure.
10. Give an example for non-linear data structure.

You might also like