-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlist_adt.c
116 lines (86 loc) · 2.44 KB
/
list_adt.c
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
109
110
111
112
113
114
#include<stdio.h>
struct list
{
int array[25];
int lsize;
};
void insert(struct list *ptrl, int pos, int x);//prototype
int delete(struct list *ptrl, int pos);//prototype
int print(struct list xyz);//prototype
int isFull(struct list l);//prototype
int isEmpty(struct list l);//prototype
void main()
{
struct list l;
l.lsize=0;
isFull(l); //checks whether the array is full or not before entering the element
int k;
printf("enter the no. of inputs you want to enter\n");
scanf("%d",&k); //entering the no. of inputs
while(k>25 || k<0)
{
printf("invalid input");
printf("enter the no. of inputs you want to enter\n");
scanf("%d",&k); //entering the no. of inputs again...if invalid input
}
int p,e;
printf("enter the postion and element of the array");
scanf("%d-%d",&p,&e); //entering the position and element
int i;
for(i=0; i<k; i++)
while(p>l.lsize)
{
printf("invalid input");
printf("enter the postion and element of the array");
scanf("%d-%d",&p,&e); //entering the position and element again...if invalid input
}
insert(&l,p,e); //inserting the number in the array in a certain position
isEmpty(l); //checks whether the array is empty or not after inserting the number in the array
int n; //position to be deleted
printf("enter the postion which you want to delete\n");
scanf("%d",&n); //taking the value of position to be deleted
while(n>l.lsize || n<0)
{
printf("invalid position\n");
printf("enter the valid postion which you want to delete\n");
scanf("%d",&n);
}
printf("deleted number is %d \n",delete(&l,n-1)); //shows the value of deleted number
print(l);
}//main function ends
int isFull(struct list l)
{
if(l.lsize==25)
printf("the list is full\n");
else
printf("the list is not full\n");
}//isFull function ends
void insert(struct list *ptrl, int pos, int x)
{
int i;
for(i=ptrl->lsize; i>=pos; i--)
ptrl->array[i+1]=ptrl->array[i];
ptrl->array[pos]=x;
ptrl->lsize++;
}//insert function ends
int isEmpty(struct list l)
{
if(l.lsize==0)
printf("the list is empty\n");
else
printf("the list is not empty\n");
}//isEmpty function ends
int delete(struct list *ptrl, int pos)
{
int i,x=ptrl->array[pos];
for(i=pos+1; i<ptrl->lsize;i++)
ptrl->array[i-1]=ptrl->array[i];
ptrl->lsize--;
return (x);
}//delete function ends
int print(struct list xyz)
{
int i;
for(i=0;i<xyz.lsize;i++)
printf("%d\n",xyz.array[i]);
}//print function ends