l 0 add Lastlist a add Lastlist b
예제 l 다음 각 연산의 결과는 무엇인가? (항목의 위치는 0부터 시작 한다고 가정) add. Last(list, a); add. Last(list b); add(list, 2, c); add(list, 1, d); delete(list, 2); replace(list, 1, e): list (a) 6
예제 #2 main() { int i, n; List. Type list 2; // 리스트 생성 // 리스트 연산 수행 add. Last(list 2, "마요네즈”); add. Last(list 2, "빵”); add. Last(list 2, "치즈”); add. Last(list 2, "우유”); // 리스트 출력 display(list 2); n = get. Length(list 2); printf("쇼핑해야할 항목수는 %d입니다. n", n); for(i=0; i<n; i++) printf("%d항목은 %s입니다. ¡°, i, get. Item(list 2, i)); }
리스트 구현: 배열 l 리스트 표현 list … length typedef struct { int list[MAX_LIST_SIZE]; int length; } Array. List. Type; 리스트의 현재 크기 10
리스트 연산 l 삽입 연산 (알고리즘) typedef struct { int list[MAX_LIST_SIZE]; int length; } Array. List. Type; add(Array. List. Type list, int pos, int item) // pos 위치에 item을 삽입 { N 0 1 2 3 4 A B C D E A B C D A B C A B 5 // 배열이 꽉 차 있고, pos가 올바른 범위에 있는지 검사 E D E C D E } l N add. First(), add. Last()를 구현하라. 11
리스트 연산 l 삽입 연산 (C 코드) typedef struct { int list[MAX_LIST_SIZE]; int length; } Array. List. Type; add(Array. List. Type *list, int pos, int item) // pos 위치에 item을 삽입 { N 0 1 2 3 4 A B C D E A B C D A B C A B 5 E D E C D E } N 12
예제 typedef struct { int list[MAX_LIST_SIZE]; int length; } Array. List. Type; main() { Array. List. Type list // 리스트 생성 // Array. List. Type *list로 선언하면? // 리스트 연산 수행 init(&list); add. First(&list, 10); add(&list, 0, 20); add(&list, 0, 30); // 리스트 출력 display(list); } 16
단순 연결 리스트 표현 l (data, link)의 레코드로 표현 node data 자체 참조변수 link l SPARKS로 표현하면 type nodeptr = ↑node type node record data: integer; link: nodeptr; end; list: nodetpr; list↑. data <- 10; list↑. link <- NULL; 22
삽입연산 (1) head가 NULL인 경우 head NULL new_node (2) prev가 NULL인 경우 head new_node NULL 24
삽입연산 (3) prev가 NULL이 아닌 경우 prev head NULL new_node 25
- Slides: 28