数据结构之链表(一)单链表

目录

一、常用的4类基本结构

二、单向链表(SimpleLinked List

1、定义Definition

2、实现Implement with Java language

3、效率Efficiency

 

一、常用的4类基本结构

      数据结构(Data Structure)是指相互之间存在一种或多种特定关系的数据元素的集合,这种数据元素(data element)相互之间的关系称为结构(structure)。根据数据元素之间关系的不同,通常有下列4类数据结构:

1集合:结构中的数据元素之间除了同属于一个集合的关系外,别无其他关系。

2线性结构:结构中的数据元素之间存在一个对一个的关系。

3树形结构:结构中的数据元素之间存在一个对多个的关系。

4图状结构:结构中的数据元素之间存在一个对多个的关系。

二、单向链表(SimpleLinked List

1、定义Definition

       链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。单向链表的每个节点只有一个指向下一个节点地址的指针。

2、实现

1)定义链表节点类

[java]view plaincopyprint?

1. public class Link   

2. {  

3.     public int iData;  

4.     public Link next;  

5.       

6.     public Link(int iData)  

7.     {  

8.         this.iData=iData;  

9.     }  

10.      

11.    public void dispaly()  

12.    {  

13.        System.out.println(iData+",");  

14.    }  

15.      

16.}  

public class Link

{

    public int iData;

    public Link next;

   

    public Link(int iData)

    {

      this.iData=iData;

    }

   

    public void dispaly()

    {

      System.out.println(iData+",");

    }

 

}

节点Link包含数据项iData和一个指向下一个节点的引用(地址)next.
(2)
操作

上图中first节点不存放数据,只是作为指向第一个节点地址,表示链表的开始端。

[java]view plaincopyprint?

1. public class SimpleLinkList   

2. {  

3. <strong><span style="color: rgb(255, 0, 0);">   //指向第一节点ref to first link on list  

4.     private Link first;  

5. </span></strong>      

6.     public void SimpleLinkList()  

7.     {  

8.         first=null;  

9.     }  

10.      

11.    //true if link is empty  

12.    public boolean isEmpty()  

13.    {  

14.        return (first==null);  

15.    }  

16.      

17.    //insert at start of list  

18.    public void insertFirst(int iData)  

19.    {         

20.        Link newLink=new Link(iData);  

21.        newLink.next=first;  

22.        first=newLink;        

23.    }  

public class SimpleLinkList

{

//指向第一节点ref tofirst link on list

private Link first;

public void SimpleLinkList()

{

  first=null;

}

//true if link is empty

public boolean isEmpty()

{

  return (first==null);

}

//insert at start of list

public void insertFirst(int iData)

  Link newLink=new Link(iData);

  newLink.next=first;

  first=newLink; 

}

[java]view plaincopyprint?

1. //delete first item  

2. public Link deleteFirst()  

3. {  

4.     Link tempLink=first;  

5.     first=first.next;  

6.       

7.     return tempLink;  

8. }  

9.       

10.public void dispalyList()  

11.{  

12.span style="color: rgb(255, 0, 0);">        //start at begging of first  

13.    Link current=first;</span>  

14.    while(current!=null)  

15.    {  

16.        //print data  

17.        current.dispaly();  

18.        //move to next link  

19.        current=current.next;  

20.    }  

21.}  

22.  

23.//find link with given key  

24.public Link find(int key)  

25.{  

26.    Link current=first;  

27.      

28.    while(current.iData !=key)  

29.    {  

30.        //if end of list, didn't find it  

31.        if(current.next==null)  

32.        {  

33.            return null;  

34.        }  

35.        else  

36.        {  

37.            current=current.next;  

38.        }             

39.    }  

40.      

41.    return current;  

42.}  

43.  

44.public Link delete(int key)  

45.{  

46.    Link current=first;  

47.    Link previous=first;  

48.    while(current.iData !=key)  

49.    {  

50.        if(current.next==null)  

51.        {  

52.            return null;  

53.        }  

54.        else  

55.        {  

56.            previous=current;  

57.            current=current.next;  

58.        }     

59.    }  

60.      

61.    //if current refer to first, then first refer to next, because there is no previous at this situation  

62.    if(current==first)  

63.        first=first.next;  

64.    else  

65.        previous.next=current.next;  

66.      

67.    return current;  

68.}     

//delete first item

public Link deleteFirst()

{

Link tempLink=first;

first=first.next;

return tempLink;

}

public void dispalyList()

{

//start at begging of first

Link current=first;

while(current!=null)

{

//print data

current.dispaly();

//move to next link

current=current.next;

}

}

//find link with given key

public Link find(int key)

{

Link current=first;

while(current.iData !=key)

{

//if end of list, didn't find it

if(current.next==null)

{

  return null;

}

else

{

  current=current.next;

}

}

return current;

}

public Link delete(int key)

{

Link current=first;

Link previous=first;

while(current.iData !=key)

{

if(current.next==null)

{

  return null;

}

else

{

  previous=current;

  current=current.next;

}

}

//if current refer to first, thenfirst refer to next, because there is no previous at this situation

if(current==first)

first=first.next;

else

previous.next=current.next;

return current;

}

insertFirst方法是将节点插入链表开始端,可用下图表示该该过程

deleteFirst方法是删除第一个节点,具体过程如下

3、效率Efficiency

1)插入、删除操作

单向链表在链表开始处插入、删除操作的时间复杂度是O(1),因为不需要像顺序表(顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。)那样在插入、删除时移动其他节点,所以单向链表插入、删除比顺序表快。

2)查找

单向链表查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)O(1)

3)存储空间

使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值