单链表使用的相关函数
#include <stdio.h> #include <stdlib.h> typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedef int DataType; //类型定义 typedef struct node { DataType data; struct node* next; } LinkedNode; /*创建LinkedList*/ LinkedNode *CreateLinkedList(void) { LinkedNode *ll = (LinkedNode *)malloc(sizeof(LinkedNode)); memset((uint8 *)&(ll->data), 0x00, sizeof(DataType)); ll->next = NULL; return ll; } /*在末尾加节点*/ int AppendNodeLinkedList(LinkedNode *head, DataType dt) { if(NULL == head) return -1; LinkedNode *curr_node = head; LinkedNode *new_node = NULL; while(curr_node->next != NULL) { curr_node = curr_node->next; } new_node = (LinkedNode *)malloc(sizeof(LinkedNode)); memcpy((uint8 *)&(new_node->data), (uint8 *)&dt, sizeof(LinkedNode)); new_node->next = NULL; curr_node->next = new_node; return 1; } /*正顺遍历所有节点*/ void VisitAllNode(LinkedNode *head) { if(NULL == head) return; LinkedNode *currNode = head; printf("["); while(NULL != currNode) { printf("%d",currNode->data); currNode = currNode->next; if(NULL != currNode) printf(","); } printf("]\n"); } /*逆置整个链表*/ int ReverseLinkedList(LinkedNode **head) { if(NULL == (*head)) return -1;//头指针为空 LinkedNode *pre_node = *head; if(NULL == pre_node->next) return -2;//单节点链表不需要逆置 LinkedNode *curr_node = pre_node->next; if(NULL == curr_node->next) { curr_node->next = pre_node; pre_node->next = NULL; *head = curr_node; return 1;//两个节点的链表,两两交换即可 } //多于三个节点的链表 LinkedNode *next_node = curr_node->next; pre_node->next = NULL;//头节点变尾节点 while(NULL != next_node) { curr_node->next = pre_node; pre_node = curr_node; curr_node = next_node; next_node = next_node->next; } curr_node->next = pre_node; *head = curr_node; return 2; } /*删除pos上的节点*/ int DelNodeLinkedList(LinkedNode **head, int pos) { if(NULL == (*head)) return -1; LinkedNode *pre_node = *head; LinkedNode *curr_node = pre_node->next; LinkedNode *next_node = curr_node->next; if(NULL == curr_node && 0 == pos) { memset((uint8 *)&(pre_node->data),0x00, sizeof(DataType)); free(pre_node); pre_node = NULL; *head = pre_node; return 1; } if(NULL != curr_node && NULL == next_node && 1 == pos) { memset((uint8 *)&(curr_node->data),0x00, sizeof(DataType)); free(curr_node); curr_node = NULL; pre_node->next = NULL; return 2; } while(NULL != curr_node) { next_node = curr_node->next; if(pre_node == *head && 0 == pos) { memset((uint8 *)&(pre_node->data),0x00, sizeof(DataType)); free(pre_node); pre_node = NULL; *head = curr_node; return 3; } if(1 == pos) { pre_node->next = next_node; memset((uint8 *)&(curr_node->data),0x00, sizeof(DataType)); free(curr_node); curr_node = NULL; return 4; } pos--; pre_node = curr_node; curr_node = curr_node->next; } return 5; } /*取得LinkedList的长度*/ int GetLenLinkedList(const LinkedNode *head) { if(NULL == head) return 0; int len = 0; const LinkedNode *curr_node = head; while(NULL != curr_node) { curr_node = curr_node->next; len++; } return len; } /*取得pos位置上的节点*/ LinkedNode *GetNodeLinkedList(LinkedNode *head, int pos) { if(NULL == head) return NULL; LinkedNode *curr_node = head; while(NULL != curr_node && pos != 0) { curr_node = curr_node->next; pos--; } return curr_node; } /*在pos位置上设置值*/ int SetNodeLinkedList(const LinkedNode *head, int pos, DataType dt) { if(NULL == head) return -1; const LinkedNode *curr_node = head; while(NULL != curr_node && pos != 0) { curr_node = curr_node->next; pos--; } memcpy((uint8 *)&(curr_node->data), (uint8 *)&dt, sizeof(DataType)); return 1; } /*用于排序函数的规则制定*/ int GetSortRule(DataType dt1, DataType dt2) { if(dt1 > dt2) return 1; else if(dt1 < dt2) return -1; else return 0; } /* 对链表排序 从小到大,GetSortRule:dt1>dt2,ret=1; dt1<dt2,ret=-1; dt1==dt2,ret=0; 从大到小,GetSortRule:dt1>dt2,ret=-1; dt1<dt2,ret=1; dt1==dt2,ret=0; */ int SortLinkedList(LinkedNode *head, int (*GetSortRule)(DataType dt1, DataType dt2)) { if(NULL == head) return -1; LinkedNode *pre_node = head; LinkedNode *curr_node = head; LinkedNode *out_node = head; while(NULL != out_node) { while(NULL != curr_node) { if((*GetSortRule)(pre_node->data, curr_node->data) > 0) { DataType tmp; memcpy((uint8 *)&(tmp), (uint8 *)&(pre_node->data), sizeof(DataType)); memcpy((uint8 *)&(pre_node->data), (uint8 *)&(curr_node->data), sizeof(DataType)); memcpy((uint8 *)&(curr_node->data), (uint8 *)&(tmp), sizeof(DataType)); } pre_node = curr_node; curr_node = curr_node->next; } out_node = out_node->next; pre_node = head; curr_node = head; } return 0; } /*类memcpy操作,连接新链*/ int CpyLinkedList(LinkedNode **dest_head, LinkedNode *src_head) { if(NULL == (*dest_head) || NULL == src_head) return -1; LinkedNode *dest_curr_node = *dest_head; LinkedNode *dest_head_node = *dest_head; LinkedNode *src_curr_node = src_head; LinkedNode *src_head_node = src_head; while(NULL != src_curr_node->next) { src_curr_node = src_curr_node->next; } if(NULL == dest_head_node->next) { dest_head_node->next = src_head_node; *dest_head = dest_head_node; return 1; } dest_curr_node = dest_curr_node->next; dest_head_node->next = src_head_node; src_curr_node->next = dest_curr_node; *dest_head = dest_head_node; return 2; } /*逆向遍历(效率较低)*/ int ReVisitAllNode(LinkedNode *head) { if(NULL == head) return -1; LinkedNode *curr_node = head; LinkedNode *out_node = head; int len = GetLenLinkedList(curr_node); int i = 0; printf("["); for(i = len-1; i >= 0; i--) { curr_node = head; out_node = GetNodeLinkedList(curr_node, i); printf("%d",out_node->data); if(out_node != head) printf(","); } printf("]\n"); return 1; } /*单链表成环*/ int LinkLinkedList(LinkedNode *head) { if(NULL == head) return -1; LinkedNode *curr_node = head; while(NULL != curr_node->next) { curr_node = curr_node->next; } curr_node->next = head; return 1; } /*单链表解环*/ int UnLinkLinkedList(LinkedNode *head) { if(NULL == head) return -1; LinkedNode *curr_node = head; while(head != curr_node->next) { curr_node = curr_node->next; } curr_node->next = NULL; return 1; } int main() { LinkedNode *pln = CreateLinkedList(); AppendNodeLinkedList(pln, 11); AppendNodeLinkedList(pln, 22); AppendNodeLinkedList(pln, 33); printf("normal ord:"); VisitAllNode(pln); printf("reverse1:"); ReverseLinkedList(&pln); VisitAllNode(pln); printf("del:"); DelNodeLinkedList(&pln, 0); VisitAllNode(pln); printf("LinkedList Len = %d\n",GetLenLinkedList(pln)); printf("LinkedList[%d].data = %d\n",2, GetNodeLinkedList(pln, 2)->data); SetNodeLinkedList(pln, 1, 99); printf("LinkedList[%d].data = %d\n",1, GetNodeLinkedList(pln, 1)->data); printf("del:"); VisitAllNode(pln); printf("sort:"); SortLinkedList(pln, GetSortRule); VisitAllNode(pln); LinkedNode *pln2 = CreateLinkedList(); AppendNodeLinkedList(pln2, 66); AppendNodeLinkedList(pln2, 77); //插入新链 LinkedNode *tmp = GetNodeLinkedList(pln, 1); CpyLinkedList(&tmp, pln2); tmp = NULL; printf("new linkedlist:"); VisitAllNode(pln); printf("re-visit linkedlist:"); ReVisitAllNode(pln); LinkLinkedList(pln); //VisitAllNode(pln); UnLinkLinkedList(pln); VisitAllNode(pln); return 0; }