2016년 7월 16일 토요일

c언어 자료구조 -배열을 이용한 리스트1 (ArrayList.cpp )

2009.05.06

#include "ArrayList.h"
void error(char *message)
{
 fprintf(stderr,"%s\n", message);
 exit(1); // exit 추가
}
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 add(ArrayListType *L, position pos, element item)
{
 int i;
 if(!is_full(L) && (pos >= 0) && (pos <= L->length)){
  for(i = (L->length - 1); i >= pos; i--){
   L->list[i+1] = L->list[i];
  }
  L->list[pos] = item;
  L->length++;
 }
}
element remove(ArrayListType *L, position pos)
{
 int i;
 element item;
 if(pos < 0 || pos >= L->length){
  error("위치오류");
 }
 item = L->list[pos];
 for(i = pos; i < last(L); i++){
  L->list[i] = L->list[i+1];
 }
 L->length--;
 return item;
}
position first(ArrayListType *L)
{
 if(is_empty(L)){
  error("list is empty");
  return -1;
 }
 return 0;
}
position last(ArrayListType *L)
{
 if(is_empty(L)){
  error("list is empty");
  return -1;
 }
 return L->length - 1;
}
position before(ArrayListType *L, position pos)
{
 if(is_empty(L)){
  error("list is empty");
  return -1;
 }

 if(pos == first(L)){
  error("current position is first");
  return -1;
 }
 return pos - 1;
}
position after(ArrayListType *L, position pos)
{
 if(is_empty(L)){
  error("list is empty");
  return -1;
 }

 if(pos == last(L)){
  error("current position is last");
  return -1;
 }
 return pos + 1;
}
element get_entry(ArrayListType *L, position pos)
{
 if(is_empty(L) || (pos < first(L)) || (pos > last(L))){
  error("position is out of range");
  return -1;
 }
 return L->list[pos];
}
  void add_last(ArrayListType *L, element item)
{
 add(L,L->length,item);
 
}
       //add_first(list, item) ::= 맨끝에 요소를 추가한다.
void add_first(ArrayListType *L, element item)
{
 add(L,0,item);
 

}
       //clear(list) ::= 리스트의 모든 요소를 제거한다.
void clear(ArrayListType *L)
{
 L->length=NULL;
 free(L->list);

}
       //replace(list, pos, item) ::= pos 위치의 요소를 item로 바꾼다
void replace(ArrayListType *L, position pos, element item)
{
 L->list[pos]=item;

}
       //is_in_list(list, item) ::= item이 리스트안에 있는지를 검사한다.
int is_in_list(ArrayListType *L, element wts)
{
 int i;
 for(i=0;i<L->length;i++){
  if(L->list[i]==wts){
   return 1;}}
 return 0;
}

       //get_length(list) ::= 리스트의 길이를 구한다.
int get_length(ArrayListType *L)
{
 return L->length;
}
void ans(int n)
{
 if(n){
  printf("예!!!그렇습니다!!\n");}
 else{
  printf("아니요... 그렇지 않습니당...\n");}
}

댓글 없음:

댓글 쓰기