#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
int _id; char s[];
struct Node* pre;
struct Node* next;
};
void node_free(struct Node** q) {
if( *q != NULL) {
printf("free %d\n",(*q)->_id);
free(*q);
*q = NULL;
}
}
void node_print(struct Node* q) {
if (NULL == q) {
puts("节点打印:空节点,无可打印");
return;
}
printf("---id = %2d---", q->_id);
printf("preview = %10d ", q->pre);
printf("【address = %10d】 ", q);
printf("next = %10d\n", q->next);
}
void chain_print(struct Node* qFirst) {
if (qFirst == NULL) {
puts("没有元素可以打印");
return;
}
puts("----------↓↓↓打印链表------------");
// 遍历链表
struct Node* q;
for(q = qFirst; q != NULL; q=q->next ) {
node_print(q);
}
puts("----------↑↑↑打印链表------------");
} /*
* 为链表追加节点(加在最后)
* 参数:头节点,需要追加的节点
* 返回值:无
*/
void chain_add(struct Node* qFirst, struct Node* qAdd) {
// 定位到链表头
struct Node* q = qFirst;
// 只要后面(next)有节点,往后找;直到没有next的节点(最后一个)
for(q; q->next != NULL; q=q->next ) {
node_print(q);
}
// 此时定位在最后一个节点,下图1
// 将新节点加在最后节点的后面(next)
q->next = qAdd;// 下图2
qAdd->pre = q;//下图3
} /*
* 删除节点
* 参数:1.头结点 2.待删除的结点
* 因为被删除的结点需要置空,所以需要使用二级指针
* 返回值:-1 删除失败/0 删除成功
*/
int chain_remove(struct Node** qFirst, struct Node** qRemove) {
struct Node* qPre = NULL;
struct Node* qNext = NULL;
struct Node* q = *qFirst; // 1.输入Check
if(NULL == *qRemove){
puts("删无可删!");
return -;
}else{
printf("删除节点:id=%d\n", (*qRemove)->_id);
} // 2.删除头结点,特殊对待
if(*qFirst == *qRemove ) { if((*qFirst)->next == NULL){
// 就一个头结点的场合
node_free(qFirst);
}else{
qNext = q->next;
node_free(qFirst);
*qFirst = qNext;
} return ;
}
// 遍历链表
for(q; q != NULL; q=q->next ) {
if (q == *qRemove) {
qPre = q->pre;
qNext = q->next; if (qNext!=NULL) {
qNext->pre = qPre;
qPre->next= qNext;
} else {
// 尾节点的场合
qPre->next= NULL;
}
node_free(qRemove);
return ;
}
} }
void chain_clear(struct Node** qFirst) {
puts("\n----------Clear------------"); if (qFirst == NULL) {
puts("已经是空");
return;
} // 遍历链表
// 不断删除第一个元素
while(*qFirst != NULL) {
chain_remove(qFirst,qFirst);
}
}
struct Node* chain_get(struct Node* qFirst, int index) {
printf("---获取index = %d的节点:", index);
int i = ;
// 遍历链表
struct Node* q = qFirst;
for(q; q!= NULL; q=q->next,i++ ) {
if (index == i) {
return q;
}
}
return NULL;
}
/*
* 获取链表长度(即 节点的个数)
* 参数: 头节点
* 返回值:链表长度
*/
int chain_count(struct Node* qFirst) {
if (qFirst == NULL) {
// 头节点都没有,长度为0
return ;
}
int i = ;
// 遍历链表
struct Node* q = qFirst;
for(q; q != NULL; q=q->next) {
// 顺藤摸瓜,直到最后一个节点
i++;// 找到一个就+1
}
return i;
}
struct Node* node_new(int id) {
struct Node* q = (struct Node*)malloc(sizeof(struct Node));
memset(q, , sizeof(struct Node));
q->_id = id;
return q;
}
void testFunction(){
struct Node* q1 = node_new();
struct Node* q2 = node_new();
struct Node* q3 = node_new();
struct Node* q4 = node_new();
struct Node* q5 = node_new();
puts("###有节点的链表:");
printf("count = %d\n",chain_count(NULL));
chain_print(NULL); puts("\n###1个节点的链表:");
printf("count = %d\n",chain_count(q1));
chain_print(q1); // 增
chain_add(q1, q2);
chain_add(q1, q3);
chain_add(q1, q4);
chain_add(q1, q5); puts("\n###5个节点的链表:");
printf("count = %d\n",chain_count(q1));
chain_print(q1); puts("");
struct Node* pGet;
pGet = chain_get(q1, );
node_print(pGet);
pGet = chain_get(q1, );
node_print(pGet);
pGet = chain_get(q1, );
node_print(pGet);
pGet = chain_get(q1, );
node_print(pGet); // 删
puts("\n###删除测试");
chain_remove(&q1,&q5);
chain_print(q1);
chain_remove(&q1,&q3);
chain_print(q1);
chain_remove(&q1,&q1);
chain_print(q1);
//chain_remove(&q1,&q1);
//chain_print(q1);
//chain_remove(&q1,&q1);
//chain_print(q1);
//chain_remove(&q1,&q1);
//chain_print(q1); chain_clear(&q1);
chain_print(q1); getchar();
}
int _tmain(int argc, _TCHAR* argv[]){
testFunction();
return ;
}

C语言,链表操作的更多相关文章

  1. C语言链表操作模板(添加,删除,遍历,排序)

    C语言链表操作模板,摘自郝斌的C语言视频教程,简单的修改成了纯C格式.当年照着视频学习的时候记录下来的,在使用的时候直接拿来修改修改修改能节约不少时间的. /********************* ...

  2. ZT C语言链表操作(新增单向链表的逆序建立)

    这个不好懂,不如看 转贴:C语言链表基本操作http://www.cnblogs.com/jeanschen/p/3542668.html ZT 链表逆序http://www.cnblogs.com/ ...

  3. C 语言链表操作例程 (待完善)

    #include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>#inclu ...

  4. C语言,单链表操作(增删改查)(version 0.1)

    这天要面试,提前把链表操作重新写了一遍.备份一下,以备不时之需. 希望有人能看到这篇代码,并指正. // File Name : list.h #include "stdafx.h" ...

  5. c语言实现--带头结点单链表操作

    可能是顺序表研究的细致了一点,单链表操作一下子就实现了.这里先实现带头结点的单链表操作. 大概有以下知识点. 1;结点:结点就是单链表中研究的数据元素,结点中存储数据的部分称为数据域,存储直接后继地址 ...

  6. C语言 链表

    原文:C语言 链表 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能. ...

  7. C语言链表实例--玩转链表

    下图为最一简单链表的示意图: 第 0 个结点称为头结点,它存放有第一个结点的首地址,它没有数据,只是一个指针变量.以下的每个结点都分为两个域,一个是数据域,存放各种实际的数据,如学号 num,姓名 n ...

  8. JAVA 链表操作:循环链表

    主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...

  9. go语言文件操作,这期资料比较详细( 欢迎加入go语言群: 218160862 )

    go语言文件操作,这期资料比较详细 欢迎加入go语言群: go语言深圳群 golang深圳 218160862 点击加入 文件操作 func Open(name string) (file *File ...

  10. C语言字符串操作总结大全(超详细)

    本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat( ...

随机推荐

  1. jdk的环境变量配置

    首先下载jdk,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 可以选择下载jdk的版本,按照提示一步一 ...

  2. 【集合】Java中的具体集合(一)

    Java中不止提供了集合框架中的接口,还提供了许多具体的实现. Java中的具体集合 集合类型 描述 ArrayList 一种可以动态增长和缩减的索引序列 LinkedList 一种可以在任何位置进行 ...

  3. sed常用操作命令

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据进行替换.删除.新增.选取等特定工作. 命令格式: sed [OPTION]... {script-only-i ...

  4. github/gitee使用办法2

    打开自己的仓库 git pull 把内容复制过去 git add . 添加所有 查看状态 git status 提交 git commit -m 'XXX' 最后push git push 如果内容本 ...

  5. Magento 1.9.x 子分类无法访问

    app/code/core/Mage/Catalog/Model/Url.php 大概 ~807: Change: if ($product->getUrlKey() == '' &&a ...

  6. 左耳听风-ARTS-第1周

    Algorithm https://leetcode.com/problems/longest-common-prefix/ class Solution { public String longes ...

  7. Hbase数据库

        1.简介 HBase从诞生至今将近10年,在apache基金会的孵化下,已经变成一个非常成熟的项目,也有许多不同的公司支持着许多不同的分支版本,如cloudra等等. HBase不同于一般的关 ...

  8. zabbix3.4使用外部邮件报警

    打开web页面,找到 管理--报警媒介类型 打开 Email 填写外部邮箱信息 点击更新,这样发件人和邮箱服务器信息定义完成. 添加收件人信息: 接下来创建动作,配置---动作---选触发器 ok 过 ...

  9. Spark编程指南分享

    转载自:https://www.2cto.com/kf/201604/497083.html 1.概述 在高层的角度上看,每一个Spark应用都有一个驱动程序(driver program).驱动程序 ...

  10. 关闭防火墙,selinux,交互式设置IP的脚本

    脚本内容: #!/bin/bash # ens=$(cat /proc/net/dev | awk '{if($2>0 && NR > 2) print substr($1 ...