数据结构——链表,望指教!
/*7 设计算法:已知由单链表表示的线性表中,含有三类字符元(比如,字母、数字和其他字母),构造三个以循环链表表示的线性表,使每个表中只含有同一类的字符,且利用原表中的结点空间作为这三个表中的结点空间,头结点可另辟空间。*/#include <stdio.h>#include <stdlib.h>typedef char ElemType;typedef struct LNode{ ElemType date; struct LNode * next;}LNode, * LNodeList;void GreateNode(LNode * L, int n);void PutList(LNode *L, int n);void main(){ LNode l, la, lb, lc; LNodeList p, pa, pb, pc; int n, na=0, nb=0, nc=0; printf("请输入链表的长度:"); scanf("%d", &n); printf("请输入链表的元素:\n"); GreateNode(&l, n); fflush(stdin); PutList(&l, n); p = l.next; pa = &la; pb = &lb; pc = &lc; while (p) { if (p->date >= 'a' && p->date <= 'z' || p->date >= 'A' && p->date <= 'Z') { pa->next = p; pa = pa->next; na++; } else if (p->date >= '0' && p->date <= '9') { pb->next = p; pb = pb->next; nb++; } else { pc->next = p; pc = pc->next; nc++; } p = p->next; } pa->next = &la; pb->next = &lb; pc->next = &lc; printf("la为字母链表,其长度为:%d\n", na); PutList(&la, na); printf("lb为数字链表,其长度为:%d\n", nb); PutList(&lb, nb); printf("lc为其它字符链表,其长度为:%d\n", nc); PutList(&lc, nc);}void GreateNode(LNode * L, int n)//在这里创建出来的链表多了一个不知哪来的结点!在我机子上其date为‘10’{ LNode * p, * q; int i; if (n <= 0) return; p = L; for (i = 0; i < n; i++) { q = (LNode *)malloc(sizeof(LNode)); scanf("%c", & q->date); p->next = q; p = q; } p->next = NULL;}void PutList(LNode *L, int n){ LNodeList p; if (n==0) printf("没有在链表中找到些类数据!\n"); else { p = L->next; while (p != L && p) { printf("%c", p->date); p = p->next; } } printf("\n");}