1. 头插法建立单链表

建立表,头结点后插入结点,删除结点,打印链表

#include <stdio.h> #include <stdlib.h> struct node { int data; struct node * next; }; //建立只含头结点的空链表 struct node * create_list() { struct node * head = NULL; head = (struct node *)malloc(sizeof(struct node)); if (NULL == head) { printf("memory out of use/n"); return NULL; } head->next = NULL; head->data = 0; return head; } //头插法建立链表 int insert_form_head(struct node * head, int num) { struct node * head_t = head->next; struct node * new_node = NULL; new_node = (struct node *)malloc(sizeof(struct node)); if (NULL == new_node) { printf("memory out of use/n"); return -1; } //将新结点插入到链表的最后 new_node->data = num; new_node->next = head_t; head->next = new_node; return 0; } //打印链表 int show_list(struct node * head) { struct node * temp; temp = head->next; while(temp) { printf("%d/n",temp->data); temp = temp->next; } return 0; } // 按值删除结点,头结点不被删除 int delete_node(struct node *head, int data) { //head_t 保存要删除结点的上一个结点 struct node * head_t = head; struct node * temp = NULL; if (head == NULL) { printf("delete node from empty list!/n"); return -1; } //查找删除的结点的前一个结点 //如果此处查找的是删除的结点,则需要另加一个指针保存删除结点的前一个指针 while(NULL != head_t->next) { if (data == head_t->next->data) break; head_t = head_t->next; } //如果要删除的结点不存在,直接返回 if (NULL==head_t->next) { printf("node not found/n"); return -1; } //删除操作 temp = head_t->next; head_t->next = head_t->next->next; free(temp); return 0; } int main(int argc, char* argv[]) { struct node * head; head = create_list(); if (NULL == head) printf("create_list error/n"); insert_form_head(head,123); insert_form_head(head,456); show_list(head); printf("delete once!/n"); delete_node(head,123); show_list(head); printf("delete second!/n"); delete_node(head,456); show_list(head); delete_node(head,0); show_list(head); }

头插法建立单链表 c相关推荐

  1. 数据结构__头插法建立单链表、尾插法建立单链表

    单链表定义.头插法建表.尾插法建表 一.单链表的定义   单链表是线性表的链式存储,是指通过一组任意的存储单元来存储线性表中的数据元素. 单链表结构定义为: 其中data为数据域,用来存放数据:nex ...

  2. C++ 头插法建立单链表,单链表原地逆置以及尾插法建立单链表

    #include <iostream> #include <stdlib.h> #include <cstdio> typedef int ElemType; us ...

  3. 头插法建立单链表educoder

    /*使用头插法建立单链表,并返回指向单链表的头结点的指针*/ Node *CreateAtHead(DataType a[],int n) {int i;/*********Begin******** ...

  4. 头插法建立单链表c语言6,2020-07-14(C语言)数据结构采用头插法建立单链表

    //采用头插法建立单链表 include include typedef struct LNode { int data; struct LNode *next; } LNode, *LinkList ...

  5. 头插法建立单链表(带头结点和不带头结点)

    头插法建立单链表(带头结点) #include <stdio.h> #include <stdlib.h> typedef struct LNode{        //定义单 ...

  6. C语言使用头插法建立单链表并输出

    使用头插法建立单链表并输出## 标题 # include<stdio.h>typedef struct node{int data;struct node *next; } Lnode;L ...

  7. 头插法建立单链表并实现输出

    题目:头插法建立单链表并实现输出. #include "stdio.h" #include "stdlib.h" #include "string.h ...

  8. 数据结构上机-尾、头插法建立单链表-单链表遍历C语言完整代码实现

    点击此处跳转视频链接:数据结构上机-尾.头插法建立单链表-单链表遍历C语言完整代码实现

  9. C语言实现头插法建立单链表

    首先要明确一点,利用头插法建立出来的单链表的输出都是逆序的(就是和你的输入顺序反着来的)然后就是要明确生成的新结点是一个个加在某个结点的前面的(这个结点不一定是头结点,下面的代码是插在p的前头),这就 ...

最新文章

  1. 自动化运维之SaltStack,批量安装httpd实战
  2. 佛弟子有三样东西需要永远保密!
  3. 2!=5 or 0在python中是否正确-python中的if判断语句
  4. python for selenium 数据驱动测试
  5. 给定数字的全部组合实现方式
  6. Linus中帮助命令man
  7. Java异常持久化,Log4j进行日志的数据库持久化,说SQL语法异常。
  8. 易生信Linux培训
  9. Debian下RPM包安装
  10. mysql修改默认的存储引擎
  11. 语音识别如何处理工作 语音识别功能三个处理阶段
  12. java4特征_java的四大基本特征
  13. Python常见陷阱
  14. idea配置阿里格式化模板+注释模板
  15. 模拟京东快递单号查询框
  16. 解决电脑蓝牙耳机默认音量过大的问题
  17. 百度富文本编辑器php设置图片上传,手把手教你百度富文本编辑器的相关配置包括图片上传(for jsp)...
  18. 攻防演练场景中的加密流量检测技术
  19. 3DMAX建模入门:美国队长的盾牌图文教程,过程炒鸡详细(上)
  20. 电脑卡住了怎么办,鼠标无法操作,那就试试快捷键吧

热门文章

  1. 11.1 WAN接入配置
  2. MyBatis四大对象
  3. BPF学习笔记(六)-- 使用bpf实现xdp的例子
  4. 查询网站收录的方式?怎样查询网站收录情况?
  5. 计算机应用词汇,计算机应用常用英语词汇 1
  6. 黑苹果忘记密码解决办法
  7. ipad iphone开发_如何在iPhone或iPad上“不信任”计算机
  8. 解决已安装模块无法import的问题
  9. 井字棋游戏(电脑先)
  10. 用智能手机让台式机连接无线