Advertisement
sebyvs

lsi crescaor

May 26th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. struct Nod
  7. {
  8. int info;
  9. Nod *leg;
  10. };
  11.  
  12. Nod *first, *last;
  13.  
  14. int main()
  15. {
  16. int n, i, x;
  17.  
  18. ifstream fin("sir.in");
  19. fin >> n;
  20. fin >> x;
  21.  
  22. first = new Nod;
  23. first -> info = x;
  24. first -> leg = NULL;
  25. last = first;
  26.  
  27. for(i = 1; i < n; i++)
  28. {
  29. fin >> x;
  30. if(x <= first -> info)
  31. {
  32. Nod *p;
  33. p = new Nod;
  34. p -> info = x;
  35. p -> leg = first;
  36. first = p;
  37. }
  38. else if(x >= last -> info)
  39. {
  40. Nod *q;
  41. q = new Nod;
  42. q -> info = x;
  43. q -> leg = NULL;
  44. last -> leg = q;
  45. last = q;
  46. }
  47. else
  48. {
  49. Nod *p,*q;
  50. for(p = first; x > p->info; p = p->leg)
  51. q = p;
  52.  
  53. p = new Nod;
  54. p -> info = x;
  55. p -> leg = q -> leg;
  56. q -> leg = p;
  57.  
  58. }
  59. }
  60. fin.close();
  61.  
  62. for(Nod *p = first; p != NULL; p = p->leg)
  63. cout << p->info <<" ";
  64. cout << "\n";
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement