SClist.h

 #ifndef __SCLIST_H__
#define __SCLIST_H__ #include<cstdio>
#include<malloc.h>
#include<assert.h>
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node *next;
}Node,*PNode; typedef struct List {
PNode first;
PNode last;
size_t size;
}List; void InitSClist(List *list);//初始化循环单链表
void push_back(List *list, ElemType x);//在循环单链表的末尾插入元素
void push_front(List *list, ElemType x);//在循环单链表的头部插入元素
void show_list(List *list);//打印循环单链表
void pop_back(List *list);//删除循环单链表的最后一个元素
void pop_front(List *list);//删除循环单链表的第一个元素
void insert_val(List *list, ElemType val);//将数据元素插入到循环单链表中(要求此时循环单链表中的数据元素顺序排列)
Node* find(List *list, ElemType x);//查找循环单链表中数据值为x的结点
int length(List *list);//求循环单链表的长度
void delete_val(List *list, ElemType x);//按值删除循环单链表中的某个数据元素
void sort(List *list);//对循环单链表进行排序
void reverse(List *list);//逆置循环单链表
void clear(List *list);//清除循环单链表
void destroy(List *list);//摧毁循环单链表 //优化
Node* _buynode(ElemType x);//创建结点 #endif

SClist.cpp

 #include"SClist.h"

 Node* _buynode(ElemType x) {
Node *s = (Node*)malloc(sizeof(Node));
assert(s != NULL);
s->data = x;
s->next = NULL;
return s;
} void InitSClist(List *list) {
Node *s = (Node*)malloc(sizeof(Node));
assert(s != NULL);
list->first = list->last = s;
list->last->next = list->first;//让链表最后一个结点的指针域指向头结点,从而时链表循环
list->size = ;
} void push_back(List *list, ElemType x) {
Node *s = _buynode(x);
list->last->next = s;
list->last = s;
23 list->last->next = list->first;
list->size++;
} void push_front(List *list, ElemType x) {
Node *s = _buynode(x);
s->next = list->first->next;
list->first->next = s;
31 if (list->last == list->first)
32 list->last = s;
list->size++;
} void show_list(List *list) {
Node *p = list->first->next;
while (p != list->first) {
printf("%d->", p->data);
p = p->next;
}
printf("Nul.\n");
} void pop_back(List *list) {
if (list->size == ) return;
Node *p = list->first;
while (p->next != list->last)
p = p->next;
free(list->last);
list->last = p;
52 list->last->next = list->first;
list->size--;
} void pop_front(List *list) {
if (list->size == ) return;
Node *p = list->first->next;
list->first->next = p->next;
60 if (list->size == 1)
61 list->last = list->first;
free(p);
list->size--;
} void insert_val(List *list, ElemType x) {
Node *p = list->first;
while (p->next != list->last && p->next->data < x)
p = p->next;
if (p->next == list->last && p->next->data < x)
push_back(list, x);
else {
Node *s = _buynode(x);
s->next = p->next;
p->next = s;
list->size++;
}
} Node* find(List *list, ElemType key) {
if (list->size == ) return NULL;
Node *p = list->first->next;
while(p != list->first && p->data != key)
p = p->next;
85 if (p == list->first)
86 return NULL;
return p;
} int length(List *list) {
return list->size;
} void delete_val(List *list, ElemType x) {
if (list->size == ) return;
Node *p = find(list, x);
if (p == NULL) {
printf("没有要删除的数据!\n");
return;
}
if (p == list->last)
pop_back(list);
else {
Node *q = p->next;
p->data = q->data;
p->next = q->next;
free(q);
list->size--;
}
} void sort(List *list) {
if (list->size == || list->size == ) return;
Node *s = list->first->next;
Node *q = s->next;
list->last->next = NULL;
list->last = s;
list->last->next = list->first;
while (q != NULL) {
s = q;
q = q->next;
Node *p = list->first;
while (p->next != list->last && p->next->data < s->data)
p = p->next;
if (p->next == list->last &&p->next->data < s->data) {
list->last->next = s;
list->last = s;
list->last->next = list->first;
}
else {
s->next = p->next;
p->next = s;
}
}
} void reverse(List *list) {
if (list->size == || list->size == ) return;
Node *p = list->first->next;
Node *q = p->next;
list->last->next = NULL;
list->last = p;
list->last->next = list->first;
while (q != NULL) {
p = q;
q = q->next;
p->next = list->first->next;
list->first->next = p;
}
} void clear(List *list) {
Node *p = list->first->next;
while (p != list->first) {
list->first->next = p->next;
free(p);
p = list->first->next;
}
list->last = list->first;
list->last->next = list->first;
list->size = ;
} void destroy(List *list) {
clear(list);
free(list->first);
list->first = list->last = NULL;
}

main.cpp

 #include"SClist.h"

 void main() {
List mylist;
InitSClist(&mylist); ElemType item;
Node *p = NULL;
int select = ;
while (select) {
printf("*******************************************\n");
printf("*[1] push_back [2] push_front *\n");
printf("*[3] show_list [4] pop_back *\n");
printf("*[5] pop_front [6] insert_val *\n");
printf("*[7] find [8] length *\n");
printf("*[9] delete_val [10] sort *\n");
printf("*[11] reverse [12] clear *\n");
printf("*[13*] destroy [0] quit_system *\n");
printf("*******************************************\n");
printf("请选择:>>");
scanf("%d", &select);
if (select == ) break;
switch (select) {
case :
printf("请输入要插入的数据(-1结束):>");
while (scanf("%d", &item), item != -) {
push_back(&mylist, item);
}
break;
case :
printf("请输入要插入的数据(-1结束):>");
while (scanf("%d", &item), item != -) {
push_front(&mylist, item);
}
break;
case :
show_list(&mylist);
break;
case :
pop_back(&mylist);
break;
case :
pop_front(&mylist);
break;
case :
printf("请输入要插入的数据:>");
scanf("%d", &item);
insert_val(&mylist, item);
break;
case :
printf("请输入要查找的数据:>");
scanf("%d", &item);
p = find(&mylist, item);
if (p == NULL)
printf("要查找的数据在单链表中不存在!\n");
break;
case :
printf("单链表的长度为%d\n", length(&mylist));
break;
case :
printf("请输入要删除的值:>");
scanf("%d", &item);
delete_val(&mylist, item);
break;
case :
sort(&mylist);
break;
case :
reverse(&mylist);
break;
case :
clear(&mylist);
break;
//case 13:
//destroy(&mylist);
//break;
default:
printf("选择错误,请重新选择!\n");
break;
}
}
destroy(&mylist);
}

C语言版本:循环单链表的实现的更多相关文章

  1. C语言版本:单链表的实现(优化版本)

    未优化版本:http://www.cnblogs.com/duwenxing/p/7569376.html slist.h #ifndef __SLIST_H__ #define __SLIST_H_ ...

  2. C语言版本:单链表的实现

    slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #inc ...

  3. c语言实现循环单链表

    //初始化 Node*InitList() { Node*head=(Node*)malloc(sizeof(Node)); head->next=NULL; head->data=-1; ...

  4. c语言循环单链表

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

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

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

  6. 带头结点的循环单链表----------C语言

    /***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...

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

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

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

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

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

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

随机推荐

  1. Windows+Python+anaconda机器学习安装及环境配置步骤

    Windows+Python+anaconda机器学习安装及环境配置步骤 1. 下载安装python3.6以上版本(包含pip,不用自己安装)2. 直接下载安装pycharm安装包(用于编写pytho ...

  2. Vuex状态管理详解

    什么是Vuex 专门为vue应用程序开发的状态管理模式,采用集中式存储管理应用的所有组件的状态(数据),以相应的规则保证状态以一种可预测的方式发生改变 Vuex的作用(什么样的情况下使用Vuex) 多 ...

  3. 数据同步canal服务端HA配置

    canal服务端HA模式,本人并未使用过,为保证文章的完整性,从以下地址摘抄该部分内容,待以后验证及使用 https://github.com/alibaba/canal/wiki/AdminGuid ...

  4. 通过SSH秘钥登录线上MySQL数据库(基于Navicat)

    前言 生产环境的数据库往往需要经过严格的安全限制,所以禁用密码登录,使用秘钥的方式是一种相对安全的登录方式. 原理: 角色: 主机A:其他主机,有访问线上数据库的权限 主机B:线上数据库的主机 主机C ...

  5. 蓝桥杯之剪格子(经典dfs)

    如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+--+-- ...

  6. (转)LVS+Keepalived使用总结 vip丢失

    /sbin/ifconfig lo: $vip broadcast $vip netmask 255.255.255.255 up /sbin/ifconfig lo: 172.16.254.63 b ...

  7. JQuery radio单选框应用

    转载:JQuery判断radio(单选框)是否选中和获取选中值方法总结 一.利用获取选中值判断选中 直接上代码,别忘记引用JQuery包 复制代码 代码如下: < !DOCTYPE html P ...

  8. PHP设计模式——訪问者模式

    声明:本系列博客參考资料<大话设计模式>,作者程杰. 訪问者模式表示一个作用于某对象结构中的各元素的操作. 它使你能够在不改变各元素类的前提下定义作用于这些元素的新操作. UML类图: w ...

  9. centos 64位系统安装

    由于centos 64位镜像大于4G,所以U盘装不进去.iso镜像,选择网络安装的方法或者使用一个U盘制作启动盘和一个硬盘来装镜像的方法. 1 网络安装第一步 下载 CentOS 安装 ISO 浏览  ...

  10. 自研后端HTTP请求参数验证器服务ParamertValidateService

    好处:方便了后端对HTTP请求中参数进行核验,只需一次编写效验器,一行代码便可对所有参数的pojo进行参数核验!而且更改效验逻辑时只需要更改效验器类即可,实现了解耦合. 只需要程序员按照规范开发一个P ...