Example #1
0
void linkedlist::add_tail(int data) 
{

    Node* new_node = new Node();  // newnode creation
    
    new_node->set_data(data);  // data set
    new_node->set_next(NULL);  // set to null  node->next = NULL


    Node *temp = head;

    if ( temp != NULL ) {

    while ( temp->Next() != NULL ) {
        temp = temp->Next();
    }


    temp->set_next(new_node);
    }
    else {
    
    head = new_node;
    }
}
Example #2
0
void linkedlist::delete_node(int data) {


    Node *temp = head;


    if ( !temp  )
    return;


    if ( temp->Next() == NULL ) {
    delete temp;
    head = NULL;
    }
    else {

    Node *previous;
    
    while(temp != NULL)
    {
        if ( temp->Data() == data ) break;
        previous = temp;
        temp = temp->Next();
    } 


    previous->set_next(temp->Next());


    delete temp;
    }
}
	void partition_list(List &list, int x) {
		if (list.Head() == NULL) { return; }

		List small;
		List large;

		Node *pre = list.Head();

		while (pre != NULL) {
			if (pre->Data() <= x) {
				small.Append(pre->Data());
			} else {
				large.Append(pre->Data());
			}
			pre = pre->Next();
		}

		if (small.Head() != NULL) {
			pre = small.Head();
			while (pre->Next() != NULL) {
				pre = pre->Next();
			}
			pre->SetNext(large.Head());

			list.SetHead(small.Head());
		} else {
			list.SetHead(large.Head());
		}
	}
Example #4
0
void linkedlist::print_data() 
{

    Node *temp = head;

    // No nodes
    if ( temp == NULL ) {
    cout << "empty list";;
    return;
    }


    if ( temp->Next() == NULL ) {
    cout << temp->Data() << "->" << "NULL\n";

    }
    else 
    {
         while(temp != NULL)
         {
          cout << temp->Data() << "->";
          temp = temp->Next();
         }

    }
}
Example #5
0
void List::Delete(int data){
//	//List is empty
//	if(head == NULL){
//		cout << "Cannot delete";
//	}
//	
//	//List has one element
//	else if(head->Data() == data){
//		head = NULL;
//		return;
//	}
//	
//	else{
//		//Traverse the list to find it
//		Node* prev;
//		Node* curr = head;
//		while(curr->Data() != data){
//			prev = curr;
//			curr = curr->Next();		
//		}
//		prev->SetNext(curr->Next());
//		return;
//	}
	
			//Traverse the list to find it
		Node* prev;
		Node* curr = head;
		while(curr->Data() != data){
			prev = curr;
			curr = curr->Next();		
		}
		prev->SetNext(curr->Next());
		return;
}
Example #6
0
/**
* Print the contents of the list
*/
void List::Print( ) {

	// Temp pointer
	Node *tmp = head;

	// No nodes
	if ( tmp == NULL ) {
		cout << "EMPTY" << endl;
		return;
	}

	// One node in the list
	if ( tmp->Next( ) == NULL ) {
		cout << tmp->Data( );
		cout << " --> ";
		cout << "NULL" << endl;
	}
	else {
		// Parse and print the list
		do {
			cout << tmp->Data( );
			cout << " --> ";
			tmp = tmp->Next( );
		} while ( tmp != NULL );

		cout << "NULL" << endl;
	}
}
Example #7
0
/**
* Append a node to the linked list
*/
void List::Append( int data ) {

	// Create a new node
	Node* newNode = new Node( );
	newNode->SetData( data );
	newNode->SetNext( NULL );

	// Create a temp pointer
	Node *tmp = head;

	if ( tmp != NULL ) {
		// Nodes already present in the list
		// Parse to end of list
		while ( tmp->Next( ) != NULL ) {
			tmp = tmp->Next( );
		}

		// Point the last node to the new node
		tmp->SetNext( newNode );
	}
	else {
		// First node in the list
		head = newNode;
	}
}
Example #8
0
/**
* Delete a node from the list
*/
void List::Delete( int data ) {

	// Create a temp pointer
	Node *tmp = head;

	// No nodes
	if ( tmp == NULL )
		return;

	// Last node of the list
	if ( tmp->Next( ) == NULL ) {
		delete tmp;
		head = NULL;
	}
	else {
		// Parse thru the nodes
		Node *prev = tmp;
		do {
			if ( tmp->Data( ) == data ) break;
			prev = tmp;
			tmp = tmp->Next( );
		} while ( tmp != NULL );

		// Adjust the pointers
		prev->SetNext( tmp->Next( ) );

		// Delete the current node
		delete tmp;
	}
}
int main()
{
    List list;
    Solution res;

    list.Init(10, 10);

    cout << "before: ";
    list.toString();

    Node *end = list.Head();
    Node *loop;
    int i = 0;
    while (end->Next() != NULL) {
        end = end->Next();
        if (i++ == 7) {
            loop = end;
        }
    }
    end->SetNext(loop);

    cout << "loop start at: " << loop->Data() << endl;

    res.circular_list(list);
    res.circular_list2(list);

    return 0;
}
Example #10
0
nat32 Node::Write(io::OutVirt<io::Binary> & out) const
{
 nat32 ret = 0;
  // Header...
   nat32 bs = WriteSize();
   nat32 os = TotalWriteSize();
   nat8 zb = 0;

   ret += out.Write("HON",3);
   ret += out.Write(MagicString(),3);
   ret += out.Write(&bs,4);
   ret += out.Write(&zb,1);
   ret += out.Write(&os,4);
   ret += out.Write(&zb,1);
;
  // Child count...
   nat32 childCount = ChildCount();
   ret += out.Write(&childCount,4);

  // Children...
   if (child)
   {
    Node * targ = child;
    do
    {
     ret += targ->Write(out);
     targ = targ->Next();
    } while (!targ->First());
   }   

 if (ret!=bs) out.SetError(true);
 return ret;
}
// @brief  : PMDファイルの取り出し
// @param  : 親クラス
//         : 描画するのか?
//         : ファイル名
//--------------------------------------------------------------------
_3d::PMDNoBone *PMDNoBoneManagerDx9::Load( IDraw *_parent, bool _is_draw, const std::string &_file_name )
{
    assert(!_file_name.empty());

    // ファイルの検索
    PMDDataDx9 *data = nullptr;
    Node *i = Begin();
    while(i)
    {
        data = dynamic_cast<PMDDataDx9 *>(i);
        std::string name = data->m_FilePass;
        if( name.compare(_file_name) == 0 ) break;
        i = i->Next();
        data = nullptr;
    }

    // ファイルの作成
    if(!data)
    {
        data = new PMDDataDx9(_file_name);
    }

    // モデルの作成
    PMDNoBoneDx9 *re = new PMDNoBoneDx9(data,_parent,_is_draw);
    PMDMeshDx9 *mesh = new PMDMeshDx9(data,re,true);
    for(unsigned i = 0 ; i < data->m_MaxMaterial ; ++i)
    {
        new PMDMaterialDx9(&data->m_Material[i],data,mesh,true);
    }

    // 作成
    return re;
}
    void circular_list(List list) {
        if (list.Head() == NULL) {
            return;
        }

        Node *fast = list.Head();
        Node *slow = list.Head();

        slow = slow->Next();
        fast = fast->Next()->Next();

        while (fast != slow) {
            if (fast == NULL || fast->Next() == NULL || slow == NULL) {
                return;
            }
            slow = slow->Next();
            fast = fast->Next()->Next();
        }

        fast = list.Head();

        while (fast != slow) {
            fast = fast->Next();
            slow = slow->Next();
        }

        cout << fast->Data() << endl;
    }
	/*inserting a value infront of the */
	void LinkedList::insert_back(int data){
		//Creating a node
		Node *newNode = new Node();
		newNode->setData(data);
		newNode->setNext(NULL);

		//Creating a temporary pointer.
		Node *tmp = head;

		if (tmp != NULL){
			while(tmp->Next() != NULL){
				tmp = tmp->Next();
			}
			tmp->setNext(newNode);
		}else{
			head = newNode;
		}
	}
Example #14
0
void List::deleteFromListEnd(int n){
	Node* tmp = head;
	Node* tmp2= head;

	for(int i=0;tmp->Next()!=NULL;tmp = tmp->Next(),i++){

		if(i>=n)
			tmp2=tmp2->Next();
	}
	tmp = tmp2->Next();
	tmp2->setNext(NULL);

	while(tmp->Next()!=NULL){
		Node* tmp1 = tmp;
		tmp= tmp->Next();
		delete tmp1;
	}
}
Example #15
0
void List::print(){

	Node* tmp = head;

	do	{
		cout<<tmp->Data()<<"->";
		tmp = tmp->Next();
	}while (tmp!=NULL);

	cout<<"NULL\n";
}
Example #16
0
void List::Print(){
	if(head == NULL)
		cout << "List is empty!" << endl;
	else{
		while(head != NULL){
			cout << head->Data() << "\t";
			head = head->Next();
		}
	}
	return;
}
	void LinkedList::print_List(){
		Node *tmp = head;

		//Checking the list if there is a node or not.
		if(tmp == NULL){
			cout << "List is empty\n";
			return;
		}

		//Checking only one node situation.
		if(tmp->Next() == NULL){
			cout << "Starting: " <<tmp->Data()
				 <<"Next Value > NULL\n";
		}else{
			while(tmp!=NULL){
				cout << tmp->Data() << " > ";
				tmp = tmp->Next();
			}
		}
	}
Example #18
0
void List::deleteFromListBegin(int n){

	Node* tmp = head;
	
	for (int i=0;i<n;i++){
		Node* tmp2 = tmp;
		tmp= tmp->Next();
		delete tmp2;
	}

	head = tmp;
}
Example #19
0
 // Print this crap
 void NodeList::Print(){
   if(head == NULL){
     cout << "Cannot give you what I don't have, add something to print first... (Empty List)\n";
   }
   else{
     cout << "doing printing and stuff\n";
     Node* elem = head;
     while(elem != nullptr){
       cout <<" " << elem->getContent() << ",";
       elem = elem->Next();
     }
   }
 };
Example #20
0
nat32 Node::ChildCount() const
{
 nat32 ret = 0;
  if (child)
  {
   Node * targ = child;
   do
   {
    ret += 1;
    targ = targ->Next();
   } while (!targ->First());
  } 
 return ret;
}
Example #21
0
void List::Delete_End(){
  	if(head == NULL){
  	 	cout<<"List has no member so cannot delete end"<<endl;
        return;	 	
	}
	   
	// check if one in length 
	if(head->Next() == NULL){
       	head = NULL;
       	return;
	}
	// 2 or greater in length

    Node *current;
    Node *prev;
    prev = head;

	for(current = head->Next(); current->Next() != NULL; current = current->Next())	 {
       	prev = current;
	}
	prev->SetNext(NULL);
    return;
}  
Example #22
0
nat32 Node::WriteSize() const
{
 nat32 ret = 16 + 4;
 if (child)
 {
  Node * targ = child;
  do
  {
   ret += targ->TotalWriteSize();
   targ = targ->Next();
  } while (!targ->First());
 }
 return ret;
}
Example #23
0
nat32 Node::TotalMemory() const
{
 nat32 ret = Memory();
 if (child)
 {
  Node * targ = child;
  do
  {
   ret += targ->TotalMemory();
   targ = targ->Next();
  } while (!targ->First());
 }
 return ret;
}
    void circular_list2(List list) {
        if (list.Head() == NULL) {
            return;
        }

        set<Node *> set;

        Node *cur = list.Head();

        while (1) {
            if (set.find(cur) != set.end()) {
                cout << cur->Data() << endl;
                return ;
            } else {
                set.insert(cur);
                if (cur->Next() == NULL) {
                    return;
                } else {
                    cur = cur->Next();
                }
            }
        }
    }
Example #25
0
Node<T> * List<T>::PushFront(const T &data) {
	Node<T> *newNode;
	if (first != nullptr) {								//Add node to list
		newNode = new Node<T>(data, first, nullptr );
		first = newNode;
		newNode->Next()->prev = newNode;
	}
	else {												//First Node in list
		newNode = new Node<T>(data);
		first = newNode;
		last = newNode;
	}
	return newNode;
}
Example #26
0
Node* List::Find(int data){
	Node *current;
    	
    for(current = head; current!= NULL && current->Data() != data; current = current->Next()){
		if(current == NULL){
			cout<<"Did not find "<<data<<"."<<endl;
           	return  NULL;
		}
		else // found
		{
			cout<<"Found "<<data<<"."<<endl;
			return current;
		}
	}
}
	void print_list()
	{
		if ( head == NULL )
		{
			cout << "Empty list." << endl;
		}
		else
		{
			Node *temp = head;
			while ( temp )
			{
				cout << temp->get_data() << " ";
				temp = temp->Next();
			}
		}
	}
Example #28
0
// @brief  : ファイルの読み込み
// @param  : ファイル名
//--------------------------------------------------------------------
Texture TextureManagerDx9::Load( const std::string &_name )
{
    if(_name.empty()) return TempSmartPtr<ITexture>(dynamic_cast<ITexture *>(Begin()));

    // ファイルの検索
    Node *i = Begin();
    while(i)
    {
        ITexture *tex = (ITexture *)i;
        std::string name = tex->Name;
        if( name.compare(_name) == 0 ) return TempSmartPtr<ITexture>(tex);
        i = i->Next();
    }

    // 作成
    return TempSmartPtr<ITexture>(new DxTexture9(_name,this));
}
Example #29
0
T List<T>::PopFront() {
	Node<T> *tmp;
	data = first->data;								//get data
	if (first != last) {
		first->next->prev = nullptr;
		tmp = first;
		first = tmp->Next();						//Next node is now first Node
		delete(tmp);
	}
	else {
		tmp = first;
		first = nullptr;							// No more nodes
		last = nullptr;
		delete(tmp);								//Delete node
	}
	return data;
}
	void removeDuplicate(List list) {
		Node *pre = list.Head();
		if (pre == NULL || pre->Next() == NULL) {
			return;
		}

		set<int> table;
		table.insert(pre->Data());

		while(pre->Next() != NULL) {
			if (table.find(pre->Next()->Data()) == table.end()) {
				table.insert(pre->Next()->Data());
				pre = pre->Next();
			} else {
				pre->SetNext(pre->Next()->Next());
			}
		}
	}