Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 29.09.2013, 12:00   #1
bayanist17
 
Регистрация: 01.11.2012
Сообщений: 4
По умолчанию Ребят, уже запарился, гляньте, что не так!? конструктор копирования и оператор присваивания

Код:
#include <iostream>
using namespace std;


struct SNode {
	  SNode*next;
	  int val;
	  SNode(){ next = NULL; val = 0;};
	  SNode(SNode* ne,int a){val=a; next=ne;}
	 
};


class SList
 {
  SNode* head;
  SNode* tail;
	public:
	SList();
	SList(int);
	SList(const SList&); //конструктор копирования
	~SList();

	bool is_empty();
	void add_front(int);
	int remove_front();
	bool find(int);
	int get_nth(int);
	int size();
	void print_slist();

  SList& operator=(const SList&); //опрератор присваивания
};
SList::SList(){
	head=new SNode();
	tail=head;
}
SList::SList(int a){
	tail=new SNode();
	head=new SNode(tail,a);
}
//+++++++++++++++++++++++++++++
  SList::SList(const SList& t) {
  
while(t.head!=t.tail)
	{
					//head=t.head;
				//head=head->next;
				head=new SNode(head,t.head->val);
		}
   tail = t.tail;

   }
 //++++++++++++++++++++++++++++++
bool SList::is_empty(){
	return (head==tail)?true:false;
}
void SList::add_front(int a){
	SNode *q=head;
	head=new SNode(q,a);
}
int SList::remove_front(){
	SNode* q=head;
	head=head->next;
	int a=q->val;
	delete q;
	return a;
}
bool SList::find(int a){
	SNode* q=head;
	while(q!=tail)
	{
		if(q->val==a)
			return true;
		else
			q=q->next;
	}
	return false;
}
int SList::get_nth(int a){
	SNode* q=this->head;
	for(int i=0;i<a;i++)
		q=q->next;
	return q->val;
}
int SList::size(){
	SNode *q=head;
	int k=0;
	for(;q!=tail;q=q->next,k++)
		;
	return k;
}
void SList::print_slist()
{
	int size=this->size();
	for(int i=0;i<size;i++)
	{
		cout<<this->get_nth(i)<<' ';
	}
	cout<<endl;
}
SList::~SList(){
      	while(head!=tail)
	{
		SNode* p=head;
		head=head->next;
		delete p;
	}
	delete tail;
}

SList& SList::operator=(const SList& t)  {
		// защита от неправильного самоприсваивания
	if (this == & t) return *this;
	else{
		// освобождаем старую память
	while(head!=tail)
	{
		SNode* p=head;
		head=head->next;
		delete p;
	}
	delete tail;

	// присваиваем значения в новой памяти объекту
while(t.head!=t.tail)
	{
					head=t.head;
				//head=head->next;
				head=new SNode(head,t.head->val);
		}
   tail = t.tail;
  return *this; }
   }


int _tmain(int argc, _TCHAR* argv[])
{

	SList L1;
L1.add_front(5);
L1.add_front(9);
L1.add_front(34);
	SList L2;
L2.add_front(25);
L1.print_slist();
L2.print_slist();

cout<<"posle = "<<endl;
	L1=L2;

L1.print_slist();
L2.print_slist();


system("pause");
	return 0;
}

Последний раз редактировалось Stilet; 29.09.2013 в 12:58.
bayanist17 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Выделение памяти New. Конструкторы копирования и присваивания. Suby Общие вопросы C/C++ 33 21.12.2012 23:08
Ребят, что я делаю не так? одномерный массив. rtif Паскаль, Turbo Pascal, PascalABC.NET 3 26.03.2012 03:56
Конструктор копирования и оператор присваивания DenisS0 Общие вопросы C/C++ 4 05.12.2011 10:41
Ребят есть программа, но что-то не так. Henkel Помощь студентам 1 21.12.2010 21:05