Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Node{
  9. int val;
  10. Node *next;
  11. };
  12.  
  13. void InsertLast(Node *&head,int n)
  14. {
  15. if(head==NULL)
  16. {
  17. Node * aux=new Node;
  18. aux->val=n;
  19. aux->next=aux;
  20.  
  21. head=aux;
  22.  
  23. return;
  24. }
  25.  
  26. if(head->next==head)
  27. {
  28. Node * aux=new Node;
  29. aux->val=n;
  30. head->next=aux;
  31. aux->next=head;
  32.  
  33. return;
  34. }
  35.  
  36. Node * aux=new Node;
  37. aux->val=n;
  38.  
  39. aux->next=head->next;
  40. head->next=aux;
  41. int temp=aux->val;
  42. aux->val=head->val;
  43. head->val=temp;
  44.  
  45. head=aux;
  46.  
  47. }
  48.  
  49. void ShowList(Node *head)
  50. {
  51. Node *aux=head;
  52. while(aux!=NULL)
  53. {
  54. cout<<aux->val<<" ";
  55. aux=aux->next;
  56. }
  57. }
  58.  
  59. void Insert_One(Node *&head, int n) // Studentul Popescu
  60. {
  61. if(head==NULL)
  62. {
  63. Node * aux=new Node;
  64. aux->val=n;
  65. aux->next=aux;
  66.  
  67. head=aux;
  68.  
  69. return;
  70. }
  71.  
  72. Node *aux=head;
  73.  
  74. while(aux->next!=head)
  75. {
  76. aux=aux->next;
  77. }
  78.  
  79. Node *temp=new Node;
  80. temp->val=n;
  81. aux->next=temp;
  82. temp->next=head;
  83. }
  84.  
  85. void Insert_Two(Node *&last,int n) // Studentul Ionescu
  86. {
  87. if(last==NULL)
  88. {
  89. Node * aux=new Node;
  90. aux->val=n;
  91. aux->next=aux;
  92.  
  93. last=aux;
  94.  
  95. return;
  96. }
  97.  
  98. Node *aux=new Node;
  99. aux->val=n;
  100. aux->next=last->next;
  101. last->next=aux;
  102. last=aux;
  103. }
  104.  
  105.  
  106. int main(void)
  107. {
  108. Node *head=NULL,*tail=NULL;
  109. clock_t start, finish;
  110. long loop;
  111.  
  112. double result,r2,elapsed_time;
  113.  
  114. start=clock();
  115.  
  116. for(int i=0;i<10000;++i)
  117. {
  118. Insert_One(head,i);
  119. }
  120.  
  121. finish=clock();
  122.  
  123. elapsed_time = (double)(finish - start) / CLOCKS_PER_SEC;
  124. cout<<"Popescu: "<<elapsed_time<<"\n";
  125.  
  126.  
  127.  
  128. start=clock();
  129.  
  130. for(int i=0;i<10000;++i)
  131. {
  132. Insert_Two(head,i);
  133. }
  134.  
  135. finish=clock();
  136.  
  137. elapsed_time = (double)(finish - start) / CLOCKS_PER_SEC;
  138. cout<<"Ionescu: "<<elapsed_time<<"\n";
  139.  
  140.  
  141.  
  142. //ShowList(list);
  143.  
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement