1 #include <stdio.h> 2 #include <stdbool.h> 3 #include <stdlib.h> 4 5 typedef struct node{ 6 struct node *previous; 7 int age; 8 struct node *next; 9 }Node; 10 11 //创建一个结点 12 Node *createNode(); 13 //初始化链表 14 void intial(Node *const pHead); 15 //是否继续 16 bool isContinue(); 17 //查找index对应的结点位置 18 Node *checkIndex(Node *pHead, int index); 19 //输出链表 20 void show(const Node *const pHead); 21 //插入结点 22 void insert(Node *pHead, int index, int element); 23 //删除结点 24 void delete(Node *pHead, int index); 25 26 int main(int argc, const char * argv[]) { 27 //定义一个指针 指向头结点(头结点不存储数据) 28 Node *pHead = NULL; 29 30 pHead = createNode(); 31 if (pHead == NULL) { 32 exit(EXIT_FAILURE); 33 } 34 35 intial(pHead); 36 37 show(pHead); 38 39 insert(pHead, 3, 99); 40 41 show(pHead); 42 43 delete(pHead, 3); 44 45 show(pHead); 46 47 return 0; 48 } 49 50 51 52 Node *createNode(){ 53 //创建一个新的结点 54 Node *pTemp = (Node *)malloc(sizeof(Node)); 55 if (pTemp == NULL) { 56 return NULL; 57 } 58 59 //赋初值 60 pTemp->previous = NULL; 61 pTemp->next = NULL; 62 63 //返回 64 return pTemp; 65 } 66 67 void intial(Node *const pHead){ 68 Node *pTail = pHead; 69 70 //添加保存数据的结点 71 while (1) { 72 Node *pTemp = createNode(); 73 if (pTemp == NULL) { 74 exit(EXIT_FAILURE); 75 } 76 //输入数据 77 printf("Please input age:"); 78 scanf("%d", &pTemp->age); 79 80 //判断需要添加的位置 81 if (pHead->next == NULL) { 82 //头指针指向第一个结点 83 pHead->next = pTemp; 84 85 //第一个结点的previous指针指向头结点 86 pTemp->previous = pHead; 87 88 //第一个结点的next指针指向头指针 89 pTemp->next = pHead; 90 91 //头指针的previous指向第一个结点 92 pHead->previous = pTemp; 93 94 //尾指针指向第一个结点 95 pTail = pTemp; 96 } else{ 97 //前面的结点已经存在了 98 //让尾结点的next指向新建的结点 99 pTail->next = pTemp; 100 101 //新建结点的previous指针指向尾结点 102 pTemp->previous = pTail; 103 104 //pTail指向新建的结点 105 pTail = pTemp; 106 107 //尾结点的next指针指向头结点 108 pTail->next = pHead; 109 110 //头结点的previous指针指向尾结点 111 pHead->previous = pTail; 112 } 113 //询问是否继续 114 if (isContinue() == false) { 115 break; 116 } 117 } 118 119 120 } 121 122 bool isContinue(){ 123 char option; 124 do { 125 getchar(); 126 printf("Is continued(y/n):"); 127 option = getchar(); 128 } while (option != ‘y‘ && option != ‘n‘); 129 130 if (option == ‘y‘) { 131 return true; 132 } else{ 133 return false; 134 } 135 } 136 137 void show(const Node *const pHead){ 138 Node *pTemp = pHead->next; 139 140 while (pTemp != NULL) { 141 printf("%d ", pTemp->age); 142 pTemp = pTemp->next; 143 144 if (pTemp == pHead) { 145 break; 146 } 147 } 148 printf("\n"); 149 } 150 151 Node *checkIndex(Node *pHead, int index){ 152 Node *pTemp = pHead->next; 153 154 while (index > 0) { 155 if (pTemp == pHead) { 156 //结点数少于index 157 break; 158 } else{ 159 pTemp = pTemp->next; 160 } 161 index --; 162 } 163 if (index > 0) { 164 return NULL; 165 } else{ 166 //让pTemp指向上一个结点(因为while循环中『指过了』) 167 pTemp = pTemp->previous; 168 return pTemp; 169 } 170 } 171 172 void insert(Node *pHead, int index, int element){ 173 //首先应该获取到index对应的结点地址 174 Node *pIndex = checkIndex(pHead, index); 175 176 if (pIndex == NULL) { 177 printf("相应的元素不存在\n"); 178 exit(EXIT_FAILURE); 179 } else{ 180 //在index前插入一个指针,并且为这个指针分配内存 181 Node *pNew = createNode(); 182 183 if (pNew == NULL) { 184 exit(EXIT_FAILURE); 185 } else{ 186 pNew->age = element; 187 188 //pNew的next指针指向pIndex 189 pNew->next = pIndex; 190 191 //pNew的previous指针指向pIndex前面的那个结点的地址 192 pNew->previous = pIndex->previous; 193 194 //pIndex前面的那个结点的next指针指向pNew 195 pIndex->previous->next = pNew; 196 197 //pIndex的previous指针指向pNew 198 pIndex->previous = pNew; 199 } 200 } 201 } 202 203 void delete(Node *pHead, int index){ 204 //获取到index对应的结点 205 Node *pIndex = checkIndex(pHead, index); 206 207 if (pIndex == NULL) { 208 exit(EXIT_FAILURE); 209 } else{ 210 //pIndex前面结点的next指针指向pIndex后面的结点 211 pIndex->previous->next = pIndex->next; 212 213 //pIndex后面结点的previous指针指向pIndex前面的结点 214 pIndex->next->previous = pIndex->previous; 215 216 //释放pIndex结点 217 free(pIndex); 218 } 219 }
时间: 2024-10-19 10:39:12