突然想到,于是写了一段代码,但是……完全想不到该怎么释放内存
这代码就是突然间想到,然后……写了这么一段,所以请不要在意它是干什么的。程序代码:
#include <stdio.h> #include <stdlib.h> struct shuzi { int n; int count; struct shuzi * p; }; struct shuzi * digui(int n,struct shuzi *q); void PRINTF(struct shuzi *p); int main(void) { struct shuzi *q; int n; q = NULL; while(scanf("%d",&n) == 1) q = digui(n,q); PRINTF(q); return 0; } struct shuzi * MALLOC(void); struct shuzi * digui(int n,struct shuzi *q) { if(q == NULL) { q = MALLOC(); q->n = n; q->count = 1; q->p = NULL; } else if(q->n == n) q->count++; else q->p = digui(n,q->p); return q; } struct shuzi * MALLOC(void) { return (struct shuzi *)malloc(sizeof(struct shuzi)); } void PRINTF(struct shuzi *p) { if(p != NULL) { PRINTF(p->p); printf("%d %d\n",p->n,p->count); } }
[此贴子已经被作者于2017-2-14 23:17编辑过]