728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <Windows.h> struct Node { int data; struct Node* next; }; int Insert(struct Node*root, int position); int Delete(struct Node*root, int position); int SetData(struct Node*root, int position, int data); int GetData(struct Node*root, int position, int* data); void print(struct Node* root); #define ASSERT(exp) if(!(exp)){puts("Err\n");} void main(void) { struct Node* root =(struct Node*)calloc(1,sizeof(struct Node)); root->next = NULL; int r,n; //0번위치에 10개 삽입 for(int i=0;i<10;i++) { r = Insert(root, 0);ASSERT(r); r = SetData(root,0,i+1);ASSERT(r); } print(root); //삽입확인 for(int i=0;i<10;i++) { r = GetData(root,i,&n);ASSERT(r); ASSERT(n == 10 - i); } print(root); //모두삭제 for(int i=0;i<10;i++) { r = Delete(root, 0);ASSERT(r); } print(root); //마지막위치에 10개삽입 for(int i=0;i<10;i++) { r = Insert(root,i);ASSERT(r); r = SetData(root,i,i+1);ASSERT(r); } print(root); //짝수번째모두삭제 for(int i=10-1;i>=0;i-=2) { r = Delete(root, i);ASSERT(r); } print(root); //짝수번째다시삽입 for(int i=1;i<10;i+=2) { r = Insert(root, i);ASSERT(r); r = SetData(root,i,i+1);ASSERT(r); } print(root); //복원확인 for(int i=0;i<10;i++) { r = GetData(root,i,&n);ASSERT(r); ASSERT(n==i+1); } print(root); //에러확인 for(int i=10;i<20;i++) { r = GetData(root,i,&n);ASSERT(!r); } print(root); //역순으로삭제 for(int i=10-1;i>=0;i--) { r = Delete(root, i);ASSERT(r); } print(root); ASSERT(root->next == NULL); //delete root; free(root); root = NULL; //좋은습관 } int Insert(struct Node*root, int position) { struct Node* p_Cur = root; struct Node* p_New = (struct Node*)calloc(1,sizeof(struct Node*)); if(!(p_New)) return 0; p_New->next = NULL; p_New->data = NULL; if(position == 0) { p_New->next = root->next; root->next = p_New; }else { for(int i = 0; i < position; i++) { if(p_Cur->next == NULL) return 0; p_Cur = p_Cur->next; } p_New->next = p_Cur->next; p_Cur->next = p_New; } return 1; } int SetData(struct Node*root, int position, int data) { struct Node* p_Cur = root->next; for(int i = 0; i < position; i++) { p_Cur = p_Cur->next; } (*p_Cur).data = data; if((*p_Cur).data) return 1; else return 0; } int GetData(struct Node*root, int position, int* data) { struct Node* p_Cur = root->next; for(int i = 0; i < position; i++) { if(p_Cur->next == NULL) return 0; p_Cur = p_Cur->next; } *data = p_Cur->data; return 1; } int Delete(struct Node*root, int position) { struct Node* p_Cur = root; struct Node* p_Con; for(int i = 0; i < position; i++) { if(p_Cur->next == NULL) return 0; p_Cur = p_Cur->next; } if(p_Cur->next != NULL) p_Con = p_Cur->next->next; else p_Con = NULL; //free(p_Cur->next); p_Cur->next = p_Con; return 1; } void print(struct Node* root) { printf("\n"); struct Node* cur; cur = root->next; while(cur!=NULL) { printf("%d ",cur->data); cur=cur->next; } Sleep(1000); } | cs |
728x90
728x90
'Dev > 알고리즘-자료구조' 카테고리의 다른 글
원형 연결 리스트 N개의 리스트에서 K번째 삭제하기 - [C] [자료구조] 구현하기(Circular Linked List) (0) | 2017.12.21 |
---|---|
[개탱][C][컴공과제][희소행렬의 합구하기][파일 입출력][구조체] (0) | 2017.12.21 |
[개탱][C언어][하노이탑][간단한 알고리즘][재귀][Stack][스택][그래픽화][그래픽][도형] (0) | 2017.12.21 |
[개탱][C언어][하노이탑][간단한 알고리즘][재귀호출][재귀함수][Stack][스택] (0) | 2017.12.21 |
[개탱][C언어][하노이탑][간단한 알고리즘][비재귀][Stack][스택][while] (0) | 2017.12.21 |