| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1176 人关注过本帖
标题:[求助]struct Node * head=new Node
只看楼主 加入收藏
时光的羁绊
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-5-1
收藏
 问题点数:0 回复次数:2 
[求助]struct Node * head=new Node

帮个忙,好不?

在下面的主函数里,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; }

搜索更多相关主题的帖子: new struct head Node 
2005-06-18 21:40
ooooo
Rank: 1
等 级:新手上路
威 望:1
帖 子:135
专家分:0
注 册:2005-6-24
收藏
得分:0 
struct Node * head=new Node;
分为两部分struct Node *head;     //声明一个指向Node 数据类型的指针 head
new Node;   //是一种动态内存分配的运算 内存分配成功返回一个地址(4字节的无符号整数)为新申请的Node单元的首地址
内存分配失败返回0;
=运算使head 指向新申请的Node单元
一个new 应和一个delete对应

2005-06-24 22:34
newllon
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2005-5-29
收藏
得分:0 
right

2005-06-29 01:51
快速回复:[求助]struct Node * head=new Node
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014193 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved