怎么样让指针指向尾结点C语言,C语言:单向链表中尾结点指针通常指向______。...

#includetypedef struct _node

{

int data;

_node *next;

}node,*list;

int getmax_value(list head)

{

if (head->next == null)

{

printf("链表中没有节点.\n");

exit(-1);

}

node *ptr = head->next;

int max = ptr->data;

while (ptr->next != null)

{

ptr = ptr->next;

if (ptr->data > max)

{

max = ptr->data;

}

}

return max;

}

node* getmax_address(list head)

{

if (head->next == null)

{

printf("链表中没有节点.\n");

exit(-1);

}

node *ptr = head->next;

node *max_address = ptr;

while (ptr->next != null)

{

ptr = ptr->next;

if (ptr->data > max_address->data)

{

max_address = ptr;

}

}

return max_address;

}

list creatlist(int num)//num为创建节点个数

{

list head = (node*)malloc(sizeof(node));

head->data = 0;

head->next = null;

node *ptr = head;

node *node = null;

int data;

while (num)

{

printf("请输入节点则值:");

scanf("%d", &data);

node = (node*)malloc(sizeof(node));

node->data = data;

node->next = null;

ptr->next = node;

ptr = node;

head->data++;

num--;

}

return head;

}

void freelist(list head)

{

node* ptr = head;

while (head)

{

head = ptr->next;

free(ptr);

ptr = head;

}

}

int main()

{

list head=creatlist(5);

printf("链表的最大值为:%d\n", getmax_value(head));

printf("链表最大值的地址为:0x%x,%d\n", getmax_address(head), (getmax_address(head))->data);

freelist(head);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值