我需要一個將值添加到列表前面的函數。我嘗試過的所有東西都不起作用。有人可以告訴我該怎麼辦?爲了顯示目的,我儘可能多地刪除了不相關的代碼。如何將值添加到我的鏈接列表的前面
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
}
*我需要一個函數將值添加到列表的前面* - 爲什麼你需要一個特殊的函數?無論插入值的鏈接列表中的哪個位置,插入都應該有效。這不需要編寫一個新函數,這需要您調試代碼,因爲插入代碼無法正常工作。 – PaulMcKenzie
爲什麼只專注於列表的前面部分?通用函數的位置在哪裏*插入到列表中?你會有3個獨立的功能,前面,中間的某個地方,然後回來?其次,當你編寫一個鏈表時,你應該用鉛筆和紙製作鏈表,爲數據繪製鏈接和框以查看如何插入列表中的任何位置。然後將紙上的內容翻譯成代碼。 – PaulMcKenzie
評論部分用於評論。我評論道。對於顯而易見的事情沒有什麼幫助 - 當你得到一個鏈表時,你需要在紙上畫出所有必要的操作,以便在紙上看到你所看到的東西時不會卡住,在代碼中應用它。 – PaulMcKenzie