天天看点

有头结点链表头插法

#include "datastruct.h"

#include "stdio.h"

#include "stdlib.h"

void creat_head_head(Linknode *head)

{

 Linknode *p,*q;

 char ch;

 head->next=NULL;

 p=head;

 printf("单链表元素为单个字符,连续输入,按$结束输入:\n");

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

 {

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

  q->data=ch;

  q->next=head->next;

  p->next=q;

 }

}

int countnode_head_head(Linknode *head)

 Linknode *p;

 int count=0;

 p=head->next;

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

 count++;

 p=p->next;

 while(p!=NULL)

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

  p=p->next;

  count++;

 return count;

void main()

 Linklist head;

 int count;

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

 creat_head_head(head);

 count=countnode_head_head(head);

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

继续阅读