首页 > 代码库 > 链表实现与遍历

链表实现与遍历

#include <stdio.h>//单向循环链表实现与遍历

typedef struct Node {

int data;

 

struct Node *next;

} Node;

int main ()

{

Node n1 = {1, NULL};

Node n2 = {2, NULL};

Node n3 = {3, NULL};

Node n4 = {4, NULL};

 

n1.next = &n2;

n2.next = &n3;

n3.next = &n4;

n4.next = &n1;

 

Node *pn = &n1;

 

do {

printf("%d\n", pn->data);

pn = pn->next;

} while(pn != &n1);

 

return 0;

 

 

#include <stdio.h>//单向链表实现与遍历

typedef struct Node {

int data;

 

struct Node *next;

} Node;

int main ()

{

Node n1 = {1, NULL};

Node n2 = {2, NULL};

Node n3 = {3, NULL};

Node n4 = {4, NULL};

 

n1.next = &n2;

n2.next = &n3;

n3.next = &n4;

 

Node *pn = &n1;

 

while(pn!=NULL)

{

  printf("%d",pn->data);

  pn=pn->next;

}

 

return 0;

链表实现与遍历