/* singlyLinkedList.c */
/* 单链表 */
/* 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。 */ #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h> /* 节点结构 */
/* head
————————————————
| value | next | -> ...
————————————————
*/
typedef struct node {
int value; /* 节点的值 */
struct node *next; /* 指向下一个节 */
} Node; /* 链表函数声明 */
void interface(void);
void addNode(Node **head, int number);
int findNode(Node *head, int number);
bool deleteNode(Node **head, int number);
void traverseNodes(Node *head);
int lengthNodes(Node *head);
bool clearNodes(Node **head); /* 程序主函数入口 */
int main(int argc, char *args[]){
/* head为指向链表头部节点的指针 */
Node *head = NULL;
int flag, number; interface();
for(;;){
printf("Command: ");
scanf("%d", &flag);
switch(flag){
case :
printf("Bye!\n");
return ;
case :
printf("Enter numbers(0 to stop): ");
for(;;){
scanf("%d", &number);
if(number==)
break;
addNode(&head, number);
}
break;
case :
printf("Enter the node value: ");
scanf("%d", &number);
printf("index: %d\n", findNode(head, number));
break;
case :
printf("Enter the node value: ");
scanf("%d", &number);
if(deleteNode(&head, number))
printf("Successfully deleted node %d\n", number);
else
printf("Failed to locate node %d\n", number);
break;
case :
traverseNodes(head);
break;
case :
if(clearNodes(&head))
printf("Done!\n");
break;
case :
printf("length: %d\n", lengthNodes(head));
break;
}
} return ;
} /* 用户界面 */
void interface(void){
puts("+*************************************************+");
puts("+ 0, quit 退出 +");
puts("+ 1, add nodes 添加 +");
puts("+ 2, find node 查找 +");
puts("+ 3, delete node 删除 +");
puts("+ 4, traverse nodes 遍历 +");
puts("+ 5, clear nodes 清除 +");
puts("+ 6, length 长度 +");
puts("+*************************************************+");
} /* 链表函数 */
/* 添加节点 */
/* 注意:函数需要修改到head,需要将head的地址传入 */
void addNode(Node **head, int number){
/* 使用malloc动态分配一个新节点 */
Node *node = (Node*)malloc(sizeof(Node));
node->value = number;
node->next = NULL; Node *last = *head;
if(!last){
/* 如果head为空,说明链表为空,将head指向node节点 */
*head = node;
}else{
/* 找到链表尾部,将last节点结构的next属性执行node节点 */
while(last->next){
last = last->next;
}
last->next = node;
}
}
/* 根据值查找节点 */
int findNode(Node *head, int number){
int index = ;
for(Node *p=head; p; p=p->next, index++){
if(p->value==number)
return index;
}
return -;
}
/* 根据值删除节点 */
bool deleteNode(Node **head, int number){
/* 如果要删除第一个节点,需要改变head指向第二个节点并free第一个节点 */
if(number==){
Node *first = *head;
Node *second = first->next;
free(first);
*head = second;
return true;
}else{
/* p指向当前节点,f指向p之前的节点 */
Node *f = NULL;
for(Node *p=*head; p; p=p->next){
if(p->value==number){
f->next = p->next;
free(p);
return true;
}
f = p;
}
}
return false;
}
/* 遍历链表 */
void traverseNodes(Node *head){
/* 遍历链表,从头到尾,遇到节点的next为NUll,则表明到了链表尾部 */
for(Node *p=head; p; p=p->next){
printf("%d -> ", p->value);
}
printf("0\n");
}
/* 清空链表 */
bool clearNodes(Node **head){
/* 遍历链表,清空节点 */
Node *q = NULL;
for(Node *p=*head; p; p=q){
q = p->next;
free(p);
}
*head = NULL;
return true;
}
/* 链表长度 */
int lengthNodes(Node *head){
int count = ;
for(Node *p = head; p; p=p->next)
count++;
return count;
}

数据结构——单链表(singly linked list)的更多相关文章

  1. python实现数据结构单链表

    #python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...

  2. C语言数据结构-单链表的实现-初始化、销毁、长度、查找、前驱、后继、插入、删除、显示操作

    1.数据结构-单链表的实现-C语言 typedef struct LNode { int data; struct LNode* next; } LNode,*LinkList; //这两者等价.Li ...

  3. 数据结构——单链表java简易实现

    巩固数据结构 单链表java实现 单链表除了表尾 每个几点都有一个后继 结点有数据和后继指针组成  通过构建表头和表尾(尾部追加需要)两个特殊几点 实现单链表的一些操作,代码如下 package co ...

  4. java数据结构——单链表、双端链表、双向链表(Linked List)

    1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...

  5. 数据结构—单链表(类C语言描写叙述)

    单链表 1.链接存储方法 链接方式存储的线性表简称为链表(Linked List). 链表的详细存储表示为: ① 用一组随意的存储单元来存放线性表的结点(这组存储单元既能够是连续的.也能够是不连续的) ...

  6. 数据结构之链表(Linked list)

    1, 无序链表(Unordered linked list) 链表是有若干个数据节点依次链接成的数据结构,如下图所示,每一个数据节点包括包括数据和一个指向下一节点的指针.(python中的list就是 ...

  7. Java数据结构之链表(Linked List)

    1.链表(Linked List)介绍 链表是有序的列表,但是它在内存存储结构如下: 2.特点: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节点. 链 ...

  8. C# 数据结构--单链表

    什么是单链表 这两天看到很多有关单链表的面试题,对单链表都不知道是啥的我.经过学习和整理来分享一下啥是单链表和单链表的一些基本使用方法.最后看些网上有关单链表的面试题代码实例. 啥是单链表? 单链表是 ...

  9. 数据结构-------单链表(C++)

    相关信息: /** * @subject 数据结构 实验2 * @author 信管1142班 201411671210 赖俊杰 * @project 单链表 * @time 2015年10月29日1 ...

随机推荐

  1. http响应总结:常见http响应错误总结

    工作中经常会被同事问这个http请求为什么调不通,我虽然能解释清楚错误是什么,但是没有总结过,想到刚开始时,也是看了别人的文章才会的,所以总结一下,贡献一下自己的经验. http 404 响应 404 ...

  2. c#小知识点总结

    实例化实例化就是将抽象变为具体,只说猫是抽象的,但是我要具体到一只单独的老猫A,那么这只猫被实例化.实例化就是一个抽象变具体的过程,也可以说为声明一个变量声明变量. int a=1,这其实也是一个实例 ...

  3. 快捷键-Visual Studio Code快捷键

    Shift+Enter 在Python终端中运行选定内容/行 C

  4. SQL索引管理器 - 用于SQL Server和Azure上的索引维护的免费GUI工具

    我作为SQL Server DBA工作了8年多,管理和优化服务器的性能.在我的空闲时间,我想为宇宙和我的同事做一些有用的事情.这就是我们最终为SQL Server和Azure 提供免费索引维护工具的方 ...

  5. [Leetcode] 5279. Subtract the Product and Sum of Digits of an Integer

    class Solution { public int subtractProductAndSum(int n) { int productResult = 1; int sumResult = 0; ...

  6. Newtonsoft.Json使用技巧

    本篇将为大家介绍Newtonsoft.Json的一些高级用法,可以修改很少的代码解决上述问题. 阅读目录 Newtonsoft.Json介绍 基本用法 高级用法 总结 回到顶部 Newtonsoft. ...

  7. tengine 基于权重负载均衡的简单配置

    环境如下: 资源服务器_1:192.168.10.10  centos 7  tengine 2.3.0 资源服务器_2:192.168.10.129  centos 7  tengine  2.3. ...

  8. Python基础14

    P73. 内嵌函数的讲解介绍 内部函数,书中讲的应用较简单,后面找篇具体的文章学习下

  9. 我为啥不想用Python

    Python这门语言从一开始就是一个玩具语言,它不是给正经程序员用的东西. 运行效率低 Python运行效率很低,这就导致Python中很多库底层实际上是C++.很多时候,自己千方百计优化的结果就不如 ...

  10. ASP.NET Core使用Quartz定时调度

    在应用程序开发过程中,经常会需要定时任务调度功能,本篇博客介绍Asp.net Core如何使用Quartz完成定时调度 一.Quartz使用步骤 创建调度器scheduler,并开启 创建Job作业 ...