萌新求助链表题
#include "stdio.h" #include "stdlib.h"
struct node
{ int data;
struct node * next;
} ;
typedef struct node NODE;
typedef struct node * PNODE;
void outlist( PNODE );
int main ( )
{ int num=1;
PNODE head;
head = (PNODE)malloc( sizeof(NODE) );
head->next = NULL;
head->data = -1;
while ( num!=0 )
{ scanf("%d", &num);
if ( num!=0 )
ins_list( head, num);
}
outlist( head );
return 0;
}
void outlist( PNODE head )
{ PNODE p;
p = head->next;
while ( p != NULL )
{ printf("%d\n", p->data);
p = p->next;
}
}
/* This is an example for list. Please programme your code like it.
ins_list( PNODE h, int num )
{
.....
}
*/
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
请问这个要求写一个ins_list函数
答案如下
ins_list( PNODE h,int num)
{
PNODE p,z;
p=(PNODE)malloc(sizeof(NODE));
z=(PNODE)malloc(sizeof(NODE));
p=h;
while (p->next!=NULL)
{
p=p->next;
}
z->next=p->next;
p->next=z;
z->data=num;
}
能不能帮助我理解一下这个函数。。自学看不懂