Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include "prog.h"
  3.  
  4. using namespace std;
  5.  
  6. list::list(){
  7. head=NULL;
  8. tail=NULL;
  9. }
  10.  
  11. //insert functions
  12. void list::push(int n){
  13. node *temp=new node;
  14. temp->data=n;
  15. temp->next=head;
  16. head=temp;
  17. }
  18.  
  19. void list::insert_place(int place, int n){
  20. node *temp=new node;
  21. node *pre_cycle=new node;
  22. node *cycle=new node;
  23. cycle=head;
  24. if(place==1){
  25. push(n);
  26. }else{
  27. for (int i=1; i<place; i++){
  28. pre_cycle=cycle;
  29. cycle=cycle->next;
  30. }
  31. temp->data=n;
  32. pre_cycle->next=temp;
  33. temp->next=cycle;
  34. }
  35. }
  36.  
  37. void list::insert_end(int n){
  38. node *last = new node;
  39. node *temp = new node;
  40. last->data=n;
  41. last->next=NULL;
  42. temp=head;
  43. while(temp->next!=NULL){
  44. temp=temp->next;
  45. }
  46. temp->next=last;
  47. }
  48.  
  49. //delete functions
  50. void list::delete_start(){
  51. node *temp=new node;
  52. temp=head;
  53. head=head->next;
  54. delete temp;
  55. }
  56.  
  57. void list::delete_place(int place){
  58. node *pre_cycle=new node;
  59. node *cycle=new node;
  60. cycle=head;
  61. if(place==1){
  62. delete_start();
  63. }else{
  64. for (int i=1; i<place; i++){
  65. pre_cycle=cycle;
  66. cycle=cycle->next;
  67. }
  68. pre_cycle->next=cycle->next;
  69. }
  70. }
  71.  
  72. void list::delete_end(){
  73. node *last = new node;
  74. node *temp = new node;
  75. last->next=NULL;
  76. temp=head;
  77. while(temp->next!=NULL){
  78. temp=temp->next;
  79. }
  80. temp->next=last;
  81. delete temp;
  82. }
  83.  
  84. int list::size(){
  85. node *temp = new node;
  86. int size=0;
  87. temp=head;
  88. while(temp!=NULL){
  89. size++;
  90. temp=temp->next;
  91.  
  92. }
  93. return size;
  94. }
  95. //sorting functions
  96. bool list::compare(node *a, node *b){
  97. return(a->data > b->data);
  98. }
  99.  
  100. void list::swap(node *a, node *b){
  101. node *temp = new node;
  102. if(a==head){
  103. head=b;
  104. a->next=b->next;
  105. b->next=a;
  106. return;
  107. }
  108. while(temp->next!=a){
  109. temp=temp->next;
  110. }
  111. temp->next=b;
  112. a->next=b->next;
  113. b->next=a;
  114.  
  115. }
  116.  
  117. //fix this trash
  118. void list::bubble_sort(){
  119. node *first = new node;
  120. first=head;
  121. while(first&&first->next!=NULL){
  122. node *temp = new node;
  123. for(temp=first; temp->next!=NULL;temp=temp->next){
  124. //if(compare(temp, temp->next)){
  125. // swap(temp->next, temp);
  126. //}
  127. }
  128. }
  129. }
  130.  
  131. //output functions
  132.  
  133. void list::display(){
  134. node *temp=new node;
  135. temp=head;
  136. while(temp!=NULL){
  137. cout<<temp->data<<endl;
  138. temp=temp->next;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement