算法-----链表

  1. packagecom.eshore.sweetop.dataframe;
  2. publicclassMyLinkedList{
  3. privateElementhead;
  4. publicvoidinsert(intx){
  5. Elemente=newElement(x);
  6. e.next=head;
  7. if(head!=null){
  8. head.pre=e;
  9. }
  10. head=e;
  11. e.pre=null;
  12. }
  13. privateElementsearch(intk){
  14. Elemente=head;
  15. while(e!=null&&e.key!=k){
  16. e=e.next;
  17. }
  18. returne;
  19. }
  20. privatevoiddelete(intx){
  21. Elemente=search(x);
  22. if(e.pre!=null){
  23. e.pre.next=e.next;
  24. }else{
  25. head=e.next;
  26. }
  27. if(e.next!=null){
  28. e.next.pre=e.pre;
  29. }
  30. }
  31. publicStringtoString(){
  32. StringBuildersb=newStringBuilder();
  33. sb.append("[");
  34. Elemente=head;
  35. while(e!=null){
  36. sb.append(e.key);
  37. sb.append(",");
  38. e=e.next;
  39. }
  40. sb.setCharAt(sb.length()-1,']');
  41. returnsb.toString();
  42. }
  43. privateclassElement{
  44. privateElementpre;
  45. privateElementnext;
  46. publicintkey;
  47. publicElement(intx){
  48. this.key=x;
  49. }
  50. }
  51. publicstaticvoidmain(String[]args){
  52. MyLinkedListlist=newMyLinkedList();
  53. list.insert(3);
  54. list.insert(6);
  55. list.insert(4);
  56. list.delete(6);
  57. System.out.println(list);
  58. }
  59. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值