/*****************************************************
Author:Simon_Kly Version:0.1 Date: 20170520
Description:循环单链表
Mail:degaullekong@gmail.com
Funcion List:
*****************************************************/ #include <stdio.h>
#include <stdlib.h> typedef struct node
{
int data;
struct node *next;
}Node, *Link; /*判断malloc是否成功执行*/
void is_malloc_ok(Link node)
{
if(node == NULL)
{
printf("malloc error!\n");
exit(-);
}
} /*创建单链表*/
void create_link(Link * head)
{
*head = (Link)malloc(sizeof(Node));
is_malloc_ok(*head);
(*head)->next = *head;
} /*创建节点*/
void create_node(Link * new_node)
{
*new_node = (Link)malloc(sizeof(Node));
is_malloc_ok(*new_node);
(*new_node)->next = NULL;
} /*插入节点尾插法*/
void insert_node_tail(Link head, Link new_node)
{
Link p = NULL; p = head; while (p->next != head)
{
p = p->next;
}
new_node->next = head;
p->next = new_node;
} /*插入节点头插*/
void insert_node_head(Link head, Link new_node)
{
new_node->next = head->next;
head->next = new_node;
} /*输出链表*/
void output_link(Link head)
{
Link p = NULL; if (head == NULL)
{
printf("link is not exist!\n");
return ;
}
else if(head->next == head)
{
printf("link is empty!\n");
return ;
} p = head->next; while (p != head)
{
printf("%d\n", p->data);
p = p->next;
}
} /*置为空链*/
void make_link_empty(Link *head)
{
Link p = NULL; p = (*head)->next; while (*head != (*head)->next)
{
(*head)->next = (*head)->next->next;
free(p);
p = (*head)->next;
}
} /*销毁链*/
void release_link(Link * head)
{
make_link_empty(head);
free(*head);
*head = NULL;
} int main()
{
int i;
Link head = NULL;
Link new_node = NULL; output_link(head);
create_link(&head);
output_link(head);
for (i = ; i < ; i++)
{
create_node(&new_node);
new_node->data = i + ;
insert_node_tail(head, new_node);
}
output_link(head); create_node(&new_node);
new_node->data = ;
insert_node_head(head, new_node);
output_link(head); release_link(&head); output_link(head);
return ;
}

循环单链表的于单链表唯一的不同:在对链表中添加或者删除节点时一定要保持链表的头尾连接,在遍历时不能再以NULL为循环结束判断条件,应该使用头和尾相等来判断整个链表结束。

带头结点的循环单链表----------C语言的更多相关文章

  1. PTA 循环单链表区间删除 (15 分)

    本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...

  2. 【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)

    所实现的循环单链表的结构例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill ...

  3. C语言版本:循环单链表的实现

    SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...

  4. 循环单链表定义初始化及创建(C语言)

    #include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...

  5. c语言循环单链表

    /************************************************************************* > File Name: singleLin ...

  6. c语言有头循环单链表

    /************************************************************************* > File Name: singleLin ...

  7. C代码实现非循环单链表

    C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...

  8. 简单约瑟夫环的循环单链表实现(C++)

    刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...

  9. 不带头结点的单链表------C语言实现

    File name:no_head_link.c Author:SimonKly Version:0.1 Date: 2017.5.20 Description:不带头节点的单链表 Funcion L ...

随机推荐

  1. django 之模板层

    1. 模板语法之变量 格式:{{ 变量名 }} 句点符,深度查询(可以点到方法,不要加括号,只能是无参的方法) 代码 视图函数: from django.shortcuts import render ...

  2. PAT甲级——A1141 PATRankingofInstitution

    After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...

  3. Cocos2d-x 发布 Android

    Cocos2d-x 发布 Android 前置需求: Android NDK Android SDK OR Eclipse ADT Bundle Android AVD target installe ...

  4. KMP算法及实现

    #include<cstdio> #include<cmath> #include<cstring> #include<iostream> #inclu ...

  5. elasticsearch-6.0.1安装

    elasticsearch-6.0.1安装 0. 介绍:     ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎:是目前全文搜索引擎的首选. ...

  6. 人生苦短,我学PYTHON

    人生苦短我学PYTHON 坚持 努力

  7. 事件循环--eventloop

    一.什么是事件循环? 事件循环是 JS 实现异步的具体解决方案,同步代码直接执行,异步函数或代码块先放在异步队列中,待同步函数执行完毕,轮询执行异步队列的函数. 事件循环 二.node.js中的事件循 ...

  8. Rendering Problems The following classes could not be found:- android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)

    如图出现如下的错误的时候,一般都是升级Androdi Studio 后导致的,引入库不全,或者其他 东西缺少 可以如下解决方案:

  9. spark on yarn提交任务时报ClosedChannelException解决方案

    spark2.1出来了,想玩玩就搭了个原生的apache集群,但在standalone模式下没有任何问题,基于apache hadoop 2.7.3使用spark on yarn一直报这个错.(Jav ...

  10. docker 部署 mysql8 的 docker-compose 文件编写

    version: '3.4' services: mysql: container_name: platform.mysql. deploy: resources: limits: memory: 3 ...