Linked list
소 스 |
|
#include <stdio.h> #include <stdlib.h> #include <string.h>
typedef struct ListNode{ char data[10]; struct ListNode* link; } ListNode;
typedef struct{ ListNode* head; } linkedList_h;
linkedList_h* createLinkedList_h(void); void freeLinkedList_h(linkedList_h*); void addFirstNode(linkedList_h*, char*); void addMiddleNode(linkedList_h* L, char* x); void addLastNode(linkedList_h*, char*); void reverse(linkedList_h*); void deleteLastNode(linkedList_h*); void printList(linkedList_h*);
linkedList_h* createLinkedList_h(void){ linkedList_h* L; L = (linkedList_h*)malloc(sizeof(linkedList_h)); L -> head = NULL; return L; }
void addFirstNode(linkedList_h* L, char *x){ ListNode* newNode; ListNode* p; newNode = (ListNode*)malloc(sizeof(ListNode)); strcpy(newNode->data, x); if (L->head == NULL){ L->head = newNode; return; } p = L->head; L ->head = newNode; newNode->link = p; }
void addMiddleNode(linkedList_h* L, char* x){ ListNode* newNode; ListNode* tempNode; ListNode *temp; newNode = (ListNode*)malloc(sizeof(ListNode)); strcpy(newNode->data, x); if (L->head == NULL){ L->head = newNode; return; } tempNode = L->head; tempNode = tempNode->link; temp = tempNode->link; tempNode->link = newNode; newNode->link = temp; return; }
/* void addMiddleNode(linkedList_h* L, char* x, char* y){ ListNode* newNode; ListNode* tempNode; ListNode *temp; newNode = (ListNode*)malloc(sizeof(ListNode)); strcpy(newNode->data, x); if (L->head == NULL){ L->head = newNode; return; } printf("굿잡0\n"); tempNode = L->head; printf("%d, %d\n", L->head, tempNode->link); //if(tempNode->data == "일"){ if(tempNode){ printf("굿잡1%s1%s1\n", tempNode->data, newNode->data); if(tempNode->data == newNode->data) printf("된다\n"); else printf("안된다\n"); temp = tempNode->link; tempNode->link = newNode; newNode->link = temp; return; } else{ printf("굿잡2\n"); while(tempNode->data !=NULL){ tempNode = tempNode->link; printf("굿잡3 , tempNode = %d , %s \n",tempNode,tempNode->data); //if(tempNode->link == y){ printf("굿잡4\n"); temp = tempNode->link; tempNode->link = newNode; newNode->link = temp; return;
} } return; } */
void addLastNode(linkedList_h* L, char* x){ ListNode* newNode; ListNode* p; newNode = (ListNode*)malloc(sizeof(ListNode)); strcpy(newNode->data, x); newNode->link= NULL; if (L->head == NULL){ L->head = newNode; return; } p = L->head; while (p->link != NULL) { p = p->link; } p ->link = newNode; }
void reverse(linkedList_h * L){ ListNode* p; ListNode* q; ListNode* r;
p = L->head; q=NULL; r=NULL;
while (p!= NULL){ r = q; q = p; p = p->link; q->link = r; } L->head = q;
}
void deleteLastNode(linkedList_h * L){ ListNode* previous; ListNode* current; if (L->head == NULL) return; if (L->head->link == NULL) { free(L->head); L->head = NULL; return; } else { previous = L->head; current = L->head->link; while(current ->link != NULL){ previous = current; current = current->link; } free(current); previous->link = NULL; } }
void freeLinkedList_h(linkedList_h* L){ ListNode* p; while(L->head != NULL){ p = L->head; L->head = L->head->link; free(p); p=NULL; } }
void printList(linkedList_h* L){ ListNode* p; printf("L = ("); p= L->head; while(p != NULL){ printf("%s", p->data); p = p->link; if(p != NULL){ printf(", "); } } printf(") \n"); }
int main(){ linkedList_h* L; L = createLinkedList_h(); printf("(1) 공백리스트 생성하기! \n"); printList(L); getchar();
printf("(2) 리스트 마지막에 3개 노드 추가하기! \n"); addLastNode(L, "월"); addLastNode(L, "수"); addLastNode(L, "금"); printList(L); getchar();
printf("(3) 리스트 처음에 노드 추가하기 \n"); addFirstNode(L, "일"); printList(L); getchar();
printf("(4) 리스트 중간에 노드 추가하기 \n"); addMiddleNode(L, "화"); printList(L); getchar();
printf("(5) 리스트 마지막에 노드 한개 추가하기! \n"); addLastNode(L, "토"); printList(L); getchar();
printf("(6) 마지막 노드 삭제하기! \n"); deleteLastNode(L); printList(L); getchar();
printf("(7) 리스트 원소를 역순으로 변환하기! \n"); reverse(L); printList(L); getchar();
printf("(7) 리스트 공간을 해제하여, 공백리스트 상태로 만들기! \n"); freeLinkedList_h(L); printList(L);
getchar(); return 0; } |
'Book' 카테고리의 다른 글
[한빛미디어] 인터넷 프로그래밍 입문 13장 연습문제 (0) | 2009.05.29 |
---|---|
[한빛미디어] C로 배우는 쉬운 자료구조 - 5장 연습문제 (0) | 2009.05.08 |
C로 배우는 쉬운 자료구조 - 희소 행렬을 전치 행렬로 변환 (0) | 2009.05.08 |
[한빛미디어] 인터넷 프로그래밍 입문 12장 연습문제 (0) | 2009.05.08 |
[한빛미디어] 인터넷 프로그래밍 입문 11장 연습문제 (0) | 2009.05.08 |