Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "list.h"
- #include <iostream>
- list::list()
- {
- head = NULL;
- }
- list::~list()
- {
- for(int i=0;i<this->count();i++)
- {
- node* tmp=head->next;
- delete head;
- head = tmp;
- }
- head = NULL;
- }
- 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 --()
- {
- node* tmp=head;
- if(!head) std::cout << "Список пуст\n";
- else head=head->next;
- delete tmp;
- }
- 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 0;
- else while(i>0){ if(a->value!=b->value) return 0;
- else a=a->next; b=b->next; i--; }
- return 1;
- }
- list& list::operator+(list& A)
- {
- node* a = this->head;
- while(a->next!=NULL) a=a->next;
- a->next=A.head;
- return *this;
- }
- bool list::operator()()
- {
- return head!=NULL;
- }
- // list.h
- #pragma once
- class list
- {
- struct node
- {
- int value;
- node* next;
- node(int a,node* b)
- {
- value = a;
- next = b;
- }
- };
- node* head;
- public:
- list(void);
- ~list(void);
- void addend(int);
- void getend();
- void gethead();
- void addhead(int);
- void showlist();
- int count();
- list& operator + (list& A);
- bool operator ()();
- void operator --();
- bool operator == (list& A);
- };
- // lab.cpp
- // OOP 6.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include "list.h"
- #include <iostream>
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_CTYPE,"rus");
- list a;
- a.addend(5);
- a.addhead(9);
- a.addend(6);
- a.showlist();
- list b;
- b.addhead(9);
- b.addend(5);
- b.addend(6);
- a.showlist();
- b.showlist();
- std::cout << (a==b) << "\n";
- a.addend(7);
- std::cout << (a==b) << "\n";
- a.showlist();
- b.showlist();
- a=a+b;
- a.showlist();
- std::cout << b() << "\n";
- b--;
- b--;
- b--;
- std::cout << b() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement