Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list.h
- #pragma once
- #include <iostream>
- using namespace std;
- class list
- {
- struct node
- {
- int value;
- node* next;
- node(int a,node* b)
- {
- value = a;
- next = b;
- }
- };
- node* head;
- public:
- friend ostream &operator << (ostream &out,const node &a);
- list(void);
- ~list(void);
- void addend(int);
- void getend();
- void gethead();
- void addhead(int);
- void showlist();
- int count();
- list& operator + (int A);
- bool operator ()();
- void operator --(int a);
- bool operator != (list& A);
- node& operator[](int i);
- friend ostream &operator << (ostream &out,const list &a);
- friend istream &operator >> (istream &in,list &a);
- };
- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
- list.cpp
- #include "StdAfx.h"
- #include "list.h"
- #include <iostream>
- using namespace std;
- list::list()
- {
- head = NULL;
- }
- list::~list()
- {
- for(int i=0;i<this->count();i++)
- {
- node* tmp=head->next;
- delete head;
- head = tmp;
- }
- head = NULL;
- }
- ostream &operator << (ostream &out,const list::node &a)
- {
- out << a.value;
- return out;
- };
- void list::addend(int x)
- {
- node* a = new node(x,NULL);
- node* tmp=head;
- if (!head) head=a;
- else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
- }
- void list::showlist()
- {
- node* a = head;
- while(a)
- {
- std::cout << a->value << " ";
- a=a->next;
- }
- std::cout << "\n";
- }
- void list::addhead(int a){
- if(!head) head = new node(a,NULL);
- else head = new node(a,head);
- }
- void list::gethead()
- {
- node* tmp=head;
- if(!head) std::cout << "Список пуст\n";
- else std::cout <<"Значение первого элемента: " << head->value << "\n";
- head=head->next;
- delete tmp;
- }
- void list::getend()
- {
- node* tmp=head;
- if(!head) std::cout << "Список пуст\n";
- else {
- while(tmp->next->next!=NULL)
- tmp=tmp->next;
- std::cout << "Последний элемент списка: " << tmp->next->value << "\n";
- delete tmp->next;
- tmp->next=NULL;
- }
- }
- void list::operator --(int a)
- {
- node* tmp=head;
- while(tmp->next->next!=NULL) tmp=tmp->next;
- delete tmp->next;
- tmp->next=NULL;
- }
- int list::count()
- {
- node* a=head;
- int i=0;
- if(head) while(a){ a=a->next; i++; }
- return i;
- }
- bool list::operator != (list & A)
- {
- node* a = head;
- node* b = A.head;
- int i=this->count();
- if(i!=A.count()) return 1;
- else while(i>0){ if(a->value!=b->value) return 1;
- else a=a->next; b=b->next; i--; }
- return 0;
- }
- list& list::operator+(int A)
- {
- node* a = new node(A,NULL);
- node* tmp=head;
- if (!head) head=a;
- else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
- return *this;
- }
- list::node& list::operator[](int i)
- {
- node* tmp=head;
- while(i) {tmp=tmp->next;
- i--;}
- return *tmp;
- }
- bool list::operator()()
- {
- return head!=NULL;
- }
- std::ostream& operator<<(std::ostream& os, const list& dt)
- {
- list::node* b = dt.head;
- while(b)
- {
- os << b->value << " ";
- b=b->next;
- }
- os << "\n";
- return os;
- };
- std::istream& operator >>(std::istream& in,list& dt)
- {
- int x,i=0;
- cout << "Vvtdite kol-vo elementov kotoroe hotite vvesti:\n";
- in >> i;
- while(i){
- in >> x;
- list::node* a = new list::node(x,NULL);
- list::node* tmp=dt.head;
- if (!dt.head) dt.head=a;
- else {while(tmp->next!=NULL) tmp=tmp->next; tmp->next=a;}
- i--;
- }
- return in;
- }
- \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
- lab6.cpp
- #include "stdafx.h"
- #include "list.h"
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_CTYPE,"rus");
- list A,B;
- A=A+2;
- A=A+3;
- A=A+4;
- A=A+5;
- B=B+2;
- B=B+3;
- B=B+4;
- A.showlist();
- B.showlist();
- cout << A[3] << "\n"<<(A!=B)<<"\n";
- A--;
- A.showlist();
- B.showlist();
- cout << (A!=B) << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement