📄 list.cc
字号:
//// ********************************************************************// *// * Author : Gerald Carter// * cartegw@humsci.auburn.edu// * Filename : list.h// * Date Created : 961220// *// * Description// * -------------// * This is the implementation file for a C++ linked list template// *// * -------------// * Modifications// * -------------// *// *******************************************************************//// HEADER FILES#include <iostream.h>#include "logic.h"#include "list.h"// for Template instantiation#pragma implementation// ####################################################################// ## class list// ##// Constructorstemplate <class T>list<T>::list ( void ){ front = end = 0; node_count = 0;}template <class T>list<T>::list ( const T& init ){ front = new linkedListNode<T, 2>; front -> SetData ( init ); end = front; node_count = 1;}template <class T>list<T>::list ( const list<T>& init_list ){ linkedListNode<T, 2> *temp = init_list.front, *tag = 0; if ( init_list.IsEmpty () ) { front = end = 0; node_count = 0; } else while ( temp ) { Add ( temp -> GetData () ); temp = (linkedListNode<T,2>*) temp -> GetLink ( NEXT ); }}// Destructortemplate <class T>list<T>::~list ( void ){ while ( front ) { end = front; front = (linkedListNode<T,2>*) front -> GetLink ( NEXT ); delete end; } end = 0;}template <class T>list<T>& list<T>::Add ( const T& add_item ){ // local variables linkedListNode<T, 2> *temp = 0; int found = -1; // Check to see if item is already in list. If so then return. found = (*this)[add_item]; if ( found != -1 ) { cerr << "Cannot add duplicate item ( " << add_item << " ) to list.\n"; return ( *this ); } // Add the item to the list. By default it goes at the end. switch ( node_count ) { case 0 : front = new linkedListNode<T, 2>; front -> SetData ( add_item ); end = front; break; default : temp = new linkedListNode<T, 2>; temp -> SetData ( add_item ); temp -> SetLink ( PREVIOUS, end ); end -> SetLink ( NEXT, temp ); end = temp; break; } ++node_count; // successful completion return ( *this );}template <class T>list<T>& list<T>::Add ( const list<T>& add_list ){ // local variables linkedListNode<T, 2> *temp = 0; // Add the new list one item at a time temp = add_list.front; while ( temp ) { Add ( temp -> GetData () ); temp = (linkedListNode<T,2>*) temp -> GetLink ( NEXT ); }}template <class T>list<T>& list<T>::Remove ( const T& rm_item ) { // local variables int found = -1, i = 0; linkedListNode<T,2> *temp = front; // search for the item to be removed. found = (*this)[rm_item]; if ( found == -1 ) cerr << "Item ( " << rm_item << " ) not found in list.\n"; else if ( found == 0 ) { temp = front; front = (linkedListNode<T,2>*) front -> GetLink ( NEXT ); if ( node_count == 1 ) front = end = 0; else front -> SetLink ( PREVIOUS, 0 ); delete temp; --node_count; } else if ( found == (node_count-1) ) { temp = end; end = (linkedListNode<T,2>*) end -> GetLink ( PREVIOUS ); end -> SetLink ( NEXT, 0 ); delete temp; --node_count; } else { while ( i != found ) { temp = (linkedListNode<T,2>*) temp -> GetLink ( NEXT ); ++i; } (temp->GetLink (PREVIOUS)) -> SetLink (NEXT , temp->GetLink (NEXT)); (temp->GetLink (NEXT)) -> SetLink (PREVIOUS , temp->GetLink (PREVIOUS)); delete temp; --node_count; } // successful completion return ( *this );}template <class T>list<T>& list<T>::Sort ( void ){ // local variables list<T> sortedList; linkedListNode<T, 2> *temp, *tag; T min; // find the minimum element in the current list and add it to the // sortedList while ( NOT IsEmpty() ) { min = front -> GetData (); temp = tag = (linkedListNode<T,2>*) front -> GetLink ( NEXT ); while ( temp ) { if ( temp->GetData() < min ) min = temp -> GetData (); temp = (linkedListNode<T,2>*) temp -> GetLink ( NEXT ); } sortedList.Add ( min ); Remove ( min ); } *this = sortedList;}template <class T>list<T>& list<T>::operator= (const list<T>& assign_list){ linkedListNode<T, 2> *temp = assign_list.front; // delete the existing list while ( front ) { end = front; front = (linkedListNode<T,2>*) front -> GetLink ( NEXT ); delete end; } node_count = 0; front = end = 0; // If the list is empty then just initialize1 if ( NOT assign_list.IsEmpty() ) while ( temp ) { Add ( temp -> GetData () ); temp = (linkedListNode<T,2>*) temp -> GetLink ( NEXT ); } return *this;}template <class T>T list<T>::operator[] ( int index ) const{ int i = 0; linkedListNode<T, 2> *point = front; T temp; if ( NOT ( ( 0 <= index ) AND ( index < node_count ) ) ) { cerr << "Index out of range.\n"; return ( temp ); } while (i != index) { point = (linkedListNode<T,2>*) point -> GetLink ( NEXT ); ++i; } return ( point -> GetData () );}template <class T>int list<T>::operator[] (const T& check) { int result = 0; linkedListNode<T, 2> *point = front; while ( point ) { if ( ( point -> GetData () ) == check) break; ++result; point = (linkedListNode<T,2>*) point -> GetLink ( NEXT ); } if ( result == node_count ) return ( -1 ); else return ( result );}template <class T>void list<T>::PrintList ( ostream& S ){ linkedListNode<T, 2> *point = front; while ( point ) { S << point -> GetData () << "\n"; point = (linkedListNode<T,2>*) point -> GetLink ( NEXT ); } S << "Node Count = " << node_count << endl;}template <class T>T list<T>::First (void){ linkedListNode<T, 2> *point = front; T temp; switch ( node_count ) { case 0 : cerr << "Null list. Unable to get first node.\n"; return ( temp ); break; case 1 : point = front; front = 0; break; default : point = front; front = (linkedListNode<T,2>*) front -> GetLink ( NEXT ); front -> SetLink ( PREVIOUS, 0 ); break; } temp = point -> GetData (); delete point; --node_count; return ( temp );}template <class T>T list<T>::Last (void){ linkedListNode<T,2> *point = front; T temp; switch ( node_count ) { case 0 : cerr << "Null list. Unable to get first\n"; return ( temp ); case 1 : point = front; front = end = 0; break; default : point = end; end = (linkedListNode<T,2>*) end -> GetLink ( PREVIOUS ); end -> SetLink ( NEXT, 0 ); break; } temp = point -> GetData (); delete point; --node_count; return ( temp );}// Typedefs for template instantiation#include "param.h"typedef list<paramEntry> dummyParamList;//**************** end of list.cc ***********************************//*******************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -