C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Q3. WAP to search (linear), insert and delete (at position) an element in an array.

posted Jan 5, 2011, 10:46 AM by Neil Mathew   [ updated Mar 15, 2011, 1:41 AM ]

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
 
int Lsearch(int *a, int n, int item)
{
int i;
for(i=0; i<n; i++)
{
if(item==a[i])
return i+1;
}
return -1;
}
 
void Ins(int *a, int *n, int pos, int item) 
//n is called by reference
{
int i;
for(i=*n; i>pos; i--)
a[i]=a[i-1];
 
a[pos]=item;
*n=*n+1;
}
 
void Del(int *a, int *n, int pos)
{
int i;
for(i=pos; i<(*n)-1; i++)
a[i]=a[i+1];
 
*n=*n-1;
}
 
main()
{
int a[50],n,i;
int item,pos;
 
printf("Enter size of your array: ");
scanf("%d",&n);
 
printf("Enter elements of your array: ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
 
int ch; 
 
do
{
printf("\nEnter the choice: ");
printf("\n1. Search for an element.");
printf("\n2. Insert an element. ");
printf("\n3. Delete an element. ");
printf("\n4. Display all elements. ");
printf("\n0. EXIT ");
printf("\n Your choice: "); 
scanf("%d",&ch);
 
switch(ch)
{
case 1: //SEARCH
 
printf("\n Enter the item to search for: ");
scanf("%d",&item);
 
pos=Lsearch(a,n,item);
 
if(pos==-1)
printf(" Element Not Found \n");
else
printf(" Element Found at %d \n",pos);
 
break;
 
case 2: //INSERT;
 
printf("\n Enter the item to insert: ");
scanf("%d",&item);
 
printf("\n Enter the position to insert element: ");
scanf("%d",&pos);
  //pos-1 cause user doesn't start count from zero
 
Ins(a,&n,(pos-1),item); 
break;
 
case 3: //DELETE;
 
printf("\n Enter the position of element to delete: ");
scanf("%d",&pos);
 
Del(a,&n,(pos-1));
break;
 
case 4: //Display
 
printf("\n Elements: ");
for(i=0; i<n; i++)
printf(" %d ",a[i]);
 
printf("\n");
break;
}
 
 
}while(ch!=0);
 
 
}


OUTPUT:

Enter size of your array: 5
Enter elements of your array: 2 4 6 8 10

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 2

 Enter the item to insert: 7

 Enter the position to insert element: 4

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 4

 Elements:  2  4  6  7  8  10 

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 3

 Enter the position of element to delete: 5

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 4

 Elements:  2  4  6  7  10 

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 1

 Enter the item to search for: 7
 Element Found at 4

Enter the choice: 
1. Search for an element.
2. Insert an element. 
3. Delete an element. 
4. Display all elements. 
0. EXIT 
 Your choice: 0


Comments