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

typedef int element;
typedef struct ListNode{
 element data;
 struct ListNode* link;
}ListNode;

/*void error(char* message)
{
 fprintf(stderr,"%s\n",message);
 exit(1);
}
*/
ListNode* create_node(element data, ListNode* link)
{
 ListNode* new_node;
 new_node = (ListNode *)malloc(sizeof(ListNode));
 if(new_node == NULL){
  fprintf(stderr,"메모리할당에러");
 exit(1);
 }
 new_node->data=data;
 new_node->link=link;
 return (new_node);
}

void insert_first(ListNode **phead , ListNode* node)
{
 if(*phead==NULL){
  *phead=node;
  node->link=node;
 }
 else
 {
  node->link=(*phead)->link;
  (*phead)->link=node;
 }
}
void insert_last(ListNode **phead , ListNode* node)
{
 if(*phead==NULL){
  *phead=node;
  node->link=node;
 }
 else
 {
  node->link = (*phead)->link;
  (*phead)->link = node;
  *phead = node;
 }
}
void display(ListNode *head)
{
 ListNode *p;
 if( head == NULL) return;

 p = head;
 do{
  printf("%d->",p->data);
  p = p->link;
 
 }while(p != head);
 printf("\n");
}
main()
{
 ListNode *list1=NULL;

 insert_first(&list1,create_node(10,NULL));
 insert_first(&list1,create_node(20,NULL));
 insert_last(&list1, create_node(30,NULL));
 display(list1);
}

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,

#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;//(*L).length=0;
}

//리스트가 비어있으면 1을반환
//그렇지 않으면 0을 반환
int is_empty(ArrayListType* L)
{
 return L->length=0;
}

//리스트가 가득 차있으면 1을 반환
//그렇지 않으면 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]);
}
//position 삽입하고자 하는 위치
//item 삽입하고자하는자료
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++;
 }
}

//position 삭제하고자 하는 위치
//반환값 삭제되는 자료
element delete(ArrayListType* L, int position)
{
 int i;
 element item;

 if(position <0 || position >= L->length)
  error("오류");
 item=L->list[position];
 for(i=position ; i<(L->length-1) ; i++)
  L->list[i]=L->list[i+1];
 L->length--;
 return item;
}


main()
{
 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);
}


 

블로그 이미지

百見 이 不如一打 요 , 百打 가 不如一作 이다.

,