数据结构之链表的实现方式

简介: 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序
实现的。

c 语言实现

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

struct Node
{
    char content[10];
    struct Node* next;
};


int main()
{
    struct Node node1;
    struct Node node2;
    struct Node node3;

    //赋值
    strcpy(node1.content,"node1");
    strcpy(node2.content,"node2");
    strcpy(node3.content,"node3");

    //串联
    node1.next = &node2;
    node2.next = &node3;
    node3.next = NULL;

    struct Node *head;
    head = &node1;
    while(NULL != head){
        printf("%s\n",(*head).content);
        head = (*head).next;
    }
    return 0;
}

java 代码实现

    public static void main(String[] args) {


        Node<Integer> node1 = new Node<>();
        Node<Integer> node2 = new Node<>();
        Node<Integer> node3 = new Node<>();

        node1.obj = 1;
        node2.obj = 2;
        node3.obj = 3;

        node1.next = node2;
        node2.next = node3;
        node3.next = null;

        //定义链表的头
        Node<Integer> head = new Node<>();
        head.next = node1;

        while (null != head){

            System.out.println(head.obj);
            head = head.next;
        }
 }
public class Node<T> {
    //链表的内容
    public T obj;
    //链表指向的下一个节点
    public Node next;
}

猜你喜欢

转载自blog.csdn.net/lovebuzhidao/article/details/80015579