求链表长度
2011-03-03 19:45
127 查看
//求链表长度
#include<stdio.h>
#include<stdlib.h>
typedef struct No{
int date;
No* next;
} Node;
Node *creat()
{
Node *head=NULL,*p,*tail;
int x;
scanf("%d",&x);
while(x!=-1){
p=(Node*)malloc(sizeof(Node));
p->date=x;
p->next=NULL;
if(head==NULL)
tail=head=p;
else{
tail->next=p;
tail=p;
}
scanf("%d",&x);
}
return head;
}
int count(Node* head)
{
int sum=0;
Node* p=head;
while(p!=NULL){
sum++;
p=p->next;
}
return sum;
}
main()
{
Node *head;
head=creat();
printf("%d/n",count(head));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct No{
int date;
No* next;
} Node;
Node *creat()
{
Node *head=NULL,*p,*tail;
int x;
scanf("%d",&x);
while(x!=-1){
p=(Node*)malloc(sizeof(Node));
p->date=x;
p->next=NULL;
if(head==NULL)
tail=head=p;
else{
tail->next=p;
tail=p;
}
scanf("%d",&x);
}
return head;
}
int count(Node* head)
{
int sum=0;
Node* p=head;
while(p!=NULL){
sum++;
p=p->next;
}
return sum;
}
main()
{
Node *head;
head=creat();
printf("%d/n",count(head));
return 0;
}
相关文章推荐
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 双链表的建立,测长度,打印,插入c++代码实例及运行结果
- 判断链表是否有环并求环的入口和环的长度
- 链表的创建 查找 排序 插入 删除 逆序 长度 显示
- 实现学生信息录入的动态长度控制(链表)
- 长度从大到小重复不打印长度相等则字典排序—链表插入实现
- 单向链表取中间元素(链表长度未知)
- 剑指offer--判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 链表面试题之判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 给定一个单向链表(长度未知),请遍历一次就找到中间的指针,假设该链表存储在只读存储器,不能被修改
- 单链表之创建与求单链表的长度
- 给定一个单链表,从链表返回一个随机节点的值。 每个节点必须具有相同的选择概率。 跟进: 如果这个链表非常大,而且它的长度不为人知呢? 你能解决这个问题,而不使用额外的空间?
- 快速查找长度未知的单链表的中间结点
- 链表问题:如何判断一个链表有环,以及环的长度,起始位置。
- 经典算法 | 判断任意长度链表是否有环的O(n)时间,O(1)空间算法
- 单链表的建立,测长度,打印,删除,插入,逆置c++代码实例及运行结果
- 双链表的建立,测长度,打印,插入c++代码实例及运行结果
- 求链表中环的长度
- 链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述
- java经典笔试题~求链表是否带环,即其链表的长度