Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. list.h
  2.  
  3. #pragma once
  4. #include <iostream>
  5. using namespace std;
  6. class list
  7. {  
  8. struct node
  9. {
  10.     int value;
  11.     node* next;
  12.     node(int a,node* b)
  13.     {
  14.         value = a;
  15.         next = b;
  16.     }
  17.    
  18. };
  19. node* head;
  20. public:
  21.     friend ostream &operator << (ostream &out,const node &a);
  22.     list(void);
  23.     ~list(void);
  24.     void addend(int);
  25.     void getend();
  26.     void gethead();
  27.     void addhead(int);
  28.     void showlist();
  29.     int count();
  30.     list& operator + (int A);
  31.     bool operator ()();
  32.     void operator --(int a);
  33.     bool operator != (list& A);
  34.     node& operator[](int i);
  35.     friend ostream &operator << (ostream &out,const list &a);
  36.     friend istream &operator >> (istream &in,list &a);
  37.  
  38. };
  39.  
  40.  
  41.  
  42. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  43.  
  44. list.cpp
  45.  
  46. #include "StdAfx.h"
  47. #include "list.h"
  48. #include <iostream>
  49. using namespace std;
  50. list::list()
  51. {
  52.     head = NULL;
  53. }
  54.  
  55.  
  56. list::~list()
  57. {
  58.     for(int i=0;i<this->count();i++)
  59.     {
  60.         node* tmp=head->next;
  61.         delete head;
  62.         head = tmp;
  63.     }
  64.     head = NULL;
  65. }
  66.  
  67.  ostream &operator << (ostream &out,const list::node &a)
  68.         {
  69.             out << a.value;
  70.             return out;
  71.     };
  72.  
  73. void list::addend(int x)
  74. {
  75.     node* a = new node(x,NULL);
  76.     node* tmp=head;
  77.   if (!head) head=a;
  78.   else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
  79. }
  80.  
  81.  
  82. void list::showlist()
  83. {
  84.     node* a = head;
  85.     while(a)
  86.     {
  87.     std::cout << a->value << "   ";
  88.     a=a->next;
  89.     }
  90.     std::cout << "\n";
  91. }
  92.  
  93. void list::addhead(int a){
  94.  
  95.     if(!head) head = new node(a,NULL);
  96.     else head = new node(a,head);
  97. }
  98.  
  99. void list::gethead()
  100. {
  101.     node* tmp=head;
  102.     if(!head) std::cout << "Список пуст\n";
  103.     else std::cout <<"Значение первого элемента: " << head->value << "\n";
  104.     head=head->next;
  105.     delete tmp;
  106. }
  107.  
  108. void list::getend()
  109. {
  110.     node* tmp=head;
  111.     if(!head) std::cout << "Список пуст\n";
  112.     else {
  113.         while(tmp->next->next!=NULL)
  114.             tmp=tmp->next;
  115.         std::cout << "Последний элемент списка: " << tmp->next->value << "\n";
  116.         delete tmp->next;
  117.         tmp->next=NULL;
  118.        
  119.     }
  120.    
  121. }
  122.  
  123. void list::operator --(int a)
  124. {
  125.     node* tmp=head;
  126.     while(tmp->next->next!=NULL) tmp=tmp->next;
  127.     delete tmp->next;
  128.     tmp->next=NULL;
  129.    
  130. }
  131.  
  132. int list::count()
  133. {
  134.     node* a=head;
  135.     int i=0;
  136.     if(head) while(a){ a=a->next; i++; }
  137.     return i;
  138. }
  139. bool list::operator != (list & A)
  140. {
  141.     node*  a = head;
  142.     node* b = A.head;
  143.     int i=this->count();
  144.     if(i!=A.count()) return 1;
  145.     else while(i>0){ if(a->value!=b->value) return 1;
  146.     else a=a->next; b=b->next; i--; }
  147.     return 0;
  148. }
  149.  
  150. list& list::operator+(int A)
  151. {
  152.     node* a = new node(A,NULL);
  153.     node* tmp=head;
  154.   if (!head) head=a;
  155.   else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
  156.   return *this;
  157. }
  158. list::node& list::operator[](int i)
  159. {
  160.     node* tmp=head;
  161.     while(i) {tmp=tmp->next;
  162.     i--;}
  163.     return *tmp;
  164. }
  165. bool list::operator()()
  166. {
  167.     return head!=NULL;
  168. }
  169.  
  170. std::ostream& operator<<(std::ostream& os, const list& dt)
  171.     {
  172.     list::node* b = dt.head;
  173.     while(b)
  174.     {
  175.     os << b->value << "   ";
  176.     b=b->next;
  177.     }
  178.     os << "\n";
  179.     return os;
  180. };
  181.  
  182. std::istream& operator >>(std::istream& in,list& dt)
  183. {
  184.  
  185.     int x,i=0;
  186.     cout << "Vvtdite kol-vo elementov kotoroe hotite vvesti:\n";
  187.     in >> i;
  188.     while(i){
  189.             in >> x;
  190.     list::node* a = new list::node(x,NULL);
  191.     list::node* tmp=dt.head;
  192.   if (!dt.head) dt.head=a;
  193.   else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
  194.   i--;
  195.     }
  196.   return in;
  197. }
  198.  
  199.  
  200.  
  201.  
  202. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  203.  
  204. lab6.cpp
  205.  
  206. #include "stdafx.h"
  207. #include "list.h"
  208. #include <iostream>
  209.  
  210. using namespace std;
  211.  
  212. int _tmain(int argc, _TCHAR* argv[])
  213. {
  214.     setlocale(LC_CTYPE,"rus");
  215.     list A,B;
  216.     A=A+2;
  217.     A=A+3;
  218.     A=A+4;
  219.     A=A+5;
  220.     B=B+2;
  221.     B=B+3;
  222.     B=B+4;
  223.     A.showlist();
  224.     B.showlist();
  225.     cout << A[3] << "\n"<<(A!=B)<<"\n";
  226.     A--;
  227.     A.showlist();
  228.     B.showlist();
  229.     cout << (A!=B) << "\n";
  230.  
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement