[日常] 算法-单链表的创建
2023-02-18 15:47:11 时间
1. 先创建一个头结点,不需要有数据域,头结点的next指向null
2.循环中创建结点,把头结点的next赋值给 新结点的next,相当于新结点的next指向了(头结点next所指向的)
3.把新结点赋值给头结点的next ,相当于头结点的next指向了新结点,这样就串起来了
4.头结点就相当于整个链表
5.循环遍历的时候,头结点没有数据可以直接跳过,把结点的next赋值给结点,相当于向下移动了一项
c语言版:
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Node{ char* data; struct Node* next; } Node; typedef Node* LinkList; int main(){ //1.创建一个链表 LinkList head=(LinkList)malloc(sizeof(Node));//分配内存 //malloc(sizeof(Node))是给头结点分配内存,head是指向头结点的指针叫头指针 head->next=NULL;//数据域加指针域叫头结点 LinkList a1=(LinkList)malloc(sizeof(Node)); a1->data="aaa"; a1->next=NULL; head->next=a1; //a1是指向第一个结点的指针,赋值给head->next,就相当于head->next指向了a1 LinkList a2=(LinkList)malloc(sizeof(Node)); a2->data="bbb"; a2->next=NULL; a1->next=a2; //a2是指向第二个结点的指针,赋值给a1->next,就相当于a1->next指向了a2 //2.循环创建一个链表 LinkList list=(LinkList)malloc(sizeof(Node)); list->next=NULL; for(int i=0;i<10;i++){ LinkList node=(LinkList)malloc(sizeof(Node)); char* str=(char*)malloc(4); sprintf(str,"aaa%d",i); node->data=str; node->next=list->next; list->next=node; } //遍历 int j=0; while(list->next){ list=list->next; printf("%s \n",list->data); j++; } return 0; }
go语言版:
package main import( "fmt" ) type Node struct{ data string next *Node } func main(){ list:=new(Node) list.next=nil var node *Node for i:=0;i<10;i++{ node=new(Node) node.data="aaa"+fmt.Sprintf("%d",i) node.next=list.next list.next=node } //遍历 for{ list=list.next fmt.Println(list.data) if list.next==nil{ break } } }
php语言版:
<?php class Node{ public $data; public $next; } $list=new Node(); $list->next=null; for($i=0;$i<10;$i++){ $node=new Node(); $node->data="aaa{$i}"; $node->next=$list->next; $list->next=$node; } //遍历 while($list->next){ $list=$list->next; echo $list->data."\n"; }
相关文章
- 论文解读《Bilinear Graph Neural Network with Neighbor Interactions》
- 论文解读《Deep Attention-guided Graph Clustering with Dual Self-supervision》
- 论文解读(ClusterSCL)《ClusterSCL: Cluster-Aware Supervised Contrastive Learning on Graphs》
- 论文解读(SimGRACE)《SimGRACE: A Simple Framework for Graph Contrastive Learning without Data Augmentation》
- 论文解读(GTN)《Graph Transformer Networks》
- 论文解读(SAGPool)《Self-Attention Graph Pooling》
- 论文解读(DiffPool)《Hierarchical Graph Representation Learning with Differentiable Pooling》
- 论文解读《Deeper Insights into Graph Convolutional Networks for Semi-Supervised Learning》
- 论文解读(GMT)《Accurate Learning of Graph Representations with Graph Multiset Pooling》
- 论文解读《Measuring and Relieving the Over-smoothing Problem for Graph NeuralNetworks from the Topological View》
- 论文解读(DAGNN)《Towards Deeper Graph Neural Networks》
- 论文解读(Debiased)《Debiased Contrastive Learning》
- 论文解读(IGSD)《Iterative Graph Self-Distillation》
- 论文解读(SUBG-CON)《Sub-graph Contrast for Scalable Self-Supervised Graph Representation Learning》
- 论文解读(MERIT)《Multi-Scale Contrastive Siamese Networks for Self-Supervised Graph Representation Learning》
- 论文解读(MCNS)《Understanding Negative Sampling in Graph RepresentationLearning》
- 论文解读(GROC)《Towards Robust Graph Contrastive Learning》
- 图神经网络的攻击防御
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- 论文解读(DCRN)《Deep Graph Clustering via Dual Correlation Reduction》