单链表逆序算法

今天去参加笔试,⊙﹏⊙b汗,单链表逆序算法竟然不会写了,真丢人了。

回来好好写了一遍.....

Java

Code:
  1. public class Node     
  2. {   
  3.     int data;   
  4.     Node next=null;   
  5.     public Node(int data){   
  6.       this.data=data;   
  7.     }   
  8.     public static Node reverse(Node head){   
  9.       Node p=null;   
  10.       Node q=head;   
  11.       while(head.next!=null){   
  12.          p=head.next;   
  13.          head.next=p.next;   
  14.          p.next=q;   
  15.          q=p;   
  16.       }   
  17.       return q;   
  18.     }   
  19.   
  20.   
  21.     public static void main(String[] args) {   
  22.         Node head=new Node(0);   
  23.         Node tail=head;   
  24.         for(int i=1;i<10;i++){   
  25.            Node p=new Node(i);   
  26.            tail.next=p;   
  27.            tail=p;   
  28.         }   
  29.         head=reverse(head);   
  30.         while(head.next!=null){   
  31.           System.out.println(head.data);   
  32.           head=head.next;   
  33.         }   
  34.     }   
  35. }  

C语言

Code:
  1. #include<stdio.h>   
  2. #include<stdlib.h>   
  3. #definestacksize100   
  4. typedefintdatatype;   
  5. typedefstruct   
  6. {   
  7. datatypedata[stacksize];   
  8. int top;   
  9. }seqstack;   
  10. typedefstructnode{   
  11. datatype data;   
  12. structnode*next;   
  13. }listnode;   
  14. typedeflistnode*linklist;   
  15. linklisthead;   
  16. linklistp;   
  17. intcount;   
  18. linklistcreatlist(intn)   
  19. {   
  20. linklisthead;   
  21. listnode*p1,*p2;   
  22. inti;   
  23. head=(linklist)malloc(sizeof(listnode));   
  24. head->next=NULL;   
  25. p2=head;   
  26. printf("Pleaseinputtherecordsofthechain!/n");   
  27. for(i=0;i<n;i )   
  28. {   
  29. p1=(linklist)malloc(sizeof(listnode));   
  30. scanf("%d",&p1->data);   
  31. p1->next=p2->next;   
  32. p2->next=p1;   
  33. p2=p1;   
  34. }   
  35. return(head);   
  36. }   
  37. voidprint(linklisthead,intn)   
  38. {   
  39. if(head==NULL)   
  40. printf("listnull!/n");   
  41. printf("Nowthese%drecordsare:/n",n);   
  42. p=head->next;   
  43. printf("M",p->data);   
  44. count=1;   
  45. while(p->next!=NULL)   
  46. {   
  47. count ;   
  48. p=p->next;    
  49.   
  50.   
  51. printf("M",p->data);   
  52. if(!(count))   
  53. printf("/n");   
  54. }   
  55. }   
  56. datatypepush(seqstack*s,intx)    
  57. {   
  58. if(s->top==stacksize-1)   
  59. printf("Thestackisoverflow!/n");   
  60. else  
  61. s->data[ s->top]=x;    
  62. }   
  63. datatypepop(seqstack*s)    
  64. {   
  65. if(s->top==-1)   
  66. printf("thestackisempty!/n");   
  67. else  
  68. return(s->data[s->top--]);   
  69. }   
  70. datatypedeleted(linklisthead)   
  71. {   
  72. datatypetemp;   
  73. linklistp;   
  74. p=head->next;   
  75. temp=(p->data);   
  76. head->next=p->next;   
  77. free(p);   
  78. return(temp);   
  79. }   
  80. voidinvertedlist(linklisthead,intn)   
  81. {    
  82. seqstacks;   
  83. inti,j,temp;   
  84. s.top=-1;   
  85. for(i=0;i<n;i )   
  86. {   
  87. temp=deleted(head);   
  88. push(&s,temp);    
  89. }   
  90. for(i=0;i<n;i )   
  91. {   
  92. temp=pop(&s);   
  93. printf("]",temp);   
  94. if(!((i 1)))   
  95. printf("/n");   
  96. }   
  97. printf("/n");   
  98. }   
  99. main()   
  100. {   
  101. linklisthead;    
  102.   
  103.   
  104. intn;   
  105. printf("Pleaseinputthevalueofn:/n");   
  106. scanf("%d",&n);   
  107. head=creatlist(n);   
  108. print(head,n);   
  109. printf("/n");   
  110. printf("Afterinverting,therecordsofthechainare:/n");   
  111. invertedlist(head,n);   
  112. system("pause");   
  113. return0;   
  114. }   

 

 

C++

Code:
  1. #include<iostream.h>   
  2. #include<stdlib.h>   
  3. typedef struct Lnode   
  4. {   
  5. int data;   
  6. struct Lnode *next;   
  7. }*LinkList;   
  8. void CreateList(LinkList &L,int n)   
  9. {   
  10. LinkList p;   
  11. int i;   
  12. L=(LinkList)malloc(sizeof(Lnode));   
  13. L->next=0;   
  14. for(i=n;i>0;i--)   
  15. {   
  16.    p=(LinkList)malloc(sizeof(Lnode));   
  17.    p->data=i;   
  18.    p->next=L->next;   
  19.    L->next=p;   
  20. }   
  21. }   
  22. void ob(LinkList &L)   
  23. {   
  24. LinkList p,q;   
  25. p=L;   
  26. p=p->next;   
  27. L->next=0;   
  28. while(p!=0)   
  29. {   
  30.    q=p;   
  31.    p=p->next;   
  32.    q->next=L->next;   
  33.    L->next=q;   
  34. }   
  35. }   
  36. void out(LinkList L)   
  37. {   
  38. LinkList p;   
  39. p=L;   
  40. p=p->next;   
  41. while(p)   
  42. {   
  43.    cout<<p<<"/t"<<p->data<<"/t"<<p->next<<endl;   
  44.    p=p->next;   
  45. }   
  46. }   
  47. void main()   
  48. {   
  49. LinkList L;   
  50. CreateList(L,10);   
  51. out(L);   
  52. ob(L);   
  53. cout<<endl;   
  54. out(L);   
  55. }   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值