数据结构头指针链表
来源:互联网 发布:思迅天店软件下载 编辑:程序博客网 时间:2024/06/04 23:21
头指针链表
头指针链表相较于头结点链表操作起来更繁琐。
以下是头指针链表的基本操作:
#include <stdio.h>#include <stdlib.h>#define TRUE 1#define FALSE 0typedef int LinkData; // 链表的数据类型typedef struct _node{LinkData data; // 链表的数据struct _node *next; // 指向链表下一个结点的指针}Node;// 链表的头插int Insert_Head(Node **h, LinkData data){if (h == NULL)return FALSE;// 创建新节点Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}// 给结点成员变量赋值node->data = data;node->next = *h;// 让新节点变为链表的第一个结点*h = node;return TRUE;}// 尾插int Insert_Last(Node **h, LinkData data){if (h == NULL){return FALSE;}// 创建新节点Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}// 给结点成员变量赋值node->data = data;node->next = NULL;// 找最后一个结点Node * tmp = *h; // 指向第一个结点if (tmp == NULL) // 空表{*h = node;}else{while (tmp->next){tmp = tmp->next;}tmp->next = node;}return TRUE;}// 在第 pos 个节点处插入数据,链表结点从1开始,没有第0个结点int Insert_Pos (Node** h, int pos, LinkData data){if (h == NULL || pos < 1)return FALSE;// 创建新节点Node* node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}// 给结点成员变量赋值node->data = data;// 空表的状态下,只能插入在第一个结点处if (*h == NULL) {if (pos != 1){printf ("当前为空表,无法在第 %d 结点处插入数据\n", pos);free(node);return FALSE;}node->next = NULL;*h = node;}else // 非空表,需要找到插入位置的前一个结点{if (pos == 1){node->next = *h;*h = node;}else{int i;Node *tmp = *h; // tmp 开始的时候指向第一个结点for (i = 0; i < pos-2; i++){if (tmp == NULL) // 如果 pos 太大,会造成越界break;tmp = tmp->next;}if (tmp == NULL){printf ("插入位置越界\n");free(node);return FALSE;}node->next = tmp->next;tmp->next = node;}}return TRUE;}int Delete_Pos(Node** h, int pos){if (h == NULL || *h == NULL || pos < 1)return FALSE;Node *tmp = *h;if (pos == 1){*h = tmp->next;free(tmp);}else{int i;for (i = 0; i < pos-2; i++){if (tmp->next == NULL) // 如果 pos 太大,会造成越界break;tmp = tmp->next;}if (tmp->next == NULL){printf ("删除位置越界\n");return FALSE;}Node* p = tmp->next;tmp->next = p->next;free(p);}return TRUE;}int Reverse_List(Node **h){// *h == NULL 空表 (*h)->next == NULL 只有一个元素if (h == NULL || *h == NULL || (*h)->next == NULL)return FALSE;Node *pre = *h;Node *cur = (*h)->next;Node *tmp;while (cur){tmp = cur->next;cur->next = pre;pre = cur;cur = tmp;}(*h)->next = NULL;*h = pre;return TRUE;}void Display(Node *h){if (h == NULL)return;int count = 0;while (h){if (count++ % 4 == 0)printf ("\n");printf ("%8d", h->data);h = h->next;}printf ("\n");}int main(){Node * head = NULL; // 指向链表第一个结点的指针(头指针)// 插入元素int i;for (i = 0; i < 10; i++){//Insert_Head(&head, i);Insert_Last(&head, i);}#if 0Insert_Pos(&head, 1, 1000);Insert_Pos(&head, 10, 2000);Insert_Pos(&head, 13, 3000);Insert_Pos(&head, 15, 3000);Insert_Pos(&head, 0, 1000);#endif//Delete_Pos(&head, 11);Display(head);Reverse_List(&head);Display(head);return 0;}
阅读全文
0 0
- 数据结构头指针链表
- 数据结构-线性表-头指针&头结点
- 数据结构-线性表-头指针&头结点
- 数据结构链表 --头指针链表复习
- 头指针链表
- 头指针链表
- 链表的 头指针 头结点
- 数据结构中的头结点和头指针
- 数据结构之链表(头指针链表的插入、删除、逆序)
- 数据结构之头指针链表的逆序、输出和指定位置的删除
- 数据结构——单链表头指针与头结点
- 链表之头指针
- 数据结构头结点链表
- 《数据结构》只设置尾指针而不设置头指针的讯循环链表的合并
- 数据结构之头指针链表de三种插入方式(头插法,尾插法,在pos处插入)
- 线性链表中的头结点和头指针
- 初学单向链表中的头结点与头指针
- 链表中的头指针和头节点
- bfs和dfs的特点
- linux网络编程--shut_down和close()函数的区别
- 总结移动端页面开发时需要注意的一些问题
- TensorFlow入门简单机器学习练习
- 特殊string格式转换
- 数据结构头指针链表
- Android Telephony分析(三)--- RILJ 详解
- webStorm编写jsp代码缩进不了问题
- 生成模型时出现意外错误
- Flex 布局教程:语法篇
- MFC 复制文件夹
- linux gprs模块 sim800a 芯片 ppp拨号上网
- PDF编辑技巧2:删除页面和添加页面
- 在学习PHP的过程中,什么时候才可以学数据结构和算法?