Ejemplo n.º 1
0
//得到二个链表公共的结点, 即带环链表的环入口
LinkedList getListsCommonNode(LinkedList first, LinkedList second)
{
    int firLength = getListLength(first);
    int secLength = getListLength(second);
    int diffValue = 0;
    LinkedList firPtr = first;
    LinkedList secPtr = second;

    //长的链表指针先移动二个链表长度之差个单位
    if (firLength > secLength)
    {
        diffValue = firLength - secLength;
        for (; diffValue--; firPtr = firPtr->next);
    }
    else
    {
        diffValue = secLength - firLength;
        for (; diffValue--; secPtr = secPtr->next);
    }

    //查找同一个结点, 二个结点同时移动
    while (firPtr && secPtr)
    {
        firPtr = firPtr->next;
        secPtr = secPtr->next;

        if (firPtr == secPtr)
            return firPtr;
    }

    return NULL;
}
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if (!l1 && !l2) {
            return nullptr;
        }

        int lenL1 = getListLength(l1);
        int lenL2 = getListLength(l2);

        ListNode *merge, *rtn;
        if (lenL1 > lenL2) {
            auto dummy = newDummyList(lenL1 - lenL2);
            dummy.second->next = l2;
            rtn = addTwoList(l1, dummy.first);
            deleteDummyList(dummy);
        } else if (lenL1 < lenL2){
            auto dummy = newDummyList(lenL2 - lenL1);
            dummy.second->next = l1;
            rtn = addTwoList(dummy.first, l2);
            deleteDummyList(dummy);
        } else {
            rtn = addTwoList(l1, l2);
        }

        if (rtn->val < 10) {
            merge = rtn;
        } else {
            int temp = rtn->val;
            merge = new ListNode(temp / 10);
            rtn->val = temp % 10;
            merge->next = rtn;
        }

        return merge;
    }
listNode * LinkedLists::checkOverlapAndReturnNodeNoCycle(listNode *head1, listNode *head2) {
	unsigned int l1 = getListLength(head1);
	unsigned int l2 = getListLength(head2);
	unsigned int diff = std::abs(l1-l2);
	listNode *iter_1 = nullptr;
	listNode *iter_2 = nullptr;

	if (l1 > l2) {
		iter_1 = advanceListByKNodes(diff,head1);
		iter_2 = head2;
	}
	else  {
		iter_1 = advanceListByKNodes(diff,head2);
		iter_2 = head1;
	}

	while ((iter_1 != iter_2) && iter_1 && iter_2) {
		iter_1 = iter_1->next;
		iter_2 = iter_2->next;
	}
	// if any list reached end then no overlap
	if (!iter_1 || !iter_2)
		return nullptr;
	else return iter_1;

}
Ejemplo n.º 4
0
ListNode* findFirstCommonNode(ListNode *head1, ListNode *head2)
{
	if(head1 == NULL || head2 == NULL)
		return 0;
	int length1 = getListLength(head1);
	int length2 = getListLength(head2);
	int lengthDiff = length1 - length2;

	ListNode *pLongList = head1;
	ListNode *pShortList = head2;
	if(lengthDiff < 0)
	{
		pLongList = head2;
		pShortList = head1;
		lengthDiff = head2 - head1;
	}
	for(int i = 0; i < lengthDiff; ++i)
	{
		pLongList = pLongList->m_pNext;
	}
	while(pLongList != NULL && pShortList != NULL && pLongList != pShortList)
	{
		pLongList = pLongList->m_pNext;
		pShortList = pShortList->m_pNext;
	}
	ListNode *commonNode = pLongList;
	return commonNode;

}
Ejemplo n.º 5
0
 ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
     if (headA == NULL || headB == NULL)
     {
         return NULL;
     }
     
     int lenA = getListLength(headA);
     int lenB = getListLength(headB);
     int more = lenA > lenB ? (lenA - lenB) : (lenB - lenA);
     ListNode *pNodeA = headA;
     ListNode *pNodeB = headB;
     if (lenA > lenB)
     {
         pNodeA = goNStep(headA, more);
     }
     else
     {
         pNodeB = goNStep(headB, more);
     }
     
     while(pNodeA != NULL || pNodeB != NULL)
     {
         if (pNodeA == pNodeB)
         {
             return pNodeA;
         }
         pNodeA = pNodeA->next;
         pNodeB = pNodeB->next;
     }
     
     return NULL;
 }
Ejemplo n.º 6
0
//main function
int main(int argc, char *argv[]){
	if (getArgs(argc, argv)){	//check that args are valid
		return 1;
	}
	printf("Beginning conversion program:\n");
	int status = 0;		//for wait() functions
	int i;			//counter
	pid_t child_pid;	//id for fork()

	//create list of files to convert in input_dir
	imageList = createFileList(input_dir);
	printf("    Found %d files to convert\n", getListLength(imageList));

	printf("    Beginning child creation process\n\n");
	//create convert_count processes right away
	for(i = 0; i < convert_count; i++)
	{
		child_pid = fork();	//create child
		if(child_pid != 0){
			//parent removes file that child will convert
			removeNextFileFromList(imageList, child_pid);
		}
		else{
			//child gets pid and calls convert function
			child_pid = getpid();
			convert(argv, imageList, child_pid);
			return 0;
		}
	}
	//after convert_count children were created, keep waiting
		//for children to finish and creating new children
		//until all images have been converted
	printf("Waiting for children to complete...\n\n");
	while(getListLength(imageList) !=  0)
	{
		wait(&status);		//wait for child
		child_pid = fork();	//create new child
		if(child_pid != 0){
			//parent removes file that child will convert
			removeNextFileFromList(imageList, child_pid);
		}
		else{
			//child gets pid and calls convert function
			child_pid = getpid();
			convert(argv, imageList, child_pid);
			return 0;
		}
		
	}
	wait(&status);	//wait for final child to finish
	printf("\nAll conversions completed\n");

	return 0;	//exit success
}
Ejemplo n.º 7
0
 TreeNode *sortedListToBST(ListNode *head) {
     if(head == NULL) {
         return NULL;
     }
     int len = getListLength(head);
     return helper(head, len);
 }
Ejemplo n.º 8
0
void ModelicaListValue::retrieveChildren()
{
  GDBAdapter *pGDBAdapter = GDBAdapter::instance();
  for (int i = 1 ; i <= getListLength() ; i++) {
    StackFramesWidget *pStackFramesWidget = MainWindow::instance()->getStackFramesWidget();
    QByteArray cmd = CommandFactory::getMetaTypeElement(pStackFramesWidget->getSelectedThread(), pStackFramesWidget->getSelectedFrame(),
                                                        mpLocalsTreeItem->getName(), i, CommandFactory::list_metaType);
    pGDBAdapter->postCommand(cmd, GDBAdapter::BlockUntilResponse, mpLocalsTreeItem, &GDBAdapter::getMetaTypeElementCB);
  }
}
Ejemplo n.º 9
0
/*不是用栈*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == NULL && headB == NULL)
            return NULL;
         int lenA,lenB;
         lenA = getListLength(headA);
         lenB = getListLength(headB);
         if(lenA < lenB)
         {
             for(int i=0;i<lenB-lenA;i++)
                 headB = headB->next;
         }
         else
         {
             for(int i=0;i<lenA-lenB;i++)
                headA = headA->next;
         }
         while(headA != NULL && headB != NULL && headA != headB)
         {
             headA = headA->next;
             headB = headB->next;
         }
         return headA;
    }
void *getRandomElement(List_PNTR l){
  unsigned short rand, max;
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return(NULL);
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return(NULL);
  }
  max = getListLength(l) - 1; // elements numbered from 0, so max=length-1
  rand = DAL_random(max);
  return(getElementN(l, rand));
}
Ejemplo n.º 11
0
int isPalindrome(struct ListNode *head){
	int size = getListLength(head);
	struct ListNode *mid = head;
	int i = 0;
	while (i++ < size/2)
		mid = mid->next;
	mid = reverseList(mid);
	i = 0;
	while (i++ < size/2){
		if (head->val != mid->val)
			return false;
		head = head->next;
		mid = mid->next;
	}
	return true;
}
Ejemplo n.º 12
0
Archivo: list.c Proyecto: dayu070/GProm
ListCell *
getNthOfList(List *list, int n)
{
	if (list == NIL)
		return NULL;
	ASSERT(getListLength(list) >= n);
    
	ListCell * node;
    
    node = list->head;
	while (node != NULL && n--)
	{
		node = node->next;
	}
    
    return node ? node : NULL;
}
Ejemplo n.º 13
0
int main(int argc, const char *argv[])
{
    LinkedList firList = getSpecialCycleList(16, 5);

    LinkedList secList = cutListOnIntersectNode(firList);
    printList(firList);
    printf("===============================\n");
    printList(secList);
    printf("===============================\n");
	
	LinkedList intersectNode = getListsCommonNode(firList, secList);
	printf("intersectData == %d\n", intersectNode->data);

	printf("firLoc == %d\nsecLoc == %d\n", getLocationByNode(firList, intersectNode), getLocationByNode(secList, intersectNode));

	printf("ListLength == %d\n", getListLength(firList));
    if (getListsCommonNode(firList, secList))
        printf("链表交点:%p\n", getListsCommonNode(firList, secList));
    else
        printf("无交点!\n");
}
Ejemplo n.º 14
0
 ListNode *rotateRight(ListNode *head, int k) {
     if (!head)
     	return NULL;
     if (!(head->next))
     	return head;
     int len = getListLength(head);
     k %= len;
     if (k == 0)
     	return head;
     ListNode *node1 = head;
     ListNode *node2 = head;
     int i = 0;
     while (node2->next) {
     	if (i >= k) {
     		node1 = node1->next;
     	}
     	node2 = node2->next;
     	i++;
     }
     node2->next = head;
     head = node1->next;
     node1->next = NULL;
     return head;
 }
Ejemplo n.º 15
0
 void reorderList(ListNode* head) {
     if (head == NULL)
         return ;
         
     int listLen = getListLength(head);
     int mid = listLen / 2;
     ListNode *pNode = head;
     
     for (int i = 0; i < mid; i++)       //
     {
         pNode = pNode->next;
     }
     ListNode *pRear = reverseList(pNode);
     ListNode *pFront = head;
     ListNode *tempNode = NULL;
     while(pFront != pRear && pRear != NULL && pRear->next != NULL)
     {
         tempNode = pRear->next;
         pRear->next = pFront->next;
         pFront->next = pRear;
         pFront = pRear->next;
         pRear = tempNode;
     }
 }
Ejemplo n.º 16
0
 ListNode *rotateRight(ListNode *head, int k) {
     if (head == NULL)
     	return head;
     int len = getListLength(head);
     if (len == 1 || len == k)
     	return head;
     k = k % len;
     ListNode * node1 = head;
     ListNode * node2 = head;
     int step = 0;
     while (node2->next != NULL) {
     	if (step < k) {
     		node2 = node2->next;
     	} else {
     		node1 = node1->next;
     		node2 = node2->next;
     	}
     	step++;
     }
     node2->next = head;
     ListNode * res = node1->next;
     node1->next = NULL;
     return res;
 }
Ejemplo n.º 17
0
int main(int argc, char* argv[])
{
	/* Declarations */
	List list;
	List list_1;
	List list_2;
	List list_3;
	List list_4;
	List list_5;
	List list_6;
	Position pos;
	Position pos_1;
	Position pos_2;
	Position pos_3;
	Position pos_4;
	Position pos_a, pos_b;
	int len;
	int idx;
	ElementType elem;

	/* Initialize list */
	list=createList();

	/* Test functions 'insertNode' and 'appendNode' */
	printf("Test functions 'insertNode' and 'appendNode'\n\n");
	printf("Before 'insertNode':\n");
	printList(list);
	pos_1=getHeaderNode(list);
	insertNode(11, list, pos_1);
	pos_2=advanceNode(pos_1);
	insertNode(2, list, pos_2);
	pos_3=advanceNode(pos_2);
	insertNode(3, list, pos_3);
	pos_4=advanceNode(pos_3);
	insertNode(10, list, pos_4);
	insertNode(9, list, pos_2);
	printf("After 'insertNode':\n");
	printList(list);
	printf("Before 'appendNode':\n");
	printList(list);
	appendNode(7, list);
	appendNode(2, list);
	printf("After 'appendNode'\n");
	printList(list);
	printf("\n");

	/* Test functions 'cloneList', 'deleteNode' and 'deleteList' */
	printf("Test functions 'cloneList', 'deleteNode' and 'deleteList'\n\n");
	list_1=cloneList(list);
	printf("Before 'deleteNode':\n");
	printList(list_1);
	deleteNode(2, list_1);
	printf("After 'deleteNode':\n");
	printList(list_1);
	printf("Before 'deleteList':\n");
	printList(list_1);
	deleteList(list_1);
	printf("After 'deleteList':\n");
	printList(list_1);
	printf("\n");

	/* Test function 'getListLength' */
	printf("Test function 'getListLength'\n\n");
	len=getListLength(list);
	printf("Length: %d\n", len);
	printf("\n");

	/* Test functions 'findNode', 'findNodePrevious' and 'getNodeIndex' */
	printf("Test functions 'findNode', 'findNodePrevious' and 'getNodeIndex'\n\n");
	elem=2;
	pos=findNode(elem, list);
	if(pos!=NULL)
	{
		idx=getNodeIndex(pos, list);
		printList(list);
		printf("finding %d, Element at index %d found\n", elem, idx);
	}
	else
	{
		printf("finding %d, not found\n", elem);
	}
	elem=3;
	pos=findNodePrevious(elem, list);
	/* Check whether elem is found in list */
	if(pos->m_next!=NULL)
	{
		idx=getNodeIndex(pos, list);
		printf("finding previous element of %d, Element at index %d found\n", elem, idx);
	}
	else
	{
		/* elem is not in list */
		printf("finding previous element of %d, not found\n", elem);
	}
	printf("\n");

	/* Test functions 'makeListEmpty' and 'isListEmpty' */
	printf("Test functions 'makeListEmpty' and 'isListEmpty'\n\n");
	list_2=cloneList(list);
	printf("Before 'makeListEmpty':\n");
	printList(list_2);
	list_2=makeListEmpty(list_2);
	if(isListEmpty(list_2))
	{
		printf("List emptied successfully\n");
		printf("After 'makeListEmpty'\n");
		printList(list_2);
	}
	printf("\n");

	/* Test functions 'getHeaderNode', 'getFirstNode', 'getLastNode', 'advanceNode' and 'getNodeElem' */
	printf("Test functions 'getHeaderNode', 'getFirstNode', 'getLastNode', 'advanceNode' and 'getNodeElem'\n\n");
	printList(list);
	pos=getHeaderNode(list);
	printf("Header at index %d\n", getNodeIndex(pos, list));
	pos=getFirstNode(list);
	printf("First element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos));
	pos=getLastNode(list);
	printf("Last element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos));
	pos=getFirstNode(list);
	pos=advanceNode(pos);
	printf("Second element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos));
	printf("\n");

	/* Test function 'reverseList' */
	printf("Test function 'reverseList'\n\n");
	list_3=cloneList(list);
	printf("Before 'reverseList':\n");
	printList(list_3);
	list_3=reverseList(list_3);
	printf("After 'reverseList':\n");
	printList(list_3);
	printf("\n");

	/* Test function 'swapNode' */
	printf("Test function 'swapNode'\n\n");
	list_4=cloneList(list);
	printf("Before 'swapNode':\n");
	printList(list_4);
	pos_a=getHeaderNode(list_4);
	pos_a=advanceNode(pos_a);
	pos_a=advanceNode(pos_a);
	pos_b=advanceNode(pos_a);
	swapNode(pos_a, pos_b, list_4);
	printf("After 'swapNode':\n");
	printList(list_4);
	printf("\n");

	/* Test function 'bubbleSortList' */
	printf("Test function 'bubbleSortList'\n\n");
	list_5=cloneList(list);
	printf("Before 'bubbleSortList':\n");
	printList(list_5);
	bubbleSortList(list_5);
	printf("After 'bubbleSortList':\n");
	printList(list_5);
	printf("\n");

	/* Test function 'connectList' */
	printf("Test function 'connectList'\n\n");
	printf("List 1:\n");
	printList(list);
	printf("Length: %d\n", getListLength(list));
	printf("List 2:\n");
	printList(list_5);
	printf("Length: %d\n", getListLength(list_5));
	list_6=connectList(list, list_5);
	printf("Connected list:\n");
	printList(list_6);
	printf("Length: %d\n", getListLength(list_6));
	printf("\n");

	/* Cleanup memory */
	destroyList(list);
	destroyList(list_1);
	destroyList(list_2);
	destroyList(list_3);
	destroyList(list_4);
	destroyList(list_5);
	destroyList(list_6);

	return 0;
}
Ejemplo n.º 18
0
QString ModelicaListValue::getValueString()
{
  return QString("<%1 item%2>").arg(getListLength()).arg(getListLength() > 1 ? "s" : "");
}
Ejemplo n.º 19
0
int getNumberOfExternalSymbols(CodeSection *codeSection)
{
    return getListLength(&codeSection->externalSymbols);
}