⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiply.cpp

📁 用链表求300的阶乘,并且返回链表节点的个数.
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#define MAXNODE 300

typedef struct dnode                 /*define a double-link*/
{
	int data;
	struct dnode *next;
	struct dnode *prior;
}LINKNODE;                       

LINKNODE *NodeCreate(LINKNODE *phead,int n)
{
	LINKNODE *p,*plast=phead;
	int i,temp=0;                /*the temp be used to deposit the carry*/  
	for(i=4;i<n+1;i++)           /*calculate the multiply*/
	{
		do
		{
			plast=plast->next;   
			plast->data*=i;		
		}while(plast->next!=NULL);

		plast=phead->next;	

		while(plast->data>=10||plast->next!=NULL)
		{
			temp=plast->data/10;               /*f1 be used to deposit the current carry*/ 
			plast->data=plast->data%10;        
			if(plast->next==NULL)              /*if has a carry then add a node*/
			{
				p=(LINKNODE *)malloc(sizeof(LINKNODE));
				p->data=temp;
				p->next=NULL;
				p->prior=plast;
				plast->next=p;
				plast=plast->next;
			}
	    	else                           
			{
			    plast=plast->next;
			    plast->data+=temp;
			}	
		}	
		if(n==i) return plast;      
		plast=phead;		
	}
	return plast;
	free(p);
}

void main()
{
	LINKNODE *phead=NULL,*p;                  /* p is a current node*/
	int num=0;
	p=(LINKNODE *)malloc(sizeof(LINKNODE));
	p->next=NULL;
	p->prior=NULL;
	phead=p;
	
	p=(LINKNODE *)malloc(sizeof(LINKNODE));
	p->prior=phead;
    p->data=6;
	p->next=NULL;
	phead->next=p;
	phead=NodeCreate(phead,MAXNODE);

	while(phead->prior!=NULL)                  
	{
		
		printf("%d",phead->data);                /*output the result*/
		phead=phead->prior;
		num++;
	}
	printf("\n total return %d nodes.\n",num);   /*output the nember of the nodes*/
	free(p);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -