循环单链表定义初始化及创建(C语言)
#include <stdio.h>
#include <stdlib.h> /**
* 含头节点循环单链表定义,初始化 及创建
*/ #define OK 1;
#define ERROR 0; //函数返回类型,表示函数运行结果的状态
typedef int Status;
//定义数据元素类型
typedef char ElemType; //循环单链表定义
typedef struct LoopLnode {
ElemType data; //数据域,这里是char类型变量
struct LoopLnode *next; //指针域,结构体类型指针
} LoopLnode, *LoopLinkList; //循环单链表初始化
Status InitList(LoopLinkList *list) {
(*list)=(LoopLinkList)malloc(sizeof(LoopLnode));
(*list)->next=(*list);
(*list)->data='T'; //测试用,可不写
return OK;
} //1."头插法"创建仅含"头指针"的单向循环链表
Status CreateList_H(LoopLinkList *list,ElemType arrData[],int length){
int j;
for(j=length-1;j>=0;j--){
//新建结点
LoopLnode *node;
node=(LoopLnode*)malloc(sizeof(LoopLnode));
node->data=arrData[j];
node->next=NULL; //插入循环链表
node->next=(*list)->next;
(*list)->next=node; //list始终指向头结点
}
return OK;
} //2."尾插法"创建仅含"头指针"的单向循环链表
Status CreateList_R(LoopLinkList *list,ElemType arrData[],int length){
LoopLnode *r;
r=*list;
int j;
for(j=0;j<length;j++) {
//新建结点
LoopLnode *node;
node=(LoopLnode*)malloc(sizeof(LoopLnode));
node->data=arrData[j];
node->next=NULL; //插入循环链表
node->next=r->next;
r=node;
}
return OK;
} //3."头插法"创建仅含"尾指针"的单向循环链表
Status BuildList_H(LoopLinkList *list,ElemType arrData[],int length){
int j;
for(j=length-1;j>=0;j--){
//新建结点
LoopLnode *node;
node=(LoopLnode*)malloc(sizeof(LoopLnode));
node->data=arrData[j];
node->next=NULL; //node插入1号结点,list为尾指针
node->next=(*list)->next->next; //node->next=头结点->next
if((*list)->next==(*list)) (*list)=node; //当只有头结点时(插入第一个结点时,手动设置node为尾指针)
(*list)->next->next=node; //头结点->next=node;
}
return OK;
} //4."尾插法"创建仅含"尾指针"的单向循环链表
Status BuildList_R(LoopLinkList *list,ElemType arrData[],int length) {
int j;
for(j=0;j<length;j++) {
//新建结点
LoopLnode *node;
node=(LoopLnode*)malloc(sizeof(LoopLnode));
node->data=arrData[j];
node->next=NULL; node->next=(*list)->next; //node->next=头结点
(*list) = node; //尾指针 —> node
}
return OK;
} int main(void){
//产生待插入到链表的数据
ElemType data1='A',data2='B',data3='C';
ElemType waitInserted[]={data1,data2,data3,};
//获得数组长度
int arrLength=sizeof(waitInserted)/sizeof(waitInserted[0]); /**1.头插法建立只含头指针循环单链表**/
//定义链表并初始化
LoopLinkList list1;
InitList(&list1);
//按既定数据建立链表
CreateList_H(&list1,waitInserted,arrLength);
//测试
printf("%c\n",list1->next->next->next->next->next->next->data); //B /**2.尾插法建立只含头指针循环单链表**/
//定义链表并初始化
LoopLinkList list2;
InitList(&list2);
//按既定数据建立链表
CreateList_R(&list2,waitInserted,arrLength);
//测试
printf("%c\n",list1->next->next->next->next->next->next->data); //B /**3.头插法建立只含尾指针循环单链表**/
//定义链表并初始化
LoopLinkList list3;
InitList(&list3);
//按既定数据建立链表
BuildList_H(&list3,waitInserted,arrLength); //list3指向表尾
//测试
printf("%c\n",list3->next->next->next->next->next->next->next->next->next->data); //T /**4.尾插法建立只含尾指针循环单链表**/
//定义链表并初始化
LoopLinkList list4;
InitList(&list4);
//按既定数据建立链表
BuildList_H(&list4,waitInserted,arrLength); //list4指向表尾
//测试
printf("%c\n",list4->next->next->next->next->next->next->next->next->next->next->data); //A printf("\nEND!");
return 0;
}
循环单链表定义初始化及创建(C语言)的更多相关文章
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- c语言循环单链表
/************************************************************************* > File Name: singleLin ...
- c语言有头循环单链表
/************************************************************************* > File Name: singleLin ...
- 简单约瑟夫环的循环单链表实现(C++)
刚刚接触C++以及数据结构,今天做了第一次尝试用C++和数据结构解决问题,问题是基于约瑟夫环问题的简单版. 先来看看约瑟夫环问题的介绍: 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3.. ...
- PTA 循环单链表区间删除 (15 分)
本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...
- C语言版本:循环单链表的实现
SClist.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include<cstdio> #include<malloc.h> # ...
- 带头结点的循环单链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- 【c++版数据结构】之循环单链表的实现(带头结点以及尾节点)
所实现的循环单链表的结构例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill ...
- 数据结构5: 链表(单链表)的基本操作及C语言实现
逻辑结构上一个挨一个的数据,在实际存储时,并没有像顺序表那样也相互紧挨着.恰恰相反,数据随机分布在内存中的各个位置,这种存储结构称为线性表的链式存储. 由于分散存储,为了能够体现出数据元素之间的逻辑关 ...
随机推荐
- hash table
Hash Table,叫做哈希表,也叫做散列表.概念:通过某种对应关系h,使得每一个元素和储存位置一一对应.这种对应关系称为哈希函数.它最大的优点就是插入.搜索和删除得很快(O(1)).碰撞(Coll ...
- Petrozavodsk Winter Training Camp 2017G(栈)题解
题意: \(M_i\)为一个\(m*m\)矩阵,已知 \[\begin{aligned} &M_0=A\\ &M_i=(\prod_{j=c_i}^{i+1}M_j)B \end{al ...
- HDU 4746 Mophues(莫比乌斯反演)题解
题意: \(Q\leq5000\)次询问,每次问你有多少对\((x,y)\)满足\(x\in[1,n],y\in[1,m]\)且\(gcd(x,y)\)的质因数分解个数小于等于\(p\).\(n,m, ...
- Spring(三) Spring IOC
Spring 核心之 IOC 容器 再谈 IOC 与 DI IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创 建.依赖的代码,反转给容器 ...
- Docker--Image and Container
2.1 深入探讨Image 说白了,image就是由一层一层的layer组成的. 2.1.1 官方image https://github.com/docker-library mysql http ...
- npm ip
npm ip webpack $ ip address var ip = require('ip'); ip.address() // my ip address ip.isEqual('::1', ...
- translate.js
http://www.openxrest.com/translatejs/index.html translate.js translate.js is a jQuery plugin to tran ...
- Xcode show whitespace
Xcode show whitespace Xcode 11.5 how to config Xcode show whitespace 如何配置 Xcode 显示空白字符 Editor -> ...
- Web Performance API
Web Performance API 性能监测/性能优化 https://developer.mozilla.org/en-US/docs/Web/API/Performance https://d ...
- cache-control config & http cache storage location control
cache-control config & http cache storage location control cache-control 设置 where is the storage ...