本文是关于链表的一些操作(包括单链表和双向循环链表)
1、单链表,双链表的创建。
2、单链表和双链表的打印。
3、单链表的插入,删除。
4、双链表的插入和删除。
5、单链表的逆置。
6、单链表节点的个数。
7、单链表,双链表的查找。
函数源码:
1 //链表相关问题 2 3 typedef int DataType; 4 typedef struct LinkNode 单链表结构 5 { 6 struct LinkNode* next; 7 DataType data; 8 }LinkNode; 9 10 LinkNode *CreateNode(DataType x) 创建单链表节点 11 12 LinkNode *tmp = (LinkNode *)malloc(sizeof(LinkNode)); 13 if (NULL == tmp) 14 { 15 printf("分配内存失败!\n"); 16 return NULL; 17 } 18 tmp->next=NULL; 19 tmp->data=x; 20 tmp; 21 } 22 23 void PrintLinkList(LinkNode *phead) 单链表打印 24 25 while (phead) 26 27 printf(%d ",phead->data); 28 phead = phead->next; 29 30 printf(\n 31 32 33 void InsertLinkList(LinkNode **phead,LinkNode *pos,DataType x) 单链表插入 34 35 LinkNode *newNode,*tmp = *phead; 36 assert(phead); 37 if (NULL==*phead) 38 39 *phead = CreateNode(x); 40 ; 41 42 while(*phead != pos) 43 44 tmp = * 45 *phead = (*phead)-> 46 47 newNode = 48 newNode->next = tmp-> 49 tmp->next = newNode; 50 51 52 size_t ListNodeCount(LinkNode* phead) 计算单链表的节点数 53 54 size_t count = 0 55 56 57 count++ 58 phead = phead-> 59 60 count; 61 62 63 LinkNode *LinkListSearch(LinkNode *phead,1)">在单链表中查找一个数 64 65 (phead) 66 67 if (phead->data == x) 68 phead; 69 phead = phead-> 70 71 72 73 74 LinkNode *ReverseLinkList(LinkNode *phead) 单链表的逆置 75 76 LinkNode *first = 77 LinkNode *cur = first-> 78 first->next= 79 80 (cur) 81 82 LinkNode *tmp = cur-> 83 cur->next = first; 84 first = cur; 85 cur = 86 87 88 89 90 size_t RemoveLinkList(LinkNode **phead,LinkNode *pos) 单链表任意节点删除 91 92 LinkNode *first = * 93 (first) 94 95 if (*phead == pos) 删头节点 96 { 97 *phead = first-> 98 free(pos); 99 pos =100 return 1101 } 102 else if (first->next == pos) 非头节点情况 103 104 first->next = pos->105 106 pos =107 108 109 first = first->110 111 112 113 114 typedef struct DoubleLinkList 双链表结构 115 116 117 struct DoubleLinkList *prev; 118 119 }DoubleList; 120 121 DoubleList *CreateDoubleList(DataType x) 创建双链表节点 122 123 DoubleList *newNode = (DoubleList *)(DoubleList)); 124 assert(newNode); 125 newNode->next =126 newNode->prev =127 newNode->data = x; 128 129 130 131 void PrintDoubleList(DoubleList *phead) 打印双链表 132 133 DoubleList *tmp =134 (tmp) 135 136 printf(137 tmp = tmp->138 if (tmp == phead) 139 break140 141 printf(142 143 144 DoubleList *DoubleListSearch(DoubleList *phead,1)">双链表查找 145 146 DoubleList *tmp =147 148 149 150 151 if (tmp == phead->next) 152 153 phead = phead->154 155 156 157 158 void DoubleListInsert(DoubleList **phead,DataType x) 双链表的头插 159 160 DoubleList *tmp = (*phead); 161 DoubleList *newNode = CreateDoubleList(x); 162 163 if (NULL == *164 165 *phead =166 (*phead)->next = *167 (*phead)->prev = *168 169 else 170 171 newNode->next = (*phead)->172 (*phead)->next =173 newNode->prev = *174 newNode->next->prev =175 176 177 178 179 size_t RemoveDoubleListNode(DoubleList **phead,1)">删除双链表节点 180 181 DoubleList *tmp = *182 while (*183 184 if (tmp->data ==185 186 tmp->prev->next = tmp->187 tmp->next->prev = tmp->188 if (tmp->data == (*phead)->data) 189 *phead = tmp->190 if ((*phead)->next == *191 { 192 free(*193 *phead =194 } 195 (tmp); 196 tmp =197 198 199 if (*phead == tmp->200 201 tmp = tmp->202 203 204 }