1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Overload the operato

Discussion in 'C' started by mocha, Oct 26, 2010.

  1. mocha

    mocha New Member

    [FONT=&quot]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.[/FONT]
    [FONT=&quot] [/FONT]
    Code:
    [LEFT]//TEMPLET CLASS[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]#ifndef_LIST_[/LEFT]
      [LEFT]#define_LIST_[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]#include<iostream>[/LEFT]
      [LEFT]template<typename TYPE>[/LEFT]
      [LEFT]          class Node{[/LEFT]
      [LEFT]                   template<typename T> friend class List;[/LEFT]
      [LEFT]          public:[/LEFT]
      [LEFT]                   Node() {}[/LEFT]
      [LEFT]                   ~ Node() {}[/LEFT]
      [LEFT]                   Node *prev(){return PREV;}[/LEFT]
      [LEFT]                   Node *next(){return NEXT;}[/LEFT]
      [LEFT]                   Node &set(Type d){data=d;return*this;}[/LEFT]
      [LEFT]                   Node get() {return data;}[/LEFT]
      [LEFT]Privet:[/LEFT]
      [LEFT]                   Node *Next;[/LEFT]
      [LEFT]                   Node *prev;[/LEFT]
      [LEFT]                   TYPE  data;[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]          };[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]          template <typename TYPE>[/LEFT]
      [LEFT]          Class List{[/LEFT]
      [LEFT]                   friend std::ostream &operator<<(std::ostream& out,List<TYPE>)&List){[/LEFT]
      [LEFT]                             cout<<"(";[/LEFT]
      [LEFT]                             for(Node<type)*ptr=list.head;ptr!=NULL; ptr=ptr->next()){[/LEFT]
      [LEFT]                                      out<<ptr->get();[/LEFT]
      [LEFT]                                      if (ptr->next () !Null)[/LEFT]
      [LEFT]                                                out<<",";[/LEFT]
      [LEFT]                             }[/LEFT]
      [LEFT]                             out<<")";[/LEFT]
      [LEFT]                             return out;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]puplic:[/LEFT]
      [LEFT]                   List(){head = tail=NULL;count=0;}//empty list[/LEFT]
      [LEFT]                   ~list(){}[/LEFT]
      [LEFT]                   Node<TYPE>*first(){return head;}[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]                   Node<TYPE>*insertLast(TYPE d){[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]                   Node<TYPE>*n=new Node<TYPE>;[/LEFT]
      [LEFT]                   N->data=d;//using a copy constructor[/LEFT]
      [LEFT]                   if(tail==NULL){//empty list[/LEFT]
      [LEFT]                             head=tail=n;[/LEFT]
      [LEFT]                             n->NEXT =n->PREV=NULL;[/LEFT]
      [LEFT]                   }else{[/LEFT]
      [LEFT]                             n->NEXT=NULL;[/LEFT]
      [LEFT]                             n->PREV=tail;[/LEFT]
      [LEFT]                             tail->NEXT=n;[/LEFT]
      [LEFT]                             tail=n;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   ++count;[/LEFT]
      [LEFT]                   return n;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   TYPE removLast() {[/LEFT]
      [LEFT]                             TYPE d; //need to set a defult value[/LEFT]
      [LEFT]                   if (tail==NULL)[/LEFT]
      [LEFT]                             return d;//default return value for empty list[/LEFT]
      [LEFT]                   d=tail->data;[/LEFT]
      [LEFT]                   if (tail!=head){[/LEFT]
      [LEFT]                             tail=tail->PREV;[/LEFT]
      [LEFT]                             delet tail->NEXT;[/LEFT]
      [LEFT]                             tail->NEXT=NULL;[/LEFT]
      [LEFT]                   }else{[/LEFT]
      [LEFT]                             delet tail;[/LEFT]
      [LEFT]                             tail=head=NULL;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   --count;[/LEFT]
      [LEFT]                   return d;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   Node<TYPE>*insertFirst(TYPE d){[/LEFT]
      [LEFT]                             Node<TYPE>*n=new Node<TYPE>;[/LEFT]
      [LEFT]                             n->data=d;[/LEFT]
      [LEFT]                             if (head==NULL){//empty List[/LEFT]
      [LEFT]                                      head=tail=n;[/LEFT]
      [LEFT]                                      n->NEXT = N->prev=NULL;[/LEFT]
      [LEFT]                             }else{[/LEFT]
      [LEFT]                                      n->PREV=NULL;[/LEFT]
      [LEFT]                                      n->NEXT =head;[/LEFT]
      [LEFT]                                      head->PREV=n;[/LEFT]
      [LEFT]                                      head=n;[/LEFT]
      [LEFT]                             }[/LEFT]
      [LEFT]                             ++count;[/LEFT]
      [LEFT]                             return n;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   TYPE removeFirst(){[/LEFT]
      [LEFT]                             TYPE d;//need to set a default value[/LEFT]
      [LEFT]                             if (head==NULL)[/LEFT]
      [LEFT]                                      return d; //default return value for empty list[/LEFT]
      [LEFT]                             d=head->data;[/LEFT]
      [LEFT]                             if (head!=tail){[/LEFT]
      [LEFT]                                      head=head->PREV;[/LEFT]
      [LEFT]                                      delet head ->PREV;[/LEFT]
      [LEFT]                                      head->PREV=NULL;[/LEFT]
      [LEFT]                             }else{[/LEFT]
      [LEFT]                                      delete head;[/LEFT]
      [LEFT]                                      head=tail=NULL;[/LEFT]
      [LEFT]                             }[/LEFT]
      [LEFT]                             --count;[/LEFT]
      [LEFT]                             return d;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   Node<TYPE>*insertAt(int i,TYPE d){[/LEFT]
      [LEFT]                             Node<TYPE>*n=NULL;[/LEFT]
      [LEFT]                             IF(0<=i && i<=xount){[/LEFT]
      [LEFT]                                      n=new Node<TYPE>;[/LEFT]
      [LEFT]                                      n->data=d;[/LEFT]
      [LEFT]                                      cout<<"cout="<<cout<<endl;[/LEFT]
      [LEFT]                                      if (i==0)[/LEFT]
      [LEFT]                                                insertFirst(d);[/LEFT]
      [LEFT]                                      else if (i==count)[/LEFT]
      [LEFT]                                                insertLast(d);[/LEFT]
      [LEFT]                                      else {[/LEFT]
      [LEFT]                                                Node<TYPLE)*ptr=head;[/LEFT]
      [LEFT]                                                for(int cnt=0;cnt<i-1;++cnt)[/LEFT]
      [LEFT]                                                          ptr=ptr->NEXT;[/LEFT]
      [LEFT]                                                n->PREV=ptr;[/LEFT]
      [LEFT]                                                n->NEXT =ptr->NEXT;[/LEFT]
      [LEFT]                                                n->PREV->NEXT=n;[/LEFT]
      [LEFT]                                                n->NEXT->PREV=n;[/LEFT]
      [LEFT]                                                ++count;[/LEFT]
      [LEFT]                                      }[/LEFT]
      [LEFT]                             }[/LEFT]
      [LEFT]                             return n;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   TYPE removeAt(int i){[/LEFT]
      [LEFT]                             TYPE d;[/LEFT]
      [LEFT]                             return d;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT] [/LEFT]
      [LEFT]                   Node<TYPE>*Push(TYPE d){[/LEFT]
      [LEFT]                             return insertlast();[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   Type pop(){[/LEFT]
      [LEFT]                             return removeLast();[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   int size(){[/LEFT]
      [LEFT]                             return count==0;[/LEFT]
      [LEFT]                   }[/LEFT]
      [LEFT]                   bool isEmpty(){[/LEFT]
      [LEFT]                             return count==0;[/LEFT]
      [LEFT]                             TYPE peek(){[/LEFT]
      [LEFT]                                      return tail->data;[/LEFT]
      [LEFT]                             }[/LEFT]
      [LEFT]                   private:[/LEFT]
      [LEFT]                             Node<TYPE>*head;[/LEFT]
      [LEFT]                             Node<TYPE>*tail;[/LEFT]
      [LEFT]                             int count;[/LEFT]
      [LEFT]};[/LEFT]
      [LEFT]#endif[/LEFT]
    

    I tried many times but I couldn't any one want help ?:cryin:[FONT=&quot][/FONT]
     
  2. mocha

    mocha New 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
    
     

Share This Page