Lab 7 1 List define MAXLISTSIZE 100 typedef
Lab 7 -1: 리스트 - 배열 � 배열을 이용한 List #define MAX_LIST_SIZE 100 // 배열의 최대크기 typedef int element; typedef struct { int list[MAX_LIST_SIZE]; // 배열 정의 int length; // 현재 배열에 저장된 자료들의 개수 } Array. List. Type; � 리스트 함수 void init(Array. List. Type *L) int is_empty(Array. List. Type *L) int is_full(Array. List. Type *L) void display(Array. List. Type *L) void add(Array. List. Type *L, int position, element item) element delete_list(Array. List. Type *L, int position) int search(Array. List. Type *L, element data) //완성할것 프로그래밍랩 – 7 리스트 2
Lab 7 -2: 연결 리스트 � 단순 연결 리스트 사용 #define FALSE 0 #define TRUE 1 typedef int element; typedef struct List. Node { element data; struct List. Node *link; } List. Node; typedef struct { List. Node *head // 헤드 포인터 int length; // 노드의 개수 } List. Type; � � � � void insert_node(List. Node **phead, List. Node *p, List. Node *new_node) void remove_node(List. Node **phead, List. Node *p, List. Node *removed) void init(List. Type *list) List. Node *get_node_at(List. Type *list, int pos) int get_length(List. Type *list) void add(List. Type *list, int position, element data) void add_last(List. Type *list, element data) void add_first(List. Type *list, element data) int is_empty(List. Type *list) void Delete(List. Type *list, int pos) element get_entry(List. Type *list, int pos) void clear(List. Type *list) void display(List. Type *list) int is_in_list(List. Type *list, element item) List. Node *search(List. Type *list, element data, int *pos) ◦ 찾으면 List. Node 못 찾으면 NULL return // 구현할것 프로그래밍랩 – 7 리스트 4
Lab 7 -3: 문자열 데이터 리스트 � Node에 문자열을 Data로 보관 typedef char * element; typedef struct List. Node { element data; struct List. Node *link; } List. Node; typedef struct { List. Node *head; // 헤드 포인터 int length; // 노드의 개수 } List. Type; � 함수들 ◦ void add_tail(List. Type *list, element item) ◦ ◦ � node = (List. Node *)malloc(sizeof(List. Node)); � node->data = (element)malloc(strlen(item)+1); // 문자열 저장 void display(List. Type *list) int get_length(List. Type *list) element get_entry(List. Type *list, int n) List. Node *search(List. Type *list, element item, int *pos) � 찾으면 Pointer 를 return 하고 못 찾으면 NULL return 프로그래밍랩 – 7 리스트 6
// Lab 7 -3 문자열을 data로 갖는 리스트 linked list #include <stdio. h> #include <string. h> #include <stdlib. h> #define MAXITEM 100 typedef char * element; typedef struct List. Node { element data; struct List. Node *link; } List. Node; typedef struct { List. Node *head; // 헤드 포인터 int length; // 노드의 개수 } List. Type; void add_tail(List. Type *list, element item) { } void display(List. Type *list) { } int get_length(List. Type *list) { } element get_entry(List. Type *list, int n) { } // 찾아서 Node를 return, 없으면 NULLt List. Node *search(List. Type *list, element item) { } // 리스트를 초기화한다. void init(List. Type *list) { if( list == NULL ) return; list->length = 0; list->head = NULL; } main() { int i, n; List. Type list; List. Node *node; char item[100]; init(&list); add_tail(&list, "마요네즈"); add_tail(&list, "빵"); add_tail(&list, "치즈"); add_tail(&list, "우유"); n = get_length(&list); printf("쇼핑해야할 항목수는 %d입니다. n", n); display(&list); for(i=0; i<n; i++) printf("%d항목은 %s입니다n", i, get_entry(&list 2, i)); while (1) { } } int pos; printf("찾을 항목 (종료quit) : "); scanf("%s", data); if (strcmp(data, "quit")==0) break; node = search(&list, data, &pos); if (node!=NULL) printf("[%d: %s]n", pos, node->data); else printf("Not Found. !n");
Lab 7 -4: 이중 연결 리스트 � � left link 와 right link 사용 typedef int element; typedef struct Dlist. Node { element data; struct Dlist. Node *llink; struct Dlist. Node *rlink; } Dlist. Node; 함수들 ◦ void init(Dlist. Node *phead) ◦ void display(Dlist. Node *phead) ◦ void dinsert_node(Dlist. Node *before, Dlist. Node *new_node) ◦ void dremove_node(Dlist. Node *phead_node, ◦ Dlist. Node *removed) ◦ Dlist. Node *search(Dlist. Node *phead_node, element data, int *pos) �찾으면 Dlist. Node pointer 못 찾으면 NULL return // 구현할것 프로그래밍랩 – 7 리스트 9
- Slides: 12