C语言 头指针链表
来源:互联网 发布:mac如何安装阿里旺旺 编辑:程序博客网 时间:2024/06/05 02:40
链表是比较重要的一个点,头指针链表和头结点链表都是,但也是有不同之处,头指针链表的开头出是一个指针指向首节点,而头结点链表是有一个头结点指向首节点的,同时也是有个节点指向头节点的。
#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
- c语言链表 头指针
- C语言 头指针链表
- C语言 头指针单链表
- 头指针链表C程序
- c语言指针链表
- 头结点链表 C语言
- 头结点循环链表 C语言
- 头指针链表
- 头指针链表
- 链表的 头指针 头结点
- C语言指针-链表逆转
- C语言指针函数链表复习
- C语言指针和链表操作
- 链表之头指针
- 数据结构头指针链表
- C语言链表的实现(修改版)头文件
- C语言有头结点链表的经典实现
- 链表的归并(无头结点,c语言)
- 对公钥和私钥使用的理解
- MySQL触发器初探
- 问题 C: 数字排序问题
- C语言的关键字
- 火狐浏览器使用方法 使用扩展Stylish 实现浏览博客园的博文时,文字的背景颜色变为护眼绿
- C语言 头指针链表
- FTP协议
- 大话数据结构 code 第五章 02二叉树链式结构实现_BiTreeLink
- 自动化学生如何面对嵌入式学习
- poj3093 Margaritas on the River Walk(特殊的01背包)
- Awk命令简介
- 头结点链表 C语言
- 问题 D: 相邻数对问题
- python3线程启动与停止