帮个忙,好不?
在下面的主函数里,struct Node * head=new Node; 该怎么理解的?到底把什么赋值给了前面了?
struct Node { int num; struct Node * next; };
void insert(struct Node *head,struct Node * p) { struct Node * tmp; struct Node * pre; if(head->next==NULL) { head->next=p; p->next=NULL; } else { pre=tmp=head; while(tmp=tmp->next) { if(tmp->num>=p->num) break; else { pre=tmp; } } pre->next=p; p->next=tmp; } }
void display(struct Node * head) { struct Node * p=head; while(p=p->next) { printf("当前节点的值为:%d\n",p->num); } struct Node { int num; struct Node * next; };
void insert(struct Node *head,struct Node * p) { struct Node * tmp; struct Node * pre; if(head->next==NULL) { head->next=p; p->next=NULL; } else { pre=tmp=head; while(tmp=tmp->next) { if(tmp->num>=p->num) break; else { pre=tmp; } } pre->next=p; p->next=tmp; } }
void display(struct Node * head) { struct Node * p=head; while(p=p->next) { printf("当前节点的值为:%d\n",p->num); } } int main(void) { int i;
printf("**************************************\n"); printf("* 本程序实现单链表的初始化/插入/遍历 *\n"); printf("**************************************\n");
struct Node * head=new Node; struct Node * p=head; head->next=NULL;
for(i=0;i<5;i++) { printf("请输入第%d个节点的数据:",i+1); p=new Node; scanf("%d",&(p->num));
insert(head,p); }
display(head);
return 1; }