数据结构-单链表
一. 带head单链
1.1. 什么是head单链
1.2. 单链创建
listNode_t *CreatList(int listLen) { listNode_t *head = malloc(sizeof(listNode_t)); listNode_t *temp = head; temp->next = NULL; temp->data = 0; for (int i = 1; i < listLen; i++) { listNode_t *ptr = malloc(sizeof(listNode_t)); ptr->next = NULL; ptr->data = i; temp->next = ptr; temp = temp->next; } return head; }
1.3. 单链插入
listNode_t *InsertNode(listNode_t *head, int index, int data) { if (head == NULL) return NULL; listNode_t *node = head; for (int i = 0; i<index-1; i++) { if(NULL == node->next) { printf("%s|%d: can't find index=%d node\r\n", __func__, __LINE__, index); return NULL; } node = node->next; } listNode_t *newNode = CreatList(1); newNode->data = data; newNode->next = node->next; node->next = newNode; return head; }
1.4. 单链删除
listNode_t *DeleteNode(listNode_t *head, int index) { if (head == NULL) return NULL; listNode_t *node = head; listNode_t *delNode; for(int i=0;i<index-1;i++) { if(NULL==node->next) { printf("%s|%d: can't find index=%d node\r\n", __func__, __LINE__, index); return NULL; } node = node->next; } delNode = node->next; node->next=delNode->next; free(delNode); return head; }
1.5. 寻找中间节点
listNode_t *FindNodeMid(listNode_t *const head) { if (head == NULL) return NULL; listNode_t *fastNode=head; listNode_t *slowNode=head; while(fastNode->next!=NULL&&fastNode->next->next!=NULL) { fastNode=fastNode->next->next; slowNode=slowNode->next; } return slowNode; }
1.6. 完整示例
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> typedef struct listNode { int data; struct listNode *next; } listNode_t; listNode_t *CreatList(int listLen) { listNode_t *head = malloc(sizeof(listNode_t)); listNode_t *temp = head; temp->next = NULL; temp->data = 0; for (int i = 1; i < listLen; i++) { listNode_t *ptr = malloc(sizeof(listNode_t)); ptr->next = NULL; ptr->data = i; temp->next = ptr; temp = temp->next; } return head; } listNode_t *InsertNode(listNode_t *head, int index, int data) { if (head == NULL) return NULL; listNode_t *node = head; for (int i = 0; i<index-1; i++) { if(NULL == node->next) { printf("%s|%d: can't find index=%d node\r\n", __func__, __LINE__, index); return NULL; } node = node->next; } listNode_t *newNode = CreatList(1); newNode->data = data; newNode->next = node->next; node->next = newNode; return head; } listNode_t *DeleteNode(listNode_t *head, int index) { if (head == NULL) return NULL; listNode_t *node = head; listNode_t *delNode; for(int i=0;i<index-1;i++) { if(NULL==node->next) { printf("%s|%d: can't find index=%d node\r\n", __func__, __LINE__, index); return NULL; } node = node->next; } delNode = node->next; node->next=delNode->next; free(delNode); return head; } listNode_t *FindNodeMid(listNode_t *const head) { if (head == NULL) return NULL; listNode_t *fastNode=head; listNode_t *slowNode=head; while(fastNode->next!=NULL&&fastNode->next->next!=NULL) { fastNode=fastNode->next->next; slowNode=slowNode->next; } return slowNode; } static bool GetNode(listNode_t *head,int index, int *pData) { bool ret = false; if(NULL == head) return ret; listNode_t *node = head; for(int i=0;i!=index;i++) { if(NULL==node->next) { printf("%s|%d: can't find index=%d node\r\n", __func__, __LINE__, index); return false; } node = node->next; } *pData = node->data; ret=true; return ret; } bool selectNode(listNode_t *head, const int data,int *pIndex) { bool ret = false; if(NULL == head) return ret; int i=0; listNode_t *node = head; while(node->next!=NULL) { if(node->data == data) { *pIndex=i; ret=true; } node=node->next; } return ret; } int main(char argc, char *argv[]) { listNode_t *head = CreatList(5); listNode_t *midNode=NULL; InsertNode(head, 1, 6); DeleteNode(head, 1); int data; if(true == GetNode(head,1,&data)) printf("node data:%d\r\n",data); midNode = FindNodeMid(head); if(NULL != midNode) printf("mid node data:%d\r\n",midNode->data); return 0; }