當前位置:首頁 » 存儲配置 » 循環鏈表存儲結構代碼

循環鏈表存儲結構代碼

發布時間: 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;

熱點內容
裝緩存下載 發布:2024-09-20 05:42:36 瀏覽:72
gon引擎自動回收腳本 發布:2024-09-20 05:39:39 瀏覽:246
好醫生連鎖店密碼多少 發布:2024-09-20 05:09:38 瀏覽:15
魔獸腳本代理 發布:2024-09-20 05:09:35 瀏覽:99
python登陸網頁 發布:2024-09-20 05:08:39 瀏覽:757
安卓qq飛車如何轉蘋果 發布:2024-09-20 04:54:30 瀏覽:178
存儲過程中in什麼意思 發布:2024-09-20 04:24:20 瀏覽:315
php顯示數據 發布:2024-09-20 03:48:38 瀏覽:501
源碼安裝軟體 發布:2024-09-20 03:44:31 瀏覽:354
入門編程游戲的書 發布:2024-09-20 03:31:26 瀏覽:236