我試圖在給定節點之前插入一個節點。但我無法獲得所需的輸出。在雙向鏈表中給定節點之前插入一個節點
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* prev;
struct node* next;
};
void insert_beg(struct node** head, int new_data){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = new_data;
if(*head == NULL){
temp->next = *head;
temp->prev = NULL;
*head = temp;
}
else{
temp->next = *head;
(*head)->prev = temp;
*head = temp;
}
}
void insert_before(struct node* next_node,int new_data){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = new_data;
if(next_node == NULL)
printf("Invalid!!!!");
temp->prev = next_node->prev;
temp->next = next_node;
next_node->prev = temp;
if(temp->prev!=NULL)
temp->prev->next = temp;
}
void printList(struct node* head){
if(head == NULL)
printf("The list is empty\n");
else
{
while(head!=NULL){
printf("%d\n",head->data);
head = head->next;
}
}
}
int main(){
struct node* head = NULL;
printList(head);
insert_beg(&head,10);
insert_beg(&head,20);
insert_before(head,70);
insert_beg(&head,30);
printList(head);
}
在這裏,我試圖插入的節點(與數據= 70)前20
輸出:30,20,10
預期輸出:30,70,20, 10
只讀'main',但我沒有看到如何在列表中的第一項之前插入而不通過地址('&head'),因爲'head'變量需要更新。 – user3386109