干货2:头指针链表
来源:互联网 发布:sql的count 1 编辑:程序博客网 时间:2024/06/04 19:24
#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");}
阅读全文
0 0
- 干货2:头指针链表
- 干货1:头结点链表
- 头指针链表
- 头指针链表
- 链表的 头指针 头结点
- 链表之头指针
- 数据结构头指针链表
- 线性链表中的头结点和头指针
- 初学单向链表中的头结点与头指针
- 链表中的头指针和头节点
- c语言链表 头指针
- 头指针链表C程序
- 头指针链表实现机制
- C语言 头指针链表
- 双循环链表(包含头指针与尾指针)
- 顺序表,头结点链表,头指针链表,头结点循环链表
- 数据结构-线性表-头指针&头结点
- 数据结构-线性表-头指针&头结点
- 命令每天一小步--ls命令
- MyEclipse加载web工程Tomcat能跑,项目加载不出来
- 51nod 1276 岛屿的数量
- # 我也不知道对不对 # 迈阶梯
- TCP如何保证可靠性
- 干货2:头指针链表
- 字符串的最长回文子串
- java中的单例模式
- L2-015. 互评成绩
- 【笔记】编译报错error: cannot convert ‘main(int, char**)::sockadrr*’ to ‘const sockaddr*’ for
- 利用virtualenv给管理python的多环境.(MAC亲测,简单几步即可,指定python版本)
- 【HDU1233】还是畅通工程(最小生成树)
- 《零基础入门学习Python》学习过程笔记【32,33,34异常处理】(没看)
- 009讲文件