반응형
#include#include //******************************************************************* // 단일 연결리스트 구현 - head노드만 !! //******************************************************************* struct Node { int Data; // 데이타 저장소 Node * next; // 다음 노드를 가르킬 포인터 }; //******************************************************************* Node * head; //******************************************************************* // CreateNode : 노드 생성 //******************************************************************* Node * CreateNode( int data) { Node * NewNode = (Node*)malloc(sizeof(Node)); NewNode->Data = data; NewNode->next =NULL; return NewNode; } //******************************************************************* // init //******************************************************************* void Init() { head = CreateNode(0); head->next = NULL; } //******************************************************************* void push_front(int data) { Node * NewNode = CreateNode(data); if( head->next == NULL) { head->next = NewNode; } else { NewNode->next = head->next; head->next = NewNode; } } //******************************************************************* void push_back(int data) { Node * NewNode = CreateNode(data); Node * p = head; while( p->next != NULL) { p = p->next; } NewNode->next = p->next; p->next = NewNode; } //******************************************************************* void Insert(int key, int data) { Node * NewNode = CreateNode(data); Node * p = head->next; while( p != NULL) { if( p->Data == key) { NewNode->next = p->next; p->next = NewNode; return; } else { p = p->next; } } free(NewNode); push_back(data); } //----------------------------------------------------------------- // 맨뒤에서 삭제 //----------------------------------------------------------------- void pop_back() { Node * p, *s; if( head->next == NULL) { puts("list Empty ~ !!"); return; } else { p = head->next; s = head; while( p->next != NULL) { s = p; p = p->next; } s->next = p->next; free(p); } } //----------------------------------------------------------------- //----------------------------------------------------------------- // 중간에서 삭제 !! //----------------------------------------------------------------- void erase( int key) { Node * p, *s; if( head->next == NULL) { puts("list empty !!"); } else { s = head; p = head->next; while( p != NULL) { if( p->Data == key) { s->next = p->next; free(p); return; } else { s = p; p = p->next; } } } } // 전체 삭제 void clear() { Node * p, * s; if(head->next ==NULL) { puts("List Empty~!!"); free(head); puts("노드 없었던거.Disappeared !!"); return; } else { while(head->next!= NULL) { p = head->next; s= head; while(p->next!=NULL) { s=p; p=p->next; } s->next = p->next; free(p); } free(head); puts("노드 있었던거.Disappeared !!"); return; } } //to.위에 코드 짜신분 // - 이건 어때여 //void clear() //{ // while(head->next != NULL) // { // pop_back(); // } // printf("clear!"); //} // 전체 삭제 !! //void clear() // 전체삭제 //{ // Node *p = head->next; // while(p!=NULL) // { // head->next = p->next; // free(p); // p = head->next; // } //} void pop_front() { Node * p; if( head->next == NULL) { puts("list empty !! "); } else { p = head->next; head->next = p->next; free(p); } } void Show() { Node * p = head->next; while( p != NULL) { printf("%d --> " , p->Data ); p = p ->next; } puts(""); //개행 } //******************************************************************* void main() { Init(); push_front(10); push_front(20); push_front(30); Show(); pop_front(); Show(); pop_front(); Show(); pop_front(); Show(); clear(); }
'자료구조' 카테고리의 다른 글
[자료구조] 이중 연결 리스트 (0) | 2014.12.01 |
---|---|
[자료구조] 연결 리스트 (0) | 2014.12.01 |
[자료구조] vector (0) | 2014.12.01 |
[자료구조] stack (0) | 2014.12.01 |
[자료구조] set.map (0) | 2014.12.01 |