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
|
#include<stdio.h>
#include<stdlib.h>
#define MAX_LIST_SIZE 100
typedef int element;
typedef struct {
int list[MAX_LIST_SIZE];
int length;
}ArrayListType;
void error(char *message)
{
fprintf(stderr,"%s\n", message);
exit(1);
}
void init ( ArrayListType *L)
{
L->length = 0;
}
int is_empty(ArrayListType *L)
{
if(L->length == 0)
return 1;
else
return 0;
}
int is_full(ArrayListType *L)
{
if(L->length == MAX_LIST_SIZE)
return 1;
else
return 0;
}
void display(ArrayListType *L)
{
int i;
for(i = 0 ; i < L->length ; i++)
printf("%d\n", L->list[i]);
}
void add(ArrayListType *L, int position, element item)
{
if( !is_full(L) && (position >= 0 ) && (position <= L->length) )
{
int i;
for(i = (L->length - 1) ; i >=position ; i--)
L->list[i+1] = L->list[i];
//position
L->list[position] = item;
L->length++;
}
}
element delete(ArrayListType *L, int position)
{
int i;
element item;
if(position < 0 || position >= L->length )
error("error");
item = L->list[position];
for( i = position ; i <= (L->length -1) ; i++)
L->list[i] = L->list[i+1];
L->length--;
return item;
}
int main(void)
{
ArrayListType list1;
ArrayListType *plist;
init(&list1);
add(&list1, 0, 10);
add(&list1, 0, 20);
add(&list1, 0, 30);
display(&list1);
plist = (ArrayListType *)malloc(sizeof(ArrayListType));
init(plist);
add(plist, 0, 10);
add(plist, 0, 20);
add(plist, 0, 30);
display(plist);
free(plist);
system("PAUSE");
return 0;
}
|