数据结构各章实验源代码; 数据结构实验源代码
源代码在线查看: 习题03-建立单链表(3).c
#include "datastru.h"
#include
#include
int count_nohead(LINKLIST *head){
/*不带头结点的单链表:输出单链表元素值并计数*/
int i = 0;
LINKLIST *p;
p = head;
printf("输出单链表元素值 : ");
while(p != NULL)
{i++;
printf(" %c",p->data);
p = p->next;}
printf("\n");
return i;
}
LINKLIST *creatlink_nohead_rail(LINKLIST *head){
/*用尾插入法建立不带头结点的单链表*/
LINKLIST *last, *t;
char ch;
last = head;
printf("单链表元素值为单个字符, 连续输入,$为结束字符 : ");
while ((ch = getchar()) != '$')
{t = (LINKLIST *)malloc(sizeof(LINKLIST));
t->data = ch;
t->next = NULL;
if (head == NULL) {head = t; last = t;}
else { last->next = t; last = t;}
}
return (head);
}
main()
{
LINKLIST *head= NULL;
int num;
printf("\n 建立单链表\n\n");
head = creatlink_nohead_rail(head);
fflush(stdin);
num = count_nohead(head);
printf("单链表元素个数 = %d\n", num);
}