#include "stdio.h"
#include "stdlib.h"
#define datatype char
typedef struct node
{
datatype data;
struct node *next;
}Linknode,*Linklist;
Linklist creat_nohead_rail(Linknode *head)
{
Linknode *p,*q;
char ch;
printf("单链表元素为单个字符,连续输入,按$结束输入:\n");
while((ch=getchar())!='$')
{
/*if(head==NULL)
{
head=(Linklist)malloc(sizeof(Linknode));
head->data=ch;
head->next=NULL;
p=head;
}
else
{
q=(Linklist)malloc(sizeof(Linknode));
q->data=ch;
q->next=NULL;
p->next=q;
p=q;
}*/
q=(Linklist)malloc(sizeof(Linknode));
q->data=ch;
q->next=NULL;
if(head==NULL)
{
head=q;
p=q;
}
else
{
p->next=q;
p=q;
}
#include "stdlib.h"
#define datatype char
typedef struct node
{
datatype data;
struct node *next;
}Linknode,*Linklist;
Linklist creat_nohead_rail(Linknode *head)
{
Linknode *p,*q;
char ch;
printf("单链表元素为单个字符,连续输入,按$结束输入:\n");
while((ch=getchar())!='$')
{
/*if(head==NULL)
{
head=(Linklist)malloc(sizeof(Linknode));
head->data=ch;
head->next=NULL;
p=head;
}
else
{
q=(Linklist)malloc(sizeof(Linknode));
q->data=ch;
q->next=NULL;
p->next=q;
p=q;
}*/
q=(Linklist)malloc(sizeof(Linknode));
q->data=ch;
q->next=NULL;
if(head==NULL)
{
head=q;
p=q;
}
else
{
p->next=q;
p=q;
}
}
return head;
}
return head;
}
int countnode_nohead(Linknode *head)
{
Linknode *p;
int count=0;
p=head;
if(p==NULL) return 0;
printf("%c",p->data);
p=p->next;
count=1;
while(p!=NULL)
{
printf("->%c",p->data);
p=p->next;
count++;
}
return count;
}
{
Linknode *p;
int count=0;
p=head;
if(p==NULL) return 0;
printf("%c",p->data);
p=p->next;
count=1;
while(p!=NULL)
{
printf("->%c",p->data);
p=p->next;
count++;
}
return count;
}
void main()
{
int nodecount;
Linknode *head=NULL;
// head=(Linklist)malloc(sizeof(Linknode));
head=creat_nohead_rail(head);
nodecount=countnode_nohead(head);
printf("\n节点数:%d\n",nodecount);
}
{
int nodecount;
Linknode *head=NULL;
// head=(Linklist)malloc(sizeof(Linknode));
head=creat_nohead_rail(head);
nodecount=countnode_nohead(head);
printf("\n节点数:%d\n",nodecount);
}
转载于:https://blog.51cto.com/lopnxrp/325706