Esempio n. 1
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;
	}
}
Esempio n. 2
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();
         }

    }
}
	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());
		}
	}
Esempio n. 4
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;
	}
}
    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;
    }
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;
}
Esempio n. 7
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;
    }
}
Esempio n. 8
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;
}
Esempio n. 9
0
void List::print(){

	Node* tmp = head;

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

	cout<<"NULL\n";
}
	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();
			}
		}
	}
Esempio n. 11
0
void List::Print(){
	if(head == NULL)
		cout << "List is empty!" << endl;
	else{
		while(head != NULL){
			cout << head->Data() << "\t";
			head = head->Next();
		}
	}
	return;
}
Esempio n. 12
0
	void Print()
	{
		Node *p;
		p=_head;
		while(p!=NULL)
		{
			cout<<p->Data()<<"\t";
			p->SetNext(p);
		}
		delete p;
		p=NULL;
	}
Esempio n. 13
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 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());
			}
		}
	}
	void removeDuplicate2(List list) {
		Node *pre = list.Head();
		if (pre == NULL || pre->Next() == NULL) {
			return;
		}

		while (pre != NULL) {
			Node *cur = pre;
			while (cur->Next() != NULL) {
				if (cur->Next()->Data() == pre->Data()) {
					cur->SetNext(cur->Next()->Next());
				} else {
					cur = cur->Next();
				}
			}
			pre = pre->Next();
		}
	}
    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();
                }
            }
        }
    }