判断链表是否为空并且求链表长度
2012-08-10 00:00
197 查看
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *pnext;
};
struct node *creat_list();
void traverse_list(struct node *phead);
bool is_empty(struct node *phead);
int lenght_list(struct node *phead);
int main()
{
struct node *phead;
phead=creat_list();
traverse_list(phead);
if(is_empty(phead))
printf("链表不为空!\n");
else
printf("链表为空!\n");
int len=lenght_list(phead);
printf("链表的节点个数为:%d\n",len);
return 0;
}
//创建链表函数
struct node *creat_list()
{
struct node *phead,*ptail,*pnew;
phead=(struct node *)malloc(sizeof(struct node));
ptail=phead;
ptail->pnext=NULL;
if(NULL==phead)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
int len;//表示要创建的节点数
int val;
printf("请输入要创建的节点数:");
scanf("%d",&len);
for(int i=0;i<len;i++)
{
printf("请输入%d个节点的数据:",i+1);
scanf("%d",&val);
pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
pnew->data=val;
ptail->pnext=pnew;
pnew->pnext=NULL;
ptail=pnew;
}
return phead;
}
//遍历链表函数
void traverse_list(struct node *phead)
{
struct node *p=phead->pnext;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pnext;
}
printf("\n");
return ;
}
//判断链表是否为空函数
bool is_empty(struct node *phead)
{
if(phead->pnext!=NULL)
return true;
else
return false;
}
int lenght_list(struct node *phead)
{
struct node *p=phead->pnext;
int len=0;
while(p!=NULL)
{
len++;
p=p->pnext;
}
return len;
}
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *pnext;
};
struct node *creat_list();
void traverse_list(struct node *phead);
bool is_empty(struct node *phead);
int lenght_list(struct node *phead);
int main()
{
struct node *phead;
phead=creat_list();
traverse_list(phead);
if(is_empty(phead))
printf("链表不为空!\n");
else
printf("链表为空!\n");
int len=lenght_list(phead);
printf("链表的节点个数为:%d\n",len);
return 0;
}
//创建链表函数
struct node *creat_list()
{
struct node *phead,*ptail,*pnew;
phead=(struct node *)malloc(sizeof(struct node));
ptail=phead;
ptail->pnext=NULL;
if(NULL==phead)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
int len;//表示要创建的节点数
int val;
printf("请输入要创建的节点数:");
scanf("%d",&len);
for(int i=0;i<len;i++)
{
printf("请输入%d个节点的数据:",i+1);
scanf("%d",&val);
pnew=(struct node *)malloc(sizeof(struct node));
if(NULL==pnew)
{
printf("分配内存失败,终止程序!\n");
exit(0);
}
pnew->data=val;
ptail->pnext=pnew;
pnew->pnext=NULL;
ptail=pnew;
}
return phead;
}
//遍历链表函数
void traverse_list(struct node *phead)
{
struct node *p=phead->pnext;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pnext;
}
printf("\n");
return ;
}
//判断链表是否为空函数
bool is_empty(struct node *phead)
{
if(phead->pnext!=NULL)
return true;
else
return false;
}
int lenght_list(struct node *phead)
{
struct node *p=phead->pnext;
int len=0;
while(p!=NULL)
{
len++;
p=p->pnext;
}
return len;
}
相关文章推荐
- 判断链表是否有环,环的入口以及环的长度
- 链表--判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 数据结构之链表面试题汇总(三)判断单链表是否有环、取出环的起始点、得到有环链表中环的长度
- 【面试】3.如何判断链表是否有环?如何计算环的长度?
- 用Java实现判断链表是否有环,环的大小,环的长度以及计算链表的长度
- 判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 判断链表是否带环?若带环求环的长度?若带环求环的入口点?并计算以上每个问题的时间复杂度?
- 链表面试题之判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- C语言实现链表之双向链表(十二)判断链表是否为空和获取链表长度
- 链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述
- 判断链表是否有环并求环的入口和环的长度
- 链表的初始化、遍历、删除、插入、判断是否为空、求长度 C语言实现
- 判断一个链表是否有环以及长度
- 判断一个链表中是否有环,并且得到环的入口位置
- 如何判断链表是否有环?如何计算环的长度?
- 剑指offer--判断链表是否带环?若带环求环的长度?若带环求环的入口点?
- 判断两个链表是否相交并且返回第一个交点
- 链表面试题----判断一个单链表是否带环,若带环,求入口点和环的长度
- 17_7_15:判断链表是否有环+求环的长度+求环的入口。设计不能被继承的类,只能堆/栈上创建对象的类