0

这是一个双向链接列表的代码,我首先创建一个具有某些值的列表,然后在两个节点之间插入一个新节点

#include<stdio.h>
#include<stdlib.h>

   typedef struct nodet{
    int data;
    struct nodet* prev;
    struct nodet* next;

}node;


int main()
{
   node * head = NULL;

   int a[5] = {10,20,30,40,50} ;

   for(int i =0 ;i<5;i++)
   {
      add(&head,a[i]);

   } 
  
   display(head);  // * Now list is - 50 40 30 20 10 *

    
    insert(head,3);   // I am passing head as a value here

    display(head) ;  // * Now the list is : 50 40 30 100 20 10*

    return 0;
 
}
** INSERT FUNCTION **


void insert(node* h,int p)
{
    int c = 1;

     while(h != NULL && c!= 3)
   {  
       
     printf("\nvalue : %d\npostn : %d",h->data,c);
     c++;
    // && c != p
      h =  h->next;

   } 

   node* temp = h ;
   node* temp2 = h->next ;

   node* n = (node*)malloc(sizeof(node)) ;
   n->data =100 ;
   n->prev = NULL;
   n->next = NULL;


   temp->next = n;
   temp2->prev = n;

   n->prev = temp;
   n->next = temp2 ;

}

对于要反映的更改,我们使用 pass by refrence ryt? 但我仍然可以看到传递值的变化。这是怎么回事?我需要澄清一下,提前谢谢你!

4

0 回答 0