예제 #1
0
파일: 节点.c 프로젝트: FMsunyh/Algorithm
void main(void)
{
    TYPE *head=NULL,*pnum=NULL;
    int n=3,num;

    /* 创建一个含 n 个节点的链表 */
    printf("input number of node:");
 
    head=creatlink(n);
    printlink(head);
    
    /* 删除链表中值为 num 的节点 */
    printf("Input the deleted number:");
    scanf("%d",&num);
    head=deletelink(head,num);
    printlink(head);

    /* 在链表中插入一个节点 */
    printf("Insert a record\n");
    pnum=(TYPE *)malloc(LEN);
    if(pnum==NULL)
      printf("Pointer is NULL--memory alloc fail!");
    printf("Input the inserted number:");
    scanf("%d",&pnum->num);
    printf("Input the inserted age:");
    scanf("%d",&pnum->age);
    head=insertlink(head,pnum);
    printlink( head );

    /* 销毁链表, 释放动态分配的内存 */
    destroylink( head );
}
예제 #2
0
//阶乘功能函数
int func(int n)
{
    int i;                     //循环控制变量
	INT *tmp = NULL;
    INT *last = NULL;

    INT *head = NULL;
	head = my_malloc();
    init_node(head,0);

    INT *first = NULL;
	first = my_malloc();
	init_node(first, 1);
    insertlink(head, first);


//printf("initial success\n");


	for(i=2; i<=n; i++)
	{
        tmp = head->next;
	    while(NULL != tmp)
		{
		    tmp->num = tmp->num * i;
//printf("tmp->num=%d\n",tmp->num);
			tmp = tmp->next;
		}
//if(i == 30){break;}
        tmp = head->next;
		last = head;
		while(NULL != tmp)
		{
		    if(tmp->num >= 10)
			{
			    if(NULL == tmp->next)
				{
				    tmp->next = my_malloc();
                    init_node(tmp->next,0);
				}

                tmp->next->num = tmp->num / 10 + tmp->next->num;
				tmp->num = tmp->num % 10;
//printf("tmp->next->num=%d\n",tmp->next->num);
//printf("tmp->num=%d\n",tmp->num);
			}

			tmp = tmp->next;
		}
	}


//printf("function success\n");


	while(NULL != head->next)
	{
        tmp = head->next;
		last = head;
	    while(NULL != tmp->next)
		{
		    tmp = tmp->next;
			last = last->next;
		}

		printf("%d",tmp->num);
        
		last->next = NULL;
		free(tmp);
	}
 
    return 0;
}