Overload the operato

mocha's Avatar, Join Date: Oct 2010
Go4Expert Member
22)Provide other member function as needed. Overload the operator<<and operator>> to read and write the statistic variable. The operator <<will print both the average and standard deviation to an output stream. The operator >> will read on element from the input stream.
Code:
//TEMPLET CLASS
#ifndef_LIST_
#define_LIST_
#include<iostream>
template<typename TYPE>
class Node{
template<typename T> friend class List;
public:
Node() {}
~ Node() {}
Node *prev(){return PREV;}
Node *next(){return NEXT;}
Node &set(Type d){data=d;return*this;}
Node get() {return data;}
Privet:
Node *Next;
Node *prev;
TYPE data;
};
template <typename TYPE>
Class List{
friend std::ostream &operator<<(std::ostream& out,List<TYPE>)&List){
cout<<"(";
for(Node<type)*ptr=list.head;ptr!=NULL; ptr=ptr->next()){
out<<ptr->get();
if (ptr->next () !Null)
out<<",";
}
out<<")";
return out;
}
puplic:
List(){head = tail=NULL;count=0;}//empty list
~list(){}
Node<TYPE>*first(){return head;}
Node<TYPE>*insertLast(TYPE d){
Node<TYPE>*n=new Node<TYPE>;
N->data=d;//using a copy constructor
if(tail==NULL){//empty list
head=tail=n;
n->NEXT =n->PREV=NULL;
}else{
n->NEXT=NULL;
n->PREV=tail;
tail->NEXT=n;
tail=n;
}
++count;
return n;
}
TYPE removLast() {
TYPE d; //need to set a defult value
if (tail==NULL)
return d;//default return value for empty list
d=tail->data;
if (tail!=head){
tail=tail->PREV;
delet tail->NEXT;
tail->NEXT=NULL;
}else{
delet tail;
tail=head=NULL;
}
--count;
return d;
}
Node<TYPE>*insertFirst(TYPE d){
Node<TYPE>*n=new Node<TYPE>;
n->data=d;
if (head==NULL){//empty List
head=tail=n;
n->NEXT = N->prev=NULL;
}else{
n->PREV=NULL;
n->NEXT =head;
head->PREV=n;
head=n;
}
++count;
return n;
}
TYPE removeFirst(){
TYPE d;//need to set a default value
if (head==NULL)
return d; //default return value for empty list
d=head->data;
if (head!=tail){
head=head->PREV;
delet head ->PREV;
head->PREV=NULL;
}else{
delete head;
head=tail=NULL;
}
--count;
return d;
}
Node<TYPE>*insertAt(int i,TYPE d){
Node<TYPE>*n=NULL;
IF(0<=i && i<=xount){
n=new Node<TYPE>;
n->data=d;
cout<<"cout="<<cout<<endl;
if (i==0)
insertFirst(d);
else if (i==count)
insertLast(d);
else {
Node<TYPLE)*ptr=head;
for(int cnt=0;cnt<i-1;++cnt)
ptr=ptr->NEXT;
n->PREV=ptr;
n->NEXT =ptr->NEXT;
n->PREV->NEXT=n;
n->NEXT->PREV=n;
++count;
}
}
return n;
}
TYPE removeAt(int i){
TYPE d;
return d;
}
Node<TYPE>*Push(TYPE d){
return insertlast();
}
Type pop(){
return removeLast();
}
int size(){
return count==0;
}
bool isEmpty(){
return count==0;
TYPE peek(){
return tail->data;
}
private:
Node<TYPE>*head;
Node<TYPE>*tail;
int count;
};
#endif

I tried many times but I couldn't any one want help ?
mocha's Avatar, Join Date: Oct 2010
Go4Expert Member
Sorry I post a wrong questions...


Given the attached List template class ,implement a set of member functions to be able to preform the following iterations properly.Add what data you need to make these member funcution work.
List 1;
//add items to list
for (1.begin();! 1.endOfList();1.next())
cout<<1.currentData()

for(1.end();!.endOfList();1.prev())
cout<<1.currentData()<

also,finish the removeAt()member function in the implementation of the list.



Code:
//TEMPLET CLASS

#ifndef_LIST_
#define_LIST_

#include<iostream>
template<typename TYPE>
	class Node{
		template<typename T> friend class List;
	public:
		Node() {}
		~ Node() {}
		Node *prev(){return PREV;}
		Node *next(){return NEXT;}
		Node &set(Type d){data=d;return*this;}
		Node get() {return data;}
Privet:
		Node *Next;
		Node *prev;
		TYPE  data;

	};

	template <typename TYPE>
	Class List{
		friend std::ostream &operator<<(std::ostream& out,List<TYPE>)&List){
			cout<<"(";
			for(Node<type)*ptr=list.head;ptr!=NULL; ptr=ptr->next()){
				out<<ptr->get();
				if (ptr->next () !Null)
					out<<",";
			}
			out<<")";
			return out;
		}
puplic:
		List(){head = tail=NULL;count=0;}//empty list
		~list(){}
		Node<TYPE>*first(){return head;}

		Node<TYPE>*insertLast(TYPE d){

		Node<TYPE>*n=new Node<TYPE>;
		N->data=d;//using a copy constructor
		if(tail==NULL){//empty list
			head=tail=n;
			n->NEXT =n->PREV=NULL;
		}else{
			n->NEXT=NULL;
			n->PREV=tail;
			tail->NEXT=n;
			tail=n;
		}
		++count;
		return n;
		}
		TYPE removLast() {
			TYPE d; //need to set a defult value
		if (tail==NULL)
			return d;//default return value for empty list
		d=tail->data;
		if (tail!=head){
			tail=tail->PREV;
			delet tail->NEXT;
			tail->NEXT=NULL;
		}else{
			delet tail;
			tail=head=NULL;
		}
		--count;
		return d;
		}
		Node<TYPE>*insertFirst(TYPE d){
			Node<TYPE>*n=new Node<TYPE>;
			n->data=d;
			if (head==NULL){//empty List
				head=tail=n;
				n->NEXT = N->prev=NULL;
			}else{
				n->PREV=NULL;
				n->NEXT =head;
				head->PREV=n;
				head=n;
			}
			++count;
			return n;
		}
		TYPE removeFirst(){
			TYPE d;//need to set a default value
			if (head==NULL)
				return d; //default return value for empty list
			d=head->data;
			if (head!=tail){
				head=head->PREV;
				delet head ->PREV;
				head->PREV=NULL;
			}else{
				delete head;
				head=tail=NULL;
			}
			--count;
			return d;
		}
		Node<TYPE>*insertAt(int i,TYPE d){
			Node<TYPE>*n=NULL;
			IF(0<=i && i<=xount){
				n=new Node<TYPE>;
				n->data=d;
				cout<<"cout="<<cout<<endl;
				if (i==0)
					insertFirst(d);
				else if (i==count)
					insertLast(d);
				else {
					Node<TYPLE)*ptr=head;
					for(int cnt=0;cnt<i-1;++cnt)
						ptr=ptr->NEXT;
					n->PREV=ptr;
					n->NEXT =ptr->NEXT;
					n->PREV->NEXT=n;
					n->NEXT->PREV=n;
					++count;
				}
			}
			return n;
		}
		TYPE removeAt(int i){
			TYPE d;
			return d;
		}

		Node<TYPE>*Push(TYPE d){
			return insertlast();
		}
		Type pop(){
			return removeLast();
		}
		int size(){
			return count==0;
		}
		bool isEmpty(){
			return count==0;
			TYPE peek(){
				return tail->data;
			}
		private:
			Node<TYPE>*head;
			Node<TYPE>*tail;
			int count;
};
#endif