2016-10-02 156 views
-4

我需要一个将值添加到列表前面的函数。我尝试过的所有东西都不起作用。有人可以告诉我该怎么办?为了显示目的,我尽可能多地删除了不相关的代码。如何将值添加到我的链接列表的前面

Link *fillLst(int);   //Fill a linked list, count backwards 
void prntLst(Link *);  //Print each data element in the list 
void destLst(Link *);  //Destroy the list/deallocate 
Link * endLst(Link *);  //Find the end of the list 
void addLst(Link *,int); //Add data at the end of the list 
int findLst(Link *,int); //Determine what link contains the data 
Link * fndLst(Link *,int); //Determine address of link that contains data 
int cntLst(Link *);  //How many elements are in the list 


//Program Execution Begins Here 

int main(int argc, char** argv) { 
    //Declare a pointer to the linked list and data to test in link creation 
    Link *lnkList; 
    int numList=8,valAdd=42,valFnd1=5,valFnd2=11; 

} 

//Function Create a linked list and fill with data 
//Input -> n  The number of elements in the list to create 
//Output -> front The address to the front of the allocated list. 
Link *fillLst(int n){ 
    //Think of this part as the constructor 
    Link *front=new Link;//Allocate a link at the front of the list 
    front->data=n;  //Initialize with data 
    front->linkPtr=NULL; //At the moment not pointing it to anything 
    Link *next=front; //Create a pointer to progress through the list 

    //Fill the rest of the list with decreasing data down to 1 
    n--; 
    do{ 
     Link *temp=new Link; //Allocate a new link 
     temp->data=n;  //Fill with data 
     temp->linkPtr=NULL; //Place at the end 
     next->linkPtr=temp; //Hook the new link to the end of the list 
     next=temp;   //Move the pointer to the end 
    }while(--n>0);   //Continue till you count down to Zero 
    //Exit by return the original link pointer 
    return front;   //Return the front pointer to the list 
} 

//Function Print the entire contents of the linked list 
//Input -> front The address to the front of the allocated list. 
//Output-> Display the entire linked list. 
void prntLst(Link *front){ 
    Link *next=front;   //Create a pointer to the list 
    cout<<endl<<"The Beginning of the List"<<endl; 
    do{ 
     cout<<next->data<<endl; //Print the contents of the link 
     next=next->linkPtr;  //Go to the next link in the list 
    }while(next!=NULL);   //Loop until reaching the end 
    cout<<"The End of the List"<<endl<<endl; 
} 

//Function Find the address of the last link in the list 
//Input -> front The address to the front of the allocated list. 
//Output-> The address of the last link in the list 
Link *endLst(Link *front){ 
    Link *temp=front,*next; //Declare pointers used to step through the list 
    do{ 
     next=temp;   //Point to the current link with a swap 
     temp=temp->linkPtr; //Point to the next link 
    }while(temp!=NULL);  //Your done when you hit the end 
    return next; 
} 

//Function Add a link and data to the end of the list 
//Input -> front The address to the front of the allocated list. 
//   data Data to embed at the last link in the list 
void addLst(Link *front,int data){ 
    Link *last=endLst(front); //Find the last link 
    Link *add=new Link;  //Create the new link 
    add->data=data;   //Add the data 
    add->linkPtr=NULL;   //Set the pointer to NULL 
    last->linkPtr=add;   //Point to the new end of the list 
} 
+2

*我需要一个函数将值添加到列表的前面* - 为什么你需要一个特殊的函数?无论插入值的链接列表中的哪个位置,插入都应该有效。这不需要编写一个新函数,这需要您调试代码,因为插入代码无法正常工作。 – PaulMcKenzie

+0

为什么只专注于列表的前面部分?通用函数的位置在哪里*插入到列表中?你会有3个独立的功能,前面,中间的某个地方,然后回来?其次,当你编写一个链表时,你应该用铅笔和纸制作链表,为数据绘制链接和框以查看如何插入列表中的任何位置。然后将纸上的内容翻译成代码。 – PaulMcKenzie

+1

评论部分用于评论。我评论道。对于显而易见的事情没有什么帮助 - 当你得到一个链表时,你需要在纸上画出所有必要的操作,以便在纸上看到你所看到的东西时不会卡住,在代码中应用它。 – PaulMcKenzie

回答

1

它与您创建链接列表的方式类似。您需要创建一个节点,并将其余的列表追加到它。

Link* addLst(Link *front, int n) { 
    Link *node = new Link  //Create a new node to add to 
    node -> data = n;   //Add the data 
    add -> linkPtr = front; //Set the pointer to front 
    front = node    //Point front to the new node created 
    return front; 
} 
+0

谢谢你的回复,但那没有奏效。 – equati0n

+0

add是未定义的 – equati0n

相关问题