Java数据结构和算法--链表

(1)简单链表

Java代码 复制代码
  1. packageChapterFive;
  2. classLink<E>{
  3. publicEdata;
  4. publicLink<E>next;
  5. publicLink(Edata){
  6. this.data=data;
  7. }
  8. }
  9. classLinkList<E>{
  10. publicLink<E>first;
  11. //链表中数据项的个数
  12. publicintsize;
  13. publicLinkList(){
  14. first=null;
  15. size=0;
  16. }
  17. //在表头插入新的数据
  18. publicvoidinsertFirst(Evalue){
  19. Link<E>link=newLink<E>(value);
  20. link.next=first;
  21. first=link;
  22. size++;
  23. }
  24. //判断链表是否为空
  25. publicbooleanisEmpty(){
  26. returnsize==0;
  27. }
  28. //删除表头
  29. publicLink<E>deleteFirst(){
  30. Link<E>temp=first;
  31. first=first.next;
  32. size--;
  33. returntemp;
  34. }
  35. //输出链表中的所有数据
  36. publicvoiddisplay(){
  37. Link<E>curr=first;
  38. while(curr!=null){
  39. System.out.print(curr.data+"");
  40. curr=curr.next;
  41. }
  42. System.out.println();
  43. }
  44. //返回链表中数据项的个数
  45. publicintsize(){
  46. returnsize;
  47. }
  48. //获取从头至尾的第i个数据项
  49. publicLink<E>get(inti){
  50. if(i>size()-1||i<0)
  51. try{
  52. thrownewIndexOutOfBoundsException();
  53. }catch(Exceptione){
  54. e.printStackTrace();
  55. }
  56. Link<E>curr=first;
  57. for(intn=0;n<size();n++){
  58. if(n==i)
  59. returncurr;
  60. else
  61. curr=curr.next;
  62. }
  63. returnnull;
  64. }
  65. //输出从头至尾的第i个数据项
  66. publicvoidremove(inti){
  67. if(i==0)
  68. deleteFirst();
  69. elseif(i==size()-1)
  70. get(i-1).next=null;
  71. else{
  72. get(i-1).next=get(i+1);
  73. }
  74. size--;
  75. }
  76. }
  77. publicclassLink_list{
  78. publicstaticvoidmain(String[]args){
  79. LinkList<Long>ll=newLinkList<Long>();
  80. for(inti=0;i<10;i++){
  81. Longvalue=(long)(Math.random()*100);
  82. ll.insertFirst(value);
  83. }
  84. ll.display();
  85. while(!ll.isEmpty()){
  86. ll.deleteFirst();
  87. ll.display();
  88. }
  89. System.out.println("Ok");
  90. }
  91. }


(2)链栈

Java代码 复制代码
  1. packageChapterFive;
  2. classLinkStack<E>{
  3. LinkList<E>linkList;
  4. intsize;
  5. publicLinkStack(){
  6. size=0;
  7. linkList=newLinkList<E>();
  8. }
  9. //入栈
  10. publicvoidpush(Evalue){
  11. linkList.insertFirst(value);
  12. size++;
  13. }
  14. //出栈
  15. publicLink<E>pop(){
  16. size--;
  17. returnlinkList.deleteFirst();
  18. }
  19. //返回栈顶元素
  20. publicLink<E>top(){
  21. returnlinkList.first;
  22. }
  23. //判断栈是否为空
  24. publicbooleanisEmpty(){
  25. returnsize==0;
  26. }
  27. //显示栈中的全部数据
  28. publicvoiddisplay(){
  29. linkList.display();
  30. }
  31. }
  32. publicclassLink_stack{
  33. publicstaticvoidmain(String[]args){
  34. LinkStack<Long>ls=newLinkStack<Long>();
  35. for(inti=0;i<10;i++){
  36. Longvalue=newLong((long)(Math.random()*100));
  37. ls.push(value);
  38. }
  39. while(!ls.isEmpty()){
  40. ls.pop();
  41. ls.display();
  42. }
  43. System.out.println("Ok");
  44. }
  45. }


(3)有序表

Java代码 复制代码
  1. packageChapterFive;
  2. classSortedLink{
  3. publicLink<Long>first;
  4. intsize;
  5. publicSortedLink(){
  6. first=null;
  7. size=0;
  8. }
  9. //向有序链表中插入数据
  10. publicvoidinsert(longvalue){
  11. Link<Long>newLink=newLink<Long>(value);
  12. Link<Long>previous=null;
  13. Link<Long>curr=first;
  14. while(curr!=null&&(value>curr.data)){
  15. previous=curr;
  16. curr=curr.next;
  17. }
  18. if(previous==null)//链表为空(在表头插入)
  19. first=newLink;
  20. else
  21. previous.next=newLink;//插入新的节点
  22. newLink.next=curr;
  23. size++;
  24. }
  25. //删除第一个节点
  26. publicLink<Long>remove(){
  27. Link<Long>temp=first;
  28. first=first.next;
  29. size--;
  30. returntemp;
  31. }
  32. //判断链表是否为空
  33. publicbooleanisEmpty(){
  34. returnsize==0;
  35. }
  36. //输出链表的所有数据
  37. publicvoiddisplay(){
  38. Link<Long>curr=first;
  39. while(curr!=null){
  40. System.out.print(curr.data+"");
  41. curr=curr.next;
  42. }
  43. System.out.println();
  44. }
  45. }
  46. publicclassSortedLinkApp{
  47. publicstaticvoidmain(String[]args){
  48. SortedLinksl=newSortedLink();
  49. for(inti=0;i<10;i++){
  50. sl.insert((long)(Math.random()*100));
  51. }
  52. while(!sl.isEmpty()){
  53. sl.remove();
  54. sl.display();
  55. }
  56. }
  57. }


(4)双向链表

Java代码 复制代码
  1. packageChapterFive;
  2. classDoubleLink<E>{
  3. publicLink<E>first;
  4. publicLink<E>last;
  5. intsize;
  6. @SuppressWarnings("hiding")
  7. classLink<E>{
  8. publicEdata;
  9. publicLink<E>next;//链表的下一项
  10. publicLink<E>previous;//链表的前一项
  11. publicLink(Evalue){
  12. this.data=value;
  13. }
  14. }
  15. publicDoubleLink(){
  16. first=null;
  17. last=null;
  18. size=0;
  19. }
  20. //在链表的首部插入一项
  21. publicvoidinsertFirst(Evalue){
  22. Link<E>newLink=newLink<E>(value);
  23. if(isEmpty())//如果链表为空则first==last
  24. last=newLink;
  25. else
  26. first.previous=newLink;//确定原first与newLink的前后关系
  27. newLink.next=first;
  28. first=newLink;//设置新的first值
  29. size++;
  30. }
  31. //在链表的尾部插入一项
  32. publicvoidinsertLast(Evalue){
  33. Link<E>newLink=newLink<E>(value);
  34. if(isEmpty())//如果链表为空则last==first
  35. first=newLink;
  36. else{
  37. last.next=newLink;//确定原last与newLink的前后关系
  38. newLink.previous=last;
  39. }
  40. last=newLink;//设置新的last值
  41. size++;
  42. }
  43. //删除双向链表的表头
  44. publicLink<E>deleteFirst(){
  45. Link<E>temp=first;
  46. if(first.next==null)//链表中只有一项数据
  47. last=null;
  48. else
  49. first.next.previous=null;//销毁原链表的头部
  50. first=first.next;
  51. size--;
  52. returntemp;
  53. }
  54. //删除链表的最后一项
  55. publicLink<E>deleteLast(){
  56. Link<E>temp=last;
  57. if(first.next==null)//链表中只有一项数据
  58. first=null;
  59. else
  60. last.previous.next=null;//销毁原链表的尾部
  61. last=last.previous;
  62. size--;
  63. returntemp;
  64. }
  65. //判断链表是否为空
  66. publicbooleanisEmpty(){
  67. returnsize==0;
  68. }
  69. //输出链表中的所有数据项
  70. publicvoiddisplay(){
  71. Link<E>curr=first;
  72. while(curr!=null){
  73. System.out.print(curr.data+"");
  74. curr=curr.next;
  75. }
  76. System.out.println();
  77. }
  78. }
  79. publicclassDoubleLinkApp{
  80. publicstaticvoidmain(String[]args){
  81. DoubleLink<Integer>dl=newDoubleLink<Integer>();
  82. for(inti=0;i<5;i++){
  83. dl.insertFirst((int)(Math.random()*100));
  84. }
  85. for(inti=0;i<5;i++){
  86. dl.insertLast((int)(Math.random()*100));
  87. }
  88. dl.display();
  89. while(!dl.isEmpty()){
  90. dl.deleteFirst();
  91. dl.deleteLast();
  92. dl.display();
  93. }
  94. System.out.println("Ok");
  95. }
  96. }

你可能感兴趣的:(java,数据结构,算法)