int CreateList(LinkList *head)代码的while循环中,加入了一句话:
if (getchar() == '\n') break; // 输入换行时可跳出循环。
新写的int getLength(LinkList head)代码如下:
完整代码为:
#include<stdio.h>。
#include<malloc.h>。
#define ERROR 0。
#define OK 1
typedef int ElemType; /*定义表元素的类型*/。
typedef struct LNode /*线性表的单链表存储*/。
ElemType data;。
struct LNode *next;。
}LNode, *LinkList;。
/*创建单链表*/
int CreateList(LinkList *head);。
/*函数定义*/
int getLength(LinkList head);。
/*你的代码将写在此处*/
int CreateList(LinkList *head)。
LNode *p, *q;。
int e;
*head = (LinkList)malloc(sizeof(LNode));。
if (!*head)
return ERROR;。
(*head)->next = NULL;。
p = *head;
while (scanf("%d", &e) == 1)。
{
if (getchar() == '\n')。
break; // 回车时跳出循环。
q = (LNode *)malloc(sizeof(LNode));。
if (!q)
return ERROR;。
q->data = e;。
q->next = NULL;。
p->next = q;。
p = q;
}
return OK;
}/*CreateList*/。
int getLength(LinkList head)。
LNode *p = head;。
int len = 0;。
while (p != NULL)。
{
++len;
p = p->next;。
}
return len;
int main()
LinkList L = NULL;。
CreateList(&L);。
printf("%d\n", getLength(L));。
return 0;
gcc编译通过,望采纳~
如果不是循环链表要做以下规定的:
表头的prev指针必须初始化为NULL。
表尾的next指针必须初始化为NULL。
于是计算链表长度可以从表头开始迭代,每次迭代计数加一,当遇到next为NULL时结束迭代,结束之后链表的长度就计算出来了。
新建节点时要养成习惯,prev和next未链接的要赋值为NULL。
pascal中length函数只能用于测字符串的长度,不能用于测链表的长度。
#include <stdio.h>。
#include <malloc.h>。
typedef int DataType;。
typedef struct node {。
DataType data;。
struct node *next;。
}*LinkList,*pNode;。
LinkList CreateList() {。
DataType data;。
LinkList head;。
pNode p;
printf("输入整数(q to quit):");。
head = p = (pNode)malloc(sizeof(struct node)); // 生成头结点。
while(scanf("%d",&data) == 1) {。
p->next = (pNode)malloc(sizeof(struct node));。
p->next->data = data;。
p = p->next;。
printf("输入整数(q to quit):");。
}
p->next = NULL;。
return head;
// 查找指定数据值
int FindData(LinkList head,DataType data) {。
pNode p = head->next;。
if(head == NULL || head->next == NULL) return 0;。
while(p) {
if(p->data == data) return 1; // 找打时返回1。
p = p->next;。
}
return 0; // 没找到返回0。
// 查找指定位置的值(版本1)。
int FindPosition1(LinkList head,int pos,DataType *data) {。
int i = 1;
pNode p = head->next;。
if(head == NULL || head->next == NULL) return 0;。
while(p) {
if(i == pos) {。
*data = p->data;。
return 1; // 找到时,返回1。
}
p = p->next;。
++i;
}
return 0; // 没找到返回0;
// 返回链表的长度
int LengthList(LinkList head) {。
int cnt = 0;
pNode p = head->next;。
if(head == NULL || head->next == NULL) return 0;。
while(p) {
++cnt;
p = p->next;。
}
return cnt;
// 查找指定位置的值(版本2)。
int FindPosition2(LinkList head,int pos,DataType *data) {。
int i = 1;
pNode p = head->next;。
if(head == NULL || head->next == NULL) return 0;。
if(pos < 1 || pos > LengthList(head)) return 0;。
while(p) {
if(i == pos) {。
*data = p->data;。
return 1; // 找到时,返回1。
}
++i;
p = p->next;。
}
return 0; // 没找到返回0;
// 显示链表内容
void ShowList(LinkList head) {。
int cnt = 0;
pNode p = head->next;。
while(p) {
if(cnt && cnt % 10 == 0) printf("\n"); // 每行显示10个数据。
printf("%5d",p->data);。
p = p->next;。
++cnt;
}
if(cnt % 10) printf("\n");。
void FreeMemory(LinkList head) {。
pNode q,p = head;。
while(p) {
q = p;
p = q->next;。
free(q);
}
int main() {
int pos;
DataType x;
printf("创建链表A。\n");。
LinkList A = CreateList();。
ShowList(A);
printf("表A的长度是:%d\n",LengthList(A));。
printf("按位置查找,请输入位置:");。
fflush(stdin);。
scanf("%d",&pos);。
if(FindPosition2(A,pos,&x)) printf("位置%d的数据是%d\n",pos,x);。
else printf("%d:数据错误,查找失败。\n",pos);。
FreeMemory(A);。
return 0;
是这样的,你先去确定一下是不是head==A。而不是head.nextNode==A。
如果是的话,那5就是对的。从A开始算,只有5个。到E后是空的,不会再循环。
你可能理解成了head.nextNode==A。
按我的理解,head和A这两个引用的是同一个对象。于是:
while(htemp!=null){//这时候htemp就是A所引用的对象。
len++;//先加1
htemp=htemp.nextNode;//htemp指向B引用的对象。