天天看点

无头结点链表头插法

#include "datastruct.h"

#include "stdio.h"

#include "stdlib.h"

Linklist creat_nohead_head(Linknode *head)

{

 Linknode *p,*q;

 char ch;

 p=head;

 printf("链表中的元素为连续单个值,请连续输入, 输入$结束!\n");

 while((ch=getchar())!='$')

 {

  q=(Linklist)malloc(sizeof(Linknode));

  q->data=ch;

  if(head=NULL)

  {

   head=(Linklist)malloc(sizeof(Linknode));

   head=q;

   p=q;

   head->next=NULL;

  }

  else

   q->next=p;

 }

 return p;

}

int countnode_nohead_head(Linknode *head)

 Linknode *p;

 int count=0;

 if(head==NULL)

  return -1;

 while(p!=NULL)

  printf("%c->",p->data);

  p=p->next;

  count++;

 printf("\n");

 return count;

void main()

 Linknode *head=NULL;

 int count;

 head=creat_nohead_head(head);

 count=countnode_nohead_head(head);

 printf("节点个数为%d",count);

继续阅读