当前位置:首页 » 存储配置 » 循环链表存储结构代码

循环链表存储结构代码

发布时间: 2022-05-26 12:32:53

① 用java构建循环链表并作为存储结构

package arithmetic.structure;

/**
* @author 作者 :popl
*/
public class LoopDoubleList<E> {

/**
* @param args
*/
public static void main(String[] args) {{
LoopDoubleList<String> m = new LoopDoubleList<String>();
m.addLast("sad");
m.addLast("阿斯顿");
m.addLast("中国");
m.addFirst("lm");
m.addFirst("咖啡色");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// System.out.println(m.get(8));
m.remove(2);
m.add(2, "jia");
for (int i = 0; i < m.size(); i++)
System.out.println(m.get(i));
// m.finalize();
System.gc();}
while(true){System.gc();}
}

private Node hand;

/**
* 节点类
*/
private class Node {
private E data;
private Node prior;
private Node next;

public Node(E data) {
this.data = data;
this.next = null;
this.prior = null;
}
public void finalize(){
try {
System.out.println("节点回收" + data);
super.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

/**
* 在末尾添加元素
*/
public void addLast(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
Node tail = hand.prior;
tail.next = n;
n.prior = tail;
n.next = hand;
hand.prior = n;
}
}

/**
* 在头部添加元素
*/
public void addFirst(E data) {
Node n = new Node(data);
if (hand == null) {
hand = n;
hand.next = hand;
hand.prior = hand;
} else {
n.next = hand;
n.prior = hand.prior;
hand.prior.next = n;
hand.prior = n;
hand = n;
}
}

/**
* 在指定位置添加
*/
public void add(int index, E data) {
if (index >= this.size()) {
System.out.println("不存在此长度");
}
Node temp = new Node(data);
Node p = hand;
for (int i = 0; i < index - 1; i++)
p = p.next;
temp.next = p.next;
temp.prior = p;
p.next.prior = temp;
p.next = temp;
}

/**
* 获得链表长度
*/
public int size() {
int flage = 0;
if (hand == null)
return 0;
Node p = hand.next;
while (true) {
flage++;
if (p == hand)
return flage;
p = p.next;
}
}

/**
* 在指定位置取元素
*/
public E get(int index) {
if (index >= this.size())
throw new (index);
Node p = hand;
if (index < this.size() / 2) {
for (int i = 0; i < index; i++)
p = p.next;
return p.data;
}else{
for (int i = 0; i < this.size() - index; i++)
p = p.next;
return p.data;
}
}

/**
* 删除指定位置的元素
*/
public void remove(int index) {
Node p = hand;
for (int i = 0; i < index; i++)
p = p.next;
p.next.prior = p.prior;
p.prior.next = p.next;
}

}

② c#循环链表(求源代码)

//刚给你写了一个例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SY2
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[100];//一共100人
for(int i = 0; i<arr.Length;i++)//编号0~99
{
arr[i] = i;
}
int interval = 3;//从0开始数,到第三个人就杀,或者说隔两个就杀一个,也就是..2..5..8这样杀
int remainder = arr.Length;//剩余人数
int tally = interval;
int index = 0;
while (remainder > 1)
{
if (arr[index] != -1)
{
tally--;
if (tally == 0)
{
arr[index] = -1;
remainder--;
tally = interval;
Console.WriteLine("kill:No" + index +" remain:"+ remainder);
}
}
index++;
if (index == arr.Length)
{
index = 0;
}
}

for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != -1)
{
Console.WriteLine("the survivor is:No" + i);
Console.ReadLine();
return;
}
}
}

}
}

③ 循环链表怎么建立。c语言

是的
建立单向循环链表的代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct _A{
int data;
stru<img id="selectsearch-icon" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png" alt="搜索">ct _A *next;
}A;
typedef A* IA;

void createDoubleLink(IA *header) {
int data;
IA p;
printf("input data end with -1:");
scanf("%d", &data);
while(data != -1) {
p = (IA)malloc(sizeof(A));
p->data = data;
if(*header == NULL) {
*header = p;
(*header)->next = *header;
}
else{
IA q = *header;
while(q->next != *header) {
q = q->next;
}
q->next = p;
q->next->next = *header;
}
scanf("%d", &data);
}
}

void print(IA header) {
IA p = header;
if(header == NULL) return;
while(1){
printf("%d\t", p->data);
if(p->next == header) break;
p = p->next;
}
printf("\n");
}

int main()
{
IA header = NULL;
createDoubleLink(&header);
print(header);
return 0;
}

头结点的意思是链表的第一个节点,但这个节点不保存数据。

④ 数据结构编程(循环链表)

include <malloc.h>

#define M 6
#define N 2

struct as
{
int n;
struct as *next;
};

/***********main函数***************/
main()
{
struct as *p;
struct as *head;
int i,j;
int a[M]={1,2,3,4,5,6};

/*****建立链表****/
head=malloc(sizeof(struct as));
p=head;
for (i=0;i<M-1;i++)
{
p->n=a[i];
p->next=malloc(struct as);
p=p->next;
}
p->n=a[M-1];
p->next=head;

/****功能实现***/
p=head;
for (i=1;i<M;i++)//去掉M-1个数
{
for (j=1;j<=N;j++)//1到N数数
{
if (j==2)
{
p->next=p->next->next;
free(p->next);
}
p=p->next;
}
}
hrad=p;//最后剩下的结点由p所指,附给head
}

⑤ C语言中怎样用链表保存结构体数据(动态数据结构)

单向链表很简单的,你这几这么就可以了:
struct client{
char account[14]; //账号
char name[10]; //名字
char identity[20]; //身份证号
char address[15]; //地址
long int money; //存款(可存可取)
client* pNext; //指向下一个节点,如果是最后一个节点则为NULL
};

然后,程序里只需要保存第一个节点就行了:
client* head = (client*)malloc(sizeof(client)); //第一个节点这么产生
head->pNext = NULL; //该表只有一个节点,所以第一个也是最后一个,别忘记赋0

插入的时候从头部插入就行了
client* p = (client*)malloc(sizeof(client));
p->pNext = head;
head = p; //将原来的头付给p的pNext指针,然后原来保存头元素的指针用p取代。

遍历链表更加容易了
client* pNode = head;
while (pNode)
{
printf(pNode->account); //比如打印所有客户的帐号
pNode = pNode->pNext; //让pNode指向下一个节点
//如果该节点是最后一个节点,那么pNode就会变成NULL,因为最后一个节点的pNext指针是NULL,while循环就会因为pNode为0而结束
}

保存进文件的时候相当于遍历一边所有的元素,读取的时候则一个一个读取,然后重新插入链表。最后,提醒一下的是别忘记用free释放由malloc分配的内存。

另外,考虑使用C++,可以更好的管理内存,思路也会更清晰。而且,如果是为了应用,根本不需要自己开发链表类,用STL就可以了,STL不仅提供双向链表,还有Map,HashMap等数据结构,非常适合特别大的数据量保存和查找,链表的查找很慢的,找一个数据相当于要把链表全部过一遍。

⑥ 利用单向循环链表存储结构编写 凯撒移位 。

你想用循环链表加密还是其他的?
要加密用循环链表那样太麻烦了
下面是个循环链表的程序
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
char a;
struct node* next;

}node;

int main()
{
printf("enter the number of elements\n");
int n,n1;
scanf("%d",&n);
getchar();
n1=n;
printf("enter the elements \n");
node *head,*p,*q;
if(n==0)
head=NULL;
else
{
head=(node *)malloc(sizeof(node));
scanf("%c",&head->a);
getchar();
--n;
q=head;
while(n--)
{
p=(node *)malloc(sizeof(node));
q->next=p;
scanf("%c",&p->a);
getchar();
q=p;
}
q->next=head;
}
printf("the list is:\n");
q=head;
n=n1;
while(n1--)
{
printf("%c ",q->a);
q=q->next;
}

return 0;
}

⑦ C语言数据结构 如何建立单向循环链表并且输入值

#include<iostream>
usingnamespacestd;
typedefcharElemType;
typedefintStatus;
#defineOK1
#defineERROR0
typedefstructLnode
{
ElemTypedata;
structLnode*next;
}Lnode,*LinkList;
voidCreat_List(LinkListL)//创建单链表并输入元素
{
LinkListp,q;
q=L;
charch;
cout<<"请输入链表元素,并且以输入#表示结束!"<<endl;
while(cin>>ch&&ch!='#')
{
p=newLnode[sizeof(Lnode)];
if(!p)
{
cout<<"获取内存失败"<<endl;
exit(ERROR);
}
p->data=ch;//尾插法
L->next=p;
L=p;
}
L->next=q;
}
voidoutput_List(LinkListL)//遍历单链表(输出单链表元素)
{
LinkListp;
p=L->next;
if(p==L)
{
cout<<"该链表是空链表!"<<endl;
exit(ERROR);
}
while(p!=L)
{
cout<<p->data<<"";
p=p->next;
}
}
Statusmain()
{
LinkListH;
H=(LinkList)malloc(sizeof(Lnode));
H->next=NULL;//设置头结点为空
Creat_List(H);
output_List(H);
return0;
}//头结点有和没有都是可以的,头结点只是为了让操作链表更方便,

⑧ 数据结构双向循环链表的C语言实现(插入,查询,删除),代码如下:

#include<stdio.h>
#include<malloc.h>

typedefintElemtype;

typedefstructdNode{
Elemtypedata;/*数据域*/
structdNode*prior;/*指向前驱结点的指针域*/
structdNode*next;/*指向后继结点的指针域*/
}*pDLink,*DLinkList;

DLinkListGetEmptyDLink(){//初始化
DLinkListhead=(pDLink)malloc(sizeof(structdNode));
head->data=0;
head->prior=head;
head->next=head;
returnhead;
}

voidDLink_create(DLinkListhead,intn){/*双向循环链表建立函数*/
inti;
pDLinkp,r;
p=r=head;
for(i=0;i<n;i++){
p->next=(pDLink)malloc(sizeof(structdNode));/*为一个新结点分配空间*/
scanf("%d",&p->next->data);/*从键盘输入值,并保存在新结点数据域中*/
p=p->next;//p指向新结点
p->prior=r;//新结点的prior指向上一个结点
r=r->next;//上一个结点前进到新结点
}
p->next=head;//指向头结点
head->prior=p;//head的prior指向最后的结点
}

voidShow(DLinkListhead){//正向显示链表数据
pDLinkp=head->next;
while(p!=head){
printf("%d",p->data);
p=p->next;
}
printf(" ");
}

voidShowR(DLinkListhead){//反向显示数据
pDLinkp=head->prior;
while(p!=head){
printf("%d",p->data);
p=p->prior;
}
printf(" ");
}

intmain(){
DLinkListhead=GetEmptyDLink();
DLink_create(head,10);
printf("正向显示: ");
Show(head);
printf("反向显示: ");
ShowR(head);
return0;
}

⑨ C语言二级考试循环链表是循环队列的链式存储结构

循环队列本身是一种顺序存储结构,而循环列表是一种链式存储结构。两者之间是平级关系。

线性链表是线性表的链式存储结构,包括单链表,双链表,循环链表等。

队列的顺序存储结构一般采用循环队列的形式。

循环队列的操作是按数组取摸运算的,所以是顺序存储,而循环链表本身就是收尾相连的,所以循环链表不是循环队列,两种不同的存储结构,虽然实现的功能是一样的,实现循环两种方式 顺序存储就是循环队列,链式存储就是循环链表。

(9)循环链表存储结构代码扩展阅读:

1、比顺序存储结构的存储密度小(链式存储结构中每个结点都由数据域与指针域两部分组成,相比顺序存储结构增加了存储空间)。

2、逻辑上相邻的节点物理上不必相邻。

3、插入、删除灵活 (不必移动节点,只要改变节点中的指针)。

4、查找节点时链式存储要比顺序存储慢。

5、每个节点是由数据域和指针域组成。

6、由于簇是随机分配的,这也使数据删除后覆盖几率降低,恢复可能提高。

⑩ C语言中怎样用链表保存结构体数据(动态数据结构)

链表有多种形式,如:单向链表,双向链表,单向循环链表,双向循环链表。将链表结构定义为list_t,则该类型中一定(至少)存在一个指向下一节点的指针list_t
*next;除了这个指针,list_t
中可以包含其它类型的数据,包括结构体变量。比如:typedef
struct
{
struct
usr_struct
data;
list_t
*next;
}
list_t;

热点内容
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:658
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:305
子弹算法 发布:2024-09-20 08:41:55 浏览:283
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:811
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:157
sql数据库安全 发布:2024-09-20 08:31:32 浏览:88
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:502
编程键是什么 发布:2024-09-20 07:52:47 浏览:651
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:477
电脑主服务器怎么开机 发布:2024-09-20 07:19:07 浏览:728