C语言版本:循环单链表的实现
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语言版本:循环单链表的实现的更多相关文章
- C语言版本:单链表的实现(优化版本)
未优化版本:http://www.cnblogs.com/duwenxing/p/7569376.html slist.h #ifndef __SLIST_H__ #define __SLIST_H_ ...
- C语言版本:单链表的实现
slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include<cstdio> #include<malloc.h> #inc ...
- c语言实现循环单链表
//初始化 Node*InitList() { Node*head=(Node*)malloc(sizeof(Node)); head->next=NULL; head->data=-1; ...
- c语言循环单链表
/************************************************************************* > File Name: singleLin ...
- c语言有头循环单链表
/************************************************************************* > File Name: singleLin ...
- 带头结点的循环单链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- 循环单链表定义初始化及创建(C语言)
#include <stdio.h> #include <stdlib.h> /** * 含头节点循环单链表定义,初始化 及创建 */ #define OK 1; #defin ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- 简单约瑟夫环的循环单链表实现(C++)
刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...
随机推荐
- jdk1.7环境配置
JDK1.7的环境配置(我的是jdk1.7,文件名写快了,忽略忽略) 官网下载自己需要的版本(ps:我这是朋友发给我的就不提供官网地址,去百度搜jdk就可以了) 下载下来除了改存放路径还有记得再jdk ...
- 两个docker容器互连时,提示no route to host错误的问题
大家都知道,两个docker container互连的时候可以用link,但是,我们也知道,container可以将自己的端口映射到宿主机上,比如一个container A上的tomcat将端口暴露给 ...
- 封装PDO连接数据库代码
废话不说直接上代码: <?php class DB { protected static $_connect; protected $dsn, $pdo; protected $_data, $ ...
- 【转】 python中 * 的用法
转自:https://www.cnblogs.com/jony7/p/8035376.html 1.表示乘号 2.表示倍数,例如: def T(msg,time=1): print((msg+' ...
- nano,pico文本编辑器,debian执行crontab -e
debian执行crontab -e的时候出现: Edit this file to introduce tasks to be run by cron.## Each task to run has ...
- opencv 显示摄像头数据
本文章是一个小例子,主要是在ubuntu 系统中利用Opencv 显示摄像头的数据 ,显示到对话框中. 1.建立一个 main.cpp #include<opencv2/core/core.h ...
- Ubuntu学习总结-01 安装Ubuntu
Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...
- 20155211 网络攻防技术 Exp08 Web基础
20155211 网络攻防技术 Exp08 Web基础 实践内容 Web前端HTML,能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. We ...
- EZ 2018 05 13 NOIP2018 模拟赛(十三)
这次的比赛真心水,考时估分240,然后各种悠闲乱逛 然后测完T1数组开小了炸成40,T2,T3都没开long long,T2炸成20,T3爆0 掉回1600+的深渊,但是还有CJJ dalao比我更惨 ...
- md5加密,同样的代码得到不同的加密结果(已解决)
场景: 开发环境(windows下)调用第三方接口验签通过,发测试环境(linux下)后死活验签通过不了 原因: md5是一项成熟的加密技术,问题应该在代码里,查了查感觉可能是字符编码的问题,导致加签 ...