/*
 * list.c
 *
 *  Created on: 2014. 4. 2.
 *      Author: CSE
 */

#include<stdio.h>
#include<stdlib.h>

#define MAX_LIST_SIZE 100

typedef int element;
typedef struct{
element 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){
return L->length == 0;
}

int is_full(ArrayListType *L){
return L->length == MAX_LIST_SIZE;
}

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];
L->list[position] = item;
L->length++;
}
}

void clear(ArrayListType *L)
{
int i;
for(i=(L->length-1); i>=0; i--)
L->list[i] = 0;

}

void replace(ArrayListType *L,int position, element item)
{
L->list[position] = item;
}

int get_entry(ArrayListType *L,int position)
{
return L->list[position];

}

int get_length(ArrayListType *L)
{
return L->length;

}
int main(void)
{
ArrayListType list1;
ArrayListType *plist = &list1;

init(&list1);
add(plist,0,10);
add(plist,0,20);
add(plist,0,30);

display(plist);
printf("\n");

/*clear(plist); /////////////////// // // // ///
display(plist);
printf("\n");*/

replace(plist, 1 , 3); //////////////// /////////////////
display(plist);
printf("\n");

printf("%d\n",get_entry(plist,0));
printf("%d\n",get_entry(plist,1));
printf("%d\n",get_entry(plist,2));
printf("\n");
printf("%d",get_length(plist));


return 0;
}

'이전것 > Algorithm' 카테고리의 다른 글

단순 연결 리스트 (1)  (0) 2014.04.10
단순 연결리스트  (0) 2014.04.09
도전 프로그래밍 2-3  (0) 2014.04.09
정적리스트 is_in_list  (0) 2014.04.07
SparseMatrix  (0) 2014.04.03
블로그 이미지

잉비니

,