链表2
一. 单链链表
1.1. 单链链表的主体
a. 此处链表的主体包含数据和节点,与linux内核中链表的用法很不一样,详细可以查看内核中链表的使用
b. 链表节点中由真实数据和下个节点指针构成。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
struct node { int data; struct node *pNext; };
1.2. 链接的创建
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
struct node *creat_node(int data) { struct node *p = (struct node *)malloc(sizeof(struct node)); if(NULL == p) { printf("malloc fail\n"); return NULL; } p->data = data; p->pNext = NULL; return p; }
1.3. 从头插入新节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int insert_head(struct node *pHeader,struct node *newNode) { if(NULL == newNode || NULL==pHeader) { printf("point is empty\n"); return -1; } newNode->pNext = pHeader->pNext; pHeader->pNext = newNode; pHeader->data++; return 0; }
1.4. 遍历链表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
int loop_node(struct node *pHeader) { struct node *p = pHeader; if(NULL==pHeader) { printf("point is empty\n"); return -1; } while(NULL != p->pNext) { p = p->pNext; printf("node = %d\n",p->data); } return 0; }
1.5. 链表反转
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
void reverse_node(struct node *pHeader) { struct node *p = pHeader->pNext; struct node *pbackup = NULL; if(NULL == pHeader || NULL == p) { printf("node should more one\n"); return; } while(NULL != p) { pbackup = p->pNext; if(pHeader->pNext == p) { p->pNext = NULL; } else { p->pNext = pHeader->pNext; pHeader->pNext = p; } p = pbackup; } }
二. 实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h> #include <stdlib.h> #include <string.h> //#define DEBUG_VERSION #ifdef DEBUG_VERSION #define DEBUG_Printf(format) printf(format) #else #define DEBUG_Printf(format) #endif #define STR1(R) #R #define STR2(R) STR1(R) struct node { int data; struct node *pNext; }; struct node *creat_node(int data) { struct node *p = (struct node *)malloc(sizeof(struct node)); if(NULL == p) { printf("malloc fail\n"); return NULL; } p->data = data; p->pNext = NULL; return p; } int insert_head(struct node *pHeader,struct node *newNode) { if(NULL == newNode || NULL==pHeader) { printf("point is empty\n"); return -1; } newNode->pNext = pHeader->pNext; pHeader->pNext = newNode; pHeader->data++; return 0; } int loop_node(struct node *pHeader) { struct node *p = pHeader; if(NULL==pHeader) { printf("point is empty\n"); return -1; } while(NULL != p->pNext) { p = p->pNext; printf("node = %d\n",p->data); } return 0; } void reverse_node(struct node *pHeader) { struct node *p = pHeader->pNext; struct node *pbackup = NULL; if(NULL == pHeader || NULL == p) { printf("node should more one\n"); return; } while(NULL != p) { pbackup = p->pNext; if(pHeader->pNext == p) { p->pNext = NULL; } else { p->pNext = pHeader->pNext; pHeader->pNext = p; } p = pbackup; } } int main(void) { struct node *pHeader = creat_node(0); insert_head(pHeader,creat_node(123)); insert_head(pHeader,creat_node(5)); insert_head(pHeader,creat_node(3)); #ifdef DEBUG_VERSION printf("len =%d\n",strlen(STR2(DEBUG_Printf()))); #endif loop_node(pHeader); reverse_node(pHeader); printf("reverse_node:\n"); loop_node(pHeader); return 0; }