Advertisement
Guest User

laba 6

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