| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 574 人关注过本帖
标题:为什么head=setnull(&head);里面的形参不用(head)?
只看楼主 加入收藏
y218z903
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2007-7-27
收藏
 问题点数:0 回复次数:1 
为什么head=setnull(&head);里面的形参不用(head)?

# define null 0

typedef char ElemType; /* 字符型数据*/

typedef struct LNode
{
ElemType data;
struct LNode *next;
};
setnull(struct LNode **p);
int length (struct LNode **p); // 不知道为什么要这么用
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p);



main()
{
struct LNode *head,*q; /*定义静态变量*/
int select,x1,x2,x3,x4;
int i,n;
int m,g;
char e,y;

head=setnull(&head); /*建议链表并设置为空表*/
printf("请输入数据长度: ");
scanf("%d",&n);
for(i=1;i<n;i++);
{
printf("将数据插入到单链表中: ");
scanf("%d",&y);
insert(&head,y,i);} /*插入数据到链表*/
display(&head); /*显示链表所有数据*/

printf("select 1 求长度 length()\n");
printf("select 2 取结点 get()\n");
printf("select 3 求值查找 locate()\n");
printf("select 4 删除结点 delete()\n");
printf("input your select: ");
scanf("%d",&select);
switch(select)
{
case 1:
{
x1=length(&head);
printf("输出单链表的长度%d ",x1);
display(&head);
}break;

case 2:
{
printf("请输入要取得结点: ");
scanf("%d",&m);
x2=get(&head,m);
printf(x2);
display(&head);
}break;

case 3:
{
printf("请输入要查找的数据: ");
scanf("%d",&e);
x3=locate(&head,e);
printf(x3);
display(&head);
}break;

case 4:
{
printf("请输入要删除的结点: ");
scanf("%d",&g);
x4=delete(&head,g);
printf(x4);
display(&head);
}break;
}
}
}


setnull(struct LNode **p)
{
*p=null;
}

int length (struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=null)
{
n++;
q=q->next;
}
return(n);
}

ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j<i&&q!=null)
{
q=q->next;
j++;
}
if(q!=null)
return(q->data);
else
printf("位置参数不正确!\n");
}

int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=null&&q->data!=x)
{
q=q->next;
n++;
}
if(q==null)
return(-1);
else
return(n+1);
}

void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=x;
q=*p;
if(i==1)
{
s->next=q;
p=s;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置参数不正确!\n");
}
}

int delete(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(q->next!=null&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置参数不正确!\n");
}
if(t=null)
free(t);
}

void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示: ");
if(q==null)
printf("链表为空!");
else if (q->next==null)
printf("%c\n",q->data);
else
{
while(q->next!=null)
{
printf("%c->",q->data);
q=q->next;
}
printf("%c",q->data);
}
printf("\n");
}

搜索更多相关主题的帖子: head setnull 形参 
2007-10-11 20:17
yangzhifu
Rank: 1
等 级:新手上路
威 望:2
帖 子:433
专家分:0
注 册:2007-4-11
收藏
得分:0 

可以啊,只不过没符初值,里面有一个错误!在vc下运行


方寸之内,剖天下; 方坛之内,析自我;
2007-10-11 21:52
快速回复:为什么head=setnull(&head);里面的形参不用(head)?
数据加载中...
 
   



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

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