Linked list vs. Array

Array

장점 : random access
단점 : 삽입, 삭제

Linked list

장점 : 삽입, 삭제
단점 : sequential access
ex) 프로세스 관리

struct node {
 int value;
 struct node* next;
};






struct node* ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = 1;
ptr->next = (struct node*)malloc(sizeof(struct node));
ptr->next->data = 3;
ptr->next->next = (struct node*)malloc(sizeof(struct node));
ptr->next->next->data = 4;
ptr->next->next->next = NULL;

struct node* tmp = (struct node*)malloc(sizeof(struct node));
tmp->data = 2;
tmp->next = ptr->next;
ptr->next = tmp;







tmp = ptr->next->next;
ptr->next->next = tmp->next;
free(tmp);

struct node* p = ptr;
while(p ) {
 printf("%d%s", p->data, (p->next ) ? " -> " : "\n");
 p = p->next;
}

p = ptr;
while(p ) {
 Node* tmp = p;
 p = p->next;
 free(tmp);
}

Comments