归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言📝
每一个裂缝都是为透出光而努力!
CDLinkList.h
- #pragma once
- #include
- #include
-
- typedef int DataType;
-
- typedef struct CDLNode
- {
- DataType data;
- struct CDLNode* prior;
- struct CDLNode* next;
- }CDLNode,*CDLinkList;
-
- void InitCDLinkList(CDLinkList* head);
-
- int InsertElem(CDLinkList head, int i, DataType e);
-
- int DeleteElem(CDLinkList head, int i, DataType* e);
-
- void InsertElemR(CDLinkList head, DataType e);
-
- void InsertElemF(CDLinkList head, DataType e);
CDLinkList.cpp
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"CDLinkList.h"
-
- void InitCDLinkList(CDLinkList* head)
- {
- (*head) = (CDLinkList)malloc(sizeof(CDLNode));
- (*head)->prior = (*head);
- (*head)->next = (*head);
- }
-
- int InsertElem(CDLinkList head, int i, DataType e)
- {
- CDLNode* pre, * p;
- int j;
- if (i <= 0)
- {
- printf("插入位置错误!\n");
- return -1;
- }
- pre = head;
- j = 0;
- while (pre->next != head && j-1)
- {
- pre = pre->next;
- j++;
- }
- if (j != i - 1)
- {
- printf("插入位置错误!\n");
- return 0;
- }
- p = (CDLNode*)malloc(sizeof(CDLNode));
- p->data = e;
- p->prior = pre;
- p->next = pre->next;
- pre->next->prior = p;
- pre->next = p;
- return 1;
- }
-
- int DeleteElem(CDLinkList head, int i, DataType* e)
- {
- CDLNode* p;
- int j;
- if (i <= 0)
- {
- printf("删除位置错误!\n");
- return -1;
- }
- p = head;
- j = 0;
- while (p->next != head && j < i)
- {
- p = p->next;
- j++;
- }
- if (j != i)
- {
- printf("删除位置错误!\n");
- return 0;
- }
- p->prior->next = p->next;
- p->next->prior = p->prior;
- *e = p->data;
- free(p);
- return 1;
- }
-
- void InsertElemR(CDLinkList head, DataType e)
- {
- CDLNode* pre, * p;
- pre = head;
- while (pre->next != head)
- {
- pre = pre->next;
- }
- p = (CDLNode*)malloc(sizeof(CDLNode));
- p->data = e;
- p->prior = pre;
- p->next = head;
- head->prior = p;
- pre->next = p;
- }
-
- void InsertElemF(CDLinkList head, DataType e)
- {
- CDLNode* p;
- p = (CDLNode*)malloc(sizeof(CDLNode));
- p->data = e;
- p->prior = head;
- p->next = head->next;
- head->next->prior = p;
- head->next = p;
- }