[英]insertAtTail is not adding node in linkedlist
我是數據結構的新手。 我正在創建一個鏈表程序。 我創建了這個程序。 但是不明白為什么insertAtTail沒有將節點添加到鏈表。
struct node {
int data;
struct node *next;
};
struct node *head=NULL;
int main( void ) {
insertAtHead(3);
insertAtHead(4);
insertAtHead(5);
insertAtHead(8);
insertAtTail(2);
display();
return 0;
}
void insertAtHead( int data ){
struct node *newNode;
newNode = (struct node *)malloc( sizeof(struct node) );
newNode->data = data;
newNode->next = NULL;
if( head == NULL ){
head = newNode;
} else {
newNode->next = head;
head = newNode;
}
}
void insertAtTail(int data){
struct node *newNode, *temp;
newNode = (struct node *)malloc( sizeof(struct node) );
newNode->data = data;
newNode->next = NULL;
if( head == NULL){
head = newNode;
} else {
temp = head;
while ( temp != NULL){
temp = temp->next;
}
temp = newNode;
}
}
void display(){
struct node *temp;
temp = head;
while ( temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
期望輸出:8 - > 5 - > 4 - > 3 - > 2 - > NULL
實際輸出:8 - > 5 - > 4 - > 3 - > NULL
temp = head;
while ( temp != NULL) {
temp = temp->next;
}
temp = newNode;
在這里重新分配temp
將無濟於事。 相反,檢查temp->next
是否為null並使用:
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
這樣,你仍然在改變head
,而不是temp
。
改變這個:
while (temp != NULL)
temp = temp->next;
}
temp = newNode;
對此:
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
因為你要做的就是去舊的最后一個節點 ,並在做完之后,指定舊的最后一個節點的下一個節點 ,即新的最后一個節點是newNode
。
現場演示 (請注意我如何在main方法之前移動方法的定義,以便它可以編譯 - 或者,您可以在main之前聲明原型,並且仍然保留現在的定義)。
PS: 我是否施放了malloc的結果? 沒有。
變量temp
是函數的局部變量,它占用與列表節點不同的內存。
在這個循環之后
temp = head;
while (temp != NULL) {
temp = temp->next;
}
變量temp
設置為NULL。 然后在這個聲明中
temp = newNode;
你正在改變變量本身占用的內存。 列表的節點沒有改變。
按以下方式更改循環
temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
另外要注意兩個主要使用的所有功能都應在使用前聲明。
例如
void insertAtHead( int data );
//...
int main( void )
{
//...
}
此外,函數處理全局變量head
並不是一個好主意。 您應該重寫這些函數,以便將列表的頭部作為參數傳遞給函數。
例如,在這種情況下,函數insertAtHead
可以通過以下方式定義
int insertAtHead( struct node **head, int data )
{
struct node *newNode = malloc( sizeof(struct node) );
int success = newNode != NULL;
if ( success )
{
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
return success;
}
作為一個程序的結果,您可以通過在本地主要聲明他們的頭部來擁有多個列表。
這是一個示范計划
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int insertAtHead( struct node **head, int data )
{
struct node *newNode = malloc( sizeof(struct node) );
int success = newNode != NULL;
if ( success )
{
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
return success;
}
int insertAtTail( struct node **head, int data )
{
struct node *newNode = malloc( sizeof(struct node) );
int success = newNode != NULL;
if ( success )
{
newNode->data = data;
newNode->next = NULL;
while ( *head != NULL ) head = &( *head )->next;
*head = newNode;
}
return success;
}
void display( struct node *head )
{
for ( struct node *current = head; current != NULL; current = current->next )
{
printf("%d -> ", current->data);
}
puts( "NULL" );
}
int main(void)
{
struct node *head = NULL;
insertAtHead( &head, 3 );
insertAtHead( &head, 4 );
insertAtHead( &head, 5 );
insertAtHead( &head, 8 );
insertAtTail( &head, 2 );
display( head );
return 0;
}
它的輸出是
8 -> 5 -> 4 -> 3 -> 2 -> NULL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.