Example #1
0
void* CMyList::PopIndex(int idx)
{
    if (idx <= 0)
    {
        return PopHead();
    }
    if (idx >= m_NodeCount - 1)
    {
        return PopBack();
    }

    Lock();
    if (isEmpty())
    {
        UnLock();
        return NULL;
    }

    void *tagRet = NULL;
    MYLIST_NODE *pPrev = NULL, *pCurr = m_MyListHead;
    for (int i=0; i<idx; i++)
    {
        if (pCurr)
        {
            pPrev = pCurr;
            pCurr = pCurr->pNext;
        }
    }
    if (pPrev)
    {
        tagRet = pCurr->pData;
        pPrev->pNext = pCurr->pNext;
        FreeMem(pCurr);
    }
    else
    {
        tagRet = m_MyListHead->pData;
        FreeMem(m_MyListHead);
        m_MyListHead = NULL;
        m_MyListTail = NULL;
    }
    m_NodeCount--;
    UnLock();

    return tagRet;
}
static void Pop()//PROBLEM 4
{
	int boolean = 1;
	int content;
	struct node *head = NULL;

	while(boolean)
	{
		if(yesno("Would you like to enter a value (Y/N)?"))
		{
			read_int("Please enter your value: ", &content);
			Push(&head, content);
		}
		else
		{
			boolean = 0;
		}
	}

	boolean = 1; //reset boolean to default to prepare for next while loop

	while(boolean)
	{
		int rv;
		if(yesno("Would you like to pop the head node and return its value (Y/N)?"))
		{
			rv = PopHead(&head);
			printf("The return value for the popped node is %d.\n", rv);
		}
		else
		{
			boolean = 0;
		}
	}
	freeElements(&head);//free up the allocated memory
	if(head == NULL)
	{
		printf("The head node was set to NULL.\n");
	}
}//END PROBLEM 4
Example #3
0
        void Rotate(int nRotate)
        {
            // quick fix
            // need optimization

            if(!Empty()){
                if(auto nCount = std::abs(nRotate)){
                    if(nRotate > 0){
                        for(auto nIndex = 0; nIndex < nCount; ++nIndex){
                            auto stHead = Head();
                            PopHead();
                            PushBack(stHead);
                        }
                    }else{
                        for(auto nIndex = 0; nIndex < nCount; ++nIndex){
                            auto stBack = Back();
                            PopBack();
                            PushHead(stBack);
                        }
                    }
                }
            }
        }