请按照输入整数的顺序建立一个倒序的带表头节点的链表。已知程序的基本结构如下,请你编写 ins_list 函数。
预设代码:
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include "stdio.h"
#include "stdlib.h"
struct node
{ int data;
struct node * next;
} ;
typedef struct node NODE;
typedef struct node * 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;
}
return;
}
/* This is an example for list. Please programme your code like it.
void ins_list( PNODE h, int num )
{
.....
}
*/
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
建立链表按要求写就行了,可以看看书上基础的讲解,单步调试看看变量和地址什么的。(不会单步的百度解决)
下面是自己的代码
void ins_list( PNODE h, int num )
{
PNODE n = (PNODE)malloc(sizeof(NODE));
n->data = num;
n->next = h->next;
h->next = n;
}