SinglyLinkedList SinglyLinkedList::mergeTwoSortedLinkedList(SinglyLinkedList a,SinglyLinkedList b){
	SinglyLinkedList c;
	SingleNode *head1=a.head;
	SingleNode *head2=b.head;
	SingleNode *head3=c.head;
	while(head1 && head2){
		if(head1->data <= head2->data){
			c.insertAtEnd(head1->data);
			head1=head1->next;
		}
		else{
			c.insertAtEnd(head2->data);
			head2=head2->next;
		}
	}
	if(head1){
		while(head1){
			c.insertAtEnd(head1->data);
			head1=head1->next;
		}
	}

	if(head2){
			while(head2){
				c.insertAtEnd(head2->data);
				head2=head2->next;
			}
		}
	return c;
}
int main(void) {

	SinglyLinkedList<int> *list = new SinglyLinkedList<int>();

	for(unsigned int i = 0; i < 5; i++) {
		list->push(i);	
	}

	list->append(-1);
	list->append(-2);


	list->printList();

	list->deleteNode(-1);

	list->printList();

	list->deleteNode(4);

	list->printList();

	list->deleteNode(-2);

	list->printList();

	delete list;
}
示例#3
0
文件: main.cpp 项目: wakensky/garage
int main (int argc, char **argv) {
	SinglyLinkedList<size_t> list;
	SinglyLinkedList<size_t>::Node *latest_added_node = list.insert_after(list.head(), 0);
	for (size_t i = 1; i < 10000 * 1000; ++i) {
		latest_added_node = list.insert_after(latest_added_node, i);
	}
	return 0;
}
    /* Finding the element by value */
    SinglyLinkedList* find(int value)
    {
    	SinglyLinkedList *currentElement = this;
    	while(currentElement)
    	{
    		if(currentElement->getValue() == value)
    			break;

    		currentElement = currentElement->getNext();
    	}
    	return currentElement;
    }
示例#5
0
		/**
		* @brief Clears all lists and the stack.
		*
		* Suitable for when a buffer has its role switched back to front.
		*/
		void clearAll() {
			componentSpecsList.clear();
			componentFunctionsList.clear();
			entitySpecsList.clear();
			entityFunctionsList.clear();
			systemSpecsList.clear();
			systemFunctionsList.clear();
			events.clear();
			newSystems.clear();
			newEntities.clear();
			entitiesToDestroy.clear();

			stack.clear();
		}
int main(void) { 
  SinglyLinkedList<int> mytestlist;
  mytestlist.AddToFront(3);
  mytestlist.AddToFront(4);
  string str = "4 3 ";
  
  if(mytestlist.ToString().compare(str) != 0 ) 
     return 1;

  if(mytestlist.IsEmpty() == true) 
    return 1;

  if(mytestlist.Size() != 2) 
    return 1;

  return 0;
}
int main(int argc, const char * argv[]) {
    Node *head;
    SinglyLinkedList *s = new SinglyLinkedList();
    int size = 15;
    //create
    for (int i=0; i<size; i++) {
        s->add(head, i+1);
    }
    
    //Print Nodes' value
    s->printList(head);
    
    //Print middle node index
    Node* mid = s->rFindMid(head, head);
    cout<<"Middle node is:"<<mid->val<<endl;
    
    return 0;
}
示例#8
0
//*************************************************************************************
void SinglyLinkedList::mergeListsAtRandomPoint(SinglyLinkedList& listOne)
{
    if(m_debug == true)
    {
        cout<<"In function SinglyLinkedList"<<endl;
    }
    // if one list is empty then no need to merge with other
    if(listOne.isListEmpty() || this->isListEmpty())
    {
        return;
    }
    // if both the list is empty then no need to perform any operation.
    if(listOne.isListEmpty() && this->isListEmpty())
    {
        return;
    }
    Node* tempNode;
    Node* tempNodeTwo;
    int rendomLength;
    if(listOne.m_length > this->m_length)
    {
        tempNode = this->m_head;
        tempNodeTwo = listOne.m_head;
        rendomLength = listOne.m_length;
    }
    else
    {
        tempNode = listOne.m_head;
        tempNodeTwo = this->m_head;
        rendomLength = this->m_length;
    }
    while(tempNode->link != NULL)
    {
        tempNode = tempNode->link;
    }
    for(int index = 1; index < (rendomLength - 2); index++)
    {
        tempNodeTwo = tempNodeTwo->link;
    }
    tempNode->link = tempNodeTwo;
}
bool SinglyLinkedList::checkIfListPalindrome(){
	SinglyLinkedList reversed;
	SingleNode *tmp = head;
	while(tmp != NULL){
		reversed.insertAtEnd(tmp -> data);
		tmp = tmp -> next;
	}
	reversed.printSinglyLinkedList();
	reversed.reverseSinglyLinkedList();
	reversed.printSinglyLinkedList();
	SingleNode *tmp1 = head;
	SingleNode *tmp2 = reversed.head;
	while(tmp1!=NULL){
		if(tmp1->data != tmp2->data)
			return false;
		else{
			tmp1 = tmp1 -> next;
			tmp2 = tmp2 ->next;
		}
	}
	return true;
}
示例#10
0
int main()
{	
	cout << "Testing class SinglyLinkedList" <<endl;
	SinglyLinkedList *list1 = new SinglyLinkedList(10);
	cout << "Data in the list1 is " << list1->getValue() <<endl;
	
	SinglyLinkedList *list2 = new SinglyLinkedList(30);
	cout << "Data in the list2 is " << list2->getValue() <<endl;

	SinglyLinkedList *list3 = new SinglyLinkedList(50);
	cout << "Data in the list3 is " << list3->getValue() <<endl;

	cout << "Making chain of list" <<endl;

	list1->setNext(list2);
	list2->setNext(list3);

	cout << "value list 1:  " << list1->getValue() <<endl ;
	cout << "value list 2:  " << list1->getNext()->getValue() <<endl ;
	cout << "value list 3:  " << list1->getNext()->getNext()->getValue() <<endl;
	cout << "==============" << endl;

	cout <<"Adding value in front of the list" <<endl;
	SinglyLinkedList::insertInFront(&list1 , 55);


	cout << "value list 1:  " << list1->getValue() <<endl ;
	cout << "value list 2:  " << list1->getNext()->getValue() <<endl ;
	cout << "value list 3:  " << list1->getNext()->getNext()->getValue() <<endl;
	cout << "value list 4:  " << list1->getNext()->getNext()->getNext()->getValue() <<endl;
	cout << "==============" << endl;

	SinglyLinkedList *foundElement = list1->find(50);
	cout << "found element , value is: "<<foundElement->getValue() <<endl;

}
示例#11
0
int main()
{
    //test
    TestList testList;
    QTest::qExec(&testList);
    //end test
    cout << "Wait a second";
    sleep(1);//for debug(sometimes occures mistake of debugger without sleep();)
    cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b";

    cout << "Singly linked list:\n";
    SinglyLinkedList list;
    list.addValue(2);
    list.addPos(5, 4);
    list.addPos(5, 3);
    list.addPos(1, 99);
    list.deletePos(1);
    list.print();

    cout << "\nDoubly linked list:\n";
    DoublyLinkedList list2;
    list2.addValue(2);
    list2.addPos(5, 4);
    list2.addPos(5, 3);
    list2.addPos(1, 99);
    list2.print();
    list2.deletePos(1);
    list2.print();

    cout << "\nSingly linked list heap try:\n";
    SinglyLinkedList* listH1 = new SinglyLinkedList();
    listH1->addValue(3);
    listH1->addValue(5);
    listH1->addValue(7);
    listH1->addPos(1, 2);
    listH1->print();
    delete listH1;

    cout << "\nDoubly linked list heap try:\n";
    DoublyLinkedList* listH2 = new DoublyLinkedList();
    listH2->addValue(3);
    listH2->addValue(5);
    listH2->addValue(7);
    listH2->addPos(1, 2);
    listH2->print();
    delete listH2;

    return 0;
}
示例#12
0
int main(int argc, char *argv[])
{
	cout << "[+] singly linked list test program" << endl;
	SinglyLinkedList<int> slist;

	if(slist.empty())
	{
		cout << "[+] empty test pass (1/2)" << endl;
	}
	slist.addFront(1);
	if(!slist.empty())
	{
		cout << "[+] empty test pass (2/2)" << endl;
		cout << "[+] addFront test pass (1/3)" << endl;

		if(slist.front() == 1)
		{
			cout << "[+] front test pass (1/2) : " << slist.front() << endl;
			slist.addFront(2);

			if(slist.front() == 2)
			{
				cout << "[+] addFront test pass (2/2)" << endl;
				cout << "[+] front test pass (2/3) : " << slist.front() << endl;

				slist.removeFront();
				if(slist.front() == 1)
				{
					cout << "[+] removeFront test pass (1/1)" << endl;
					cout << "[+] front test pass (3/3) : " << slist.front() << endl;

					slist.clear();
					if(slist.empty())
					{
						cout << "[+] clear test pass (1/1)" << endl;
						cout << "[+] success!" << endl;
						return EXIT_SUCCESS;
					}
				}
			}
		}
	}
	cout << "[-] something wrong" << endl;
	
	return EXIT_FAILURE;
}
int main()
{
	SinglyLinkedList list;
	
	list.insertOrdered("Mike", 1105);
	list.insertOrdered("Rob",  750);
	list.insertOrdered("Paul", 720);
	list.insertOrdered("Anna", 660);
	list.insertOrdered("Rose", 590);
	list.insertOrdered("Jack", 510);
	list.insertOrdered("Jill", 740);

	cout << "List after insertions :" << endl;
	list.print();

	list.removeOrdered("Adam", 610);	// Bu eleman listede yok !

	list.removeOrdered("Jack", 510);
	list.removeOrdered("Mike", 1105);
	list.removeOrdered("Paul", 720);

	cout << "\nList after removals (Jack, Mike, Paul) :" << endl;
	list.print();

	list.removeOrdered("Rose", 590);
	list.removeOrdered("Rob",  750);	
	list.removeOrdered("Anna", 660);
	list.removeOrdered("Jill", 740);

	cout << "\nList after removals (Rose, Rob, Anna, Jill ) :" << endl;
	list.print();
    
    return 0;
	//::getchar();
}
int main() 
{
    // Grab current cout flags, used for when the stream is altered and the user wants
    //   to reset it to the default values: 
    ios::fmtflags f( cout.flags() );

    // Test 1:
    {
      SinglyLinkedList<char>* list = new SinglyLinkedList<char>();

      char var = 'A';

      for (int i = 1; i <= 10; i++) 
        {
        list->pushBack(var);
        var++;
      }

      cout << endl << "Test 1 - Char list:" << endl << "\t";
      list->print();
        cout << endl;
    }

    // Reset cout stream flags:
    cout.flags(f);

    // Test 2:
    {
        DoublyLinkedList<myType>* list = new DoublyLinkedList<myType>();

        myType var = 1;

        for (int i = 1; i <= 10; i++) 
        {
            list->pushBack(var);
            var++;
        }

        cout << endl << "Test 2 - Int list:" << endl << "\t";
        list->print();
        cout << endl;
    }

    // Reset cout stream flags:
    cout.flags(f);

    // Test 3:
    {
        BinaryTree<int>* bt = new BinaryTree<int>();

        // Insert a bunch of stuff:
        bt->insert(11);
        bt->insert(6);
        bt->insert(8);
        bt->insert(19);
        bt->insert(4);
        bt->insert(10);
        bt->insert(5);
        bt->insert(17);
        bt->insert(43);
        bt->insert(49);
        bt->insert(31);

        // Print tree preOrderTraversal:
        cout << endl << "Binary Tree PreOrderTravesal:" << endl << "\t";
        bt->preOrderTraversal();
        cout << endl;

        // Print tree inOrderTraversal:
        cout << endl << "Binary Tree InOrderTravesal:" << endl << "\t";
        bt->inOrderTraversal();
        cout << endl;

        // Print tree postOrderTraversal:
        cout << endl << "Binary Tree PostOrderTravesal:" << endl << "\t";
        bt->postOrderTraversal();
        cout << endl;
    }

    cout << endl;

    return 0;
}
void TestDataStructures::SinglyLinkedListOperations(){
    SinglyLinkedList* list = new SinglyLinkedList();
    
    list->InsertInMiddle(9);
    list->InsertInMiddle(3);
    list->InsertAtEnd(5);
    list->InsertInMiddle(8);
    list->InsertInMiddle(7);
    list->InsertInMiddle(4);
    std::cout << "\nThe List.\n";
    list->DisplayList();
    std::cout << "\n";
    
    
    Node* deletedNode = list->DeleteNode(8);
    std::cout << "Deleted Node: ";
    std::cout << deletedNode->GetData();
    std::cout << "\n";
    std::cout << "\n";
    list->DisplayList();
    std::cout << "\n";
     
    deletedNode = list->DeleteFromBegin();
    std::cout << "Deleted Node: ";
    std::cout << deletedNode->GetData();
    std::cout << "\n";
    std::cout << "\n";
    list->DisplayList();
    std::cout << "\n";
     
    deletedNode = list->DeleteFromEnd();
    std::cout << "Deleted Node: ";
    std::cout << deletedNode->GetData();
    std::cout << "\n";
    std::cout << "\n";
    list->DisplayList();
    std::cout << "\n";
    
    list->ReverseList();
    std::cout << "\n";
    std::cout << "\nReverse of the List.\n";
    list->DisplayList();
    std::cout << "\n";

}