循环链表:Insert,locateNode

时间:2018-04-09 19:25:06

标签: c++ dynamic insert doubly-linked-list circular-list

***Pointer-based dummy-headed circular singly-linked list implementation.  ***

下面是终端输出与正确输出的图片。好像我没有将值插入列表中。这应该是一个足够简单的任务,但是当我真正正确地编写函数时,我似乎无法绕过它,我已经尝试了许多不同的方法,所有这些对我来说都是有意义的。但没有成功;所以我在寻求帮助。

虽然我的问题可能是Insert函数本身;它确实调用了我编写的locateNode函数,因此我已经包含了两个标题,并且这两个函数都可供您查看。任何想法,发现错误或正确的实施,都非常感谢!我以前从未写过原帖,因为这个网站过去对答案非常有帮助。

非常感谢你的时间!!! https://imgur.com/a/XyicY

//INSERT
//***---> 'Insert() Header' --->***  
void Insert(ItemType newItem, bool& success);  
//   ---------------IN------------OUT----------  

// Inserts newItem into its proper sorted position in a sorted   
// list.  Duplicates are not allowed and attempts to insert  
// duplicates should be unsuccessful.  The success flag indicates  
// whether the insertion was successful.  
// Pre:  newItem is defined.
// Post: If insertion is successful, newItem is in its proper  
//   sorted position in the list and success is true; otherwise 
//   success is false. 

//***--->My Insert Function--->***  

void SortedList::Insert(ItemType newItem, bool& success)  
//------------------------------IN------------OUT-------  
{  
//*Declarations*  
int position;  
tNodePtr previous;  //New pointer of tNodePtr Class  
bool isPresent;  

tNodePtr newPtr;        //<--Creates new pointer.
newPtr = new tNode;   //<--Directs it toward: 'new tNode'.
isPresent = locateNode(newItem, previous, position);

    if (isPresent && position>1)
    {

        // No go: this would be a duplicate
        //PushAvail(newItemSubscript); - - - - - - - - - - - ArrayLL_VERSION
        delete newPtr;

        success = false;
    }
    else            
    {
        //WHERE I THINK - MY INSERT (*****PROBLEM*****)  FAILS************>>            

        //list[newItemSubscript].item = newItem; - - - - - - ArrayLL_VERSION
        newPtr->item = newItem;

        //list[newItemSubscript].next = list[previous].next;-ArrayLL_VERSION
        newPtr->next = previous->next;

        //list[previous].next=newItemSubscript;- - - - - - - ArrayLL_VERSION
        previous->next = newPtr;

       //<<***************************************************************
            ++size;
            success = true;
        }
} // end Insert  



//LOCATENODE
//***--->locateNode Header--->***  

bool locateNode(ItemType anItem, tNodePtr& previous, int& position) const;    
    //------------------IN--------------OUT---------------- OUT---

// Returns true if anItem exists in the list, a pointer to the previous  
// (i.e., predecessor) node in the list and and anItem's list position.  
// Returns false if anItem is not in the list and a pointer to what would  
// have been the previous node if anItem had been in the list; the value  
// of position is that of what anItem's would have been if it were in the  
// list.  
// Pre:  anItem is defined.  
// Post: See above.  Note that the value of position lies in the range  
// 1 <= position <= Length()+1. The pointer "previous" points to  
// the last node on the list whose value was less than that of  
// anItem or to the dummy node if the position==1.  

//***My locate Node Function***  
bool SortedList::locateNode  
    (ItemType anItem, tNodePtr& previous, int& position) const  
//------------IN----------------------OUT--------------- OUT-------

{  
//*Declarations*   
    tNodePtr curPtr = head->next;  
    previous = head;  
    int countToPosition = 1; //Using to count each node until anItem is 
found -will return position in the //list   


    if(head->next == head)           //IF:The list is empty.
    {

    }
    else if (anItem == curPtr->item) //ELSE IF:Found at position 1 in the list.
    {

    }
    else                             //ELSE:Continue to search until it's found.
    {
        //Traverse the list until 'curPtr' circles back to head/anItem is found.
        while(curPtr != head && anItem >= curPtr->item)
        {
            if(anItem==curPtr->item) //IF:'anItem' is found!
            {
            //  position = countToPosition; //RETURN POSITION.

                return true;                //RETURN TRUE.
            }

            previous = curPtr;              //Continue traversing.
            curPtr = curPtr->next;
            ++countToPosition;              //Increment position Count.
        }
                                     //IF:anItem is not in the list.

        position = countToPosition;     //Return position that would have 
been if 
                                        //'anItem' were in the list.

        return false;                   //RETURN FALSE.

    // Returns false  and a pointer to what would
    // have been the previous node if anItem had been in the list; 
    }
} // end locateNode

0 个答案:

没有答案