user-0 提问: 大一小白第一次发帖，求教一个数据结C语言构链表的问题

回复:
user: user-1
content: typedef struct linkNode{ 	int nExp;  // 指数 	int nCoe;  // 系数 	struct linkNode *pNext; }LINKNODE,*PLINKNODE;  void showLink(linkNode *pHead){ 	if(!pHead){ 		return ; 	} 	 	string strPress = ""; 	linkNode *pTemp = pHead->pNext; 	while(pTemp){ 		char szTemp[500] ={0}; 		sprintf(szTemp,"%dX^%d",pTemp->nCoe,pTemp->nExp);  		if(strPress.empty()){ 		   strPress = szTemp; 		}else{ 			strPress +=(string)"+"+szTemp; 		}  		pTemp=pTemp->pNext; 	}  	printf("%s\n",strPress.c_str()); 	return ; }  void createLink(linkNode * &pHead,int nNumber){ 	if(!pHead){ 		pHead = (PLINKNODE)malloc(sizeof(LINKNODE)); 	}  	linkNode *pTempNode = pHead;  	while(nNumber-->0){ 		LINKNODE *pNode = (PLINKNODE)malloc(sizeof(LINKNODE)); 		memset(pNode,0,sizeof(LINKNODE)); 		 		printf("请输入指数: \n"); 		scanf("%d",&pNode->nExp); 		printf("请输入系数: \n"); 		scanf("%d",&pNode->nCoe); 		pNode->pNext = NULL;  		pTempNode->pNext = pNode; 		pTempNode = pTempNode->pNext; 	} 	 	showLink(pHead);  }  void mergeLink(linkNode *pLeft,linkNode *pRight,linkNode *pResult){  	if(!pResult){ 		pResult = (PLINKNODE)malloc(sizeof(LINKNODE)); 	}  	if(!pLeft || !pLeft->pNext){ 		pResult->pNext = pRight; 	} 	 	if(!pRight || !pRight->pNext){ 		pResult->pNext = pLeft; 	}  	linkNode *pTemp_Left = pLeft->pNext; 	linkNode *pTemp_Right= pRight->pNext; 	 	linkNode *pTemp_Result = pResult; 	 	while(pTemp_Left && pTemp_Right) 	{ 		if(pTemp_Left->nExp >pTemp_Right->nExp){ 			pTemp_Result->pNext = pTemp_Left; 			pTemp_Left = pTemp_Left->pNext; 			pTemp_Result = pTemp_Result->pNext; 			pTemp_Result->pNext = NULL; 		}else if(pTemp_Left->nExp < pTemp_Right->nExp){ 			pTemp_Result->pNext = pTemp_Right; 			pTemp_Right = pTemp_Right->pNext; 			pTemp_Result = pTemp_Result->pNext; 			pTemp_Result->pNext = NULL; 		}else{ 			pTemp_Left->nCoe+= pTemp_Right->nCoe;  			pTemp_Result->pNext = pTemp_Left; 			pTemp_Left = pTemp_Left->pNext; 			pTemp_Result = pTemp_Result->pNext; 			pTemp_Result->pNext = NULL;   			pTemp_Right=pTemp_Right->pNext; 		}  	}  	if(pTemp_Left){ 		pTemp_Result->pNext = pTemp_Left; 	}  	if(pTemp_Right){ 		pTemp_Result->pNext = pTemp_Right; 	} 	 	printf("合并后的表达式：\n"); 	showLink(pResult); 	return ; }    int _tmain(int argc, _TCHAR* argv[]) {  	LINKNODE *pHead_left = NULL; 	 	createLink(pHead_left,4);  	LINKNODE *pHead_right = NULL; 	createLink(pHead_right,4); 	 	LINKNODE *pHead_result = NULL; 	mergeLink(pHead_left,pHead_right,pHead_result);  	return 0; }   写的比较匆忙，内存没有释放，仅供参考

user: user-2
content: 仅供参考：//链表实现一元多项式的加法减法乘法 #include <stdio.h> #include <stdlib.h> typedef struct node {     float coef;   //系数     int expn;     //指数     struct node *next; } PolyNode;      //多项式节点 polynomial node typedef PolyNode* Polynomial; Polynomial createPolynomial() {  //创建多项式     PolyNode *p, *q, *head = (PolyNode *)malloc(sizeof(PolyNode));   //头节点     head->next = NULL;     float coef;     int expn;     printf("输入该多项式每一项的系数和指数，每项一行，输入0 0结束！\n");     while (scanf("%f %d", &coef, &expn) && coef) {   // 默认，按指数递减排列         if (head->next) {             p = head;             while (p->next && expn < p->next->expn)                 p = p->next;             if (p->next) {                 if (expn == p->next->expn) { //有相同指数的直接把系数加到原多项式                     p->next->coef += coef;                     if (p->next->coef > -0.000001 && p->next->coef < 0.000001) { //若是相加后系数为0，则舍弃该节点                         q = p->next;                         p->next = q->next;                         free(q);                     }                 } else {                     q = (PolyNode*)malloc(sizeof(PolyNode));                     q->coef = coef;                     q->expn = expn;                     q->next = p->next;                     p->next = q;                 }             } else {                 p->next = (PolyNode*)malloc(sizeof(PolyNode));                 p = p->next;                 p->coef = coef;                 p->expn = expn;                 p->next = NULL;             }         } else {             head->next = (PolyNode*)malloc(sizeof(PolyNode));             head->next->coef = coef;             head->next->expn = expn;             head->next->next = NULL;         }     }     return head; } Polynomial multiply(Polynomial poly, float coef, int expn) {  //多项式与指定单项式相乘，该单项式为 coefx^expn     PolyNode *p, *q, *Poly = (PolyNode*)malloc(sizeof(PolyNode));     p = Poly;     q = poly->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = (q->coef*coef);         p->expn = (q->expn + expn);         q = q->next;     }     p->next = NULL;     return Poly; } void add(Polynomial poly1, Polynomial poly2) {  //把 poly2 加到 poly1 上     PolyNode *p, *q, *r;     r = poly1;     p = poly1->next;  //指向第一个节点     q = poly2->next;     poly2->next = NULL;     while (p && q) {         if (p->expn > q->expn) {             r->next = p;             p = p->next;             r = r->next;         } else if (p->expn < q->expn) {             r->next = q;             q = q->next;             r = r->next;         } else {             PolyNode *t;             p->coef += q->coef;             if (!(p->coef > -0.000001 && p->coef < 0.000001)) //系数不为0             {                 r->next = p;                 r = r->next;                 p = p->next;             } else {                 t = p;                 p = p->next;                 free(t);             }             t = q;             q = q->next;             free(t);         }     }     if (p)         r->next = p;     if (q)         r->next = q; } Polynomial polySubtract(Polynomial poly1, Polynomial poly2) {  //多项式减法 poly1-poly2形成一个新的多项式     //把poly2的系数取相反数，形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode)); //构造头节点     PolyNode *p, *q;     p = poly;     q = poly2->next;     while (q) {         p->next = (PolyNode*)malloc(sizeof(PolyNode));         p = p->next;         p->coef = -(q->coef);  //系数取反         p->expn = q->expn;         q = q->next;     }     p->next = NULL;     add(poly, poly1);  //利用加法     return poly; } Polynomial polyAdd(Polynomial poly1, Polynomial poly2) { //多项式相加 poly1+poly2形成一个新的多项式     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //和多项式的头节点     poly->next = NULL;     PolyNode *p, *q, *r;     r = poly;     p = poly1->next;     q = poly2->next;     while (p&&q) {         if (p->expn > q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = p->coef;             r->expn = p->expn;             p = p->next;         } else if (p->expn < q->expn) {             r->next = (PolyNode*)malloc(sizeof(PolyNode));             r = r->next;             r->coef = q->coef;             r->expn = q->expn;             q = q->next;         } else {             float m = p->coef + q->coef;             if (!(m > -0.000001 && m < 0.000001)) {                 r->next = (PolyNode*)malloc(sizeof(PolyNode));                 r = r->next;                 r->coef = m;                 r->expn = p->expn;             }             q = q->next;             p = p->next;         }     }     while (p) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = p->coef;         r->expn = p->expn;         p = p->next;     }     while (q) {         r->next = (PolyNode*)malloc(sizeof(PolyNode));         r = r->next;         r->coef = q->coef;         r->expn = q->expn;         q = q->next;     }     r->next = NULL;     return poly; } Polynomial polyMultiply(Polynomial poly1, Polynomial poly2) {  //多项式相乘     Polynomial poly = (PolyNode*)malloc(sizeof(PolyNode));  //创建多项式和的头节点     poly->next = NULL;     PolyNode *p;     p = poly2->next;     while (p) {         add(poly, multiply(poly1, p->coef, p->expn));         p = p->next;     }     return poly; } void printPoly(Polynomial poly) {  //打印多项式     if (poly && poly->next) {         PolyNode *p = poly->next;  //p指向第一个节点         while (p->next) {             printf("%gx^%d", p->coef, p->expn);             p = p->next;             if (p && (p->coef > 0))                 printf("+");         }         if (p->expn == 0)             printf("%g", p->coef);   //打印常数项         else             printf("%gx^%d", p->coef, p->expn);         printf("\n");     } } void freePoly(Polynomial poly) {  //释放内存     if (poly && poly->next) {         PolyNode *p, *q;         p = poly;         while (p) {             q = p->next;             free(p);             p = q;         }     }     poly = NULL; } int main() {     printf("用链表实现多项式的加减法\n");     Polynomial poly1, poly2, poly3;     printf("创建多项式一\n");     poly1 = createPolynomial();     printf("多项式一：\n");     printPoly(poly1);     printf("创建多项式二\n");     poly2 = createPolynomial();     printf("多项式二：\n");     printPoly(poly2);     printf("两多项式相加，和为：\n");     poly3 = polyAdd(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两个多项式相乘，积为：\n");     poly3 = polyMultiply(poly1, poly2);     printPoly(poly3);     freePoly(poly3);     printf("两多项式相减，差为：\n");     poly3 = polySubtract(poly1, poly2);     printPoly(poly3);     freePoly(poly1);     freePoly(poly2);     freePoly(poly3);     system("pause");     return 0; }

user: user-2
content: 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。  数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633

user: user-3
content: #include<stdio.h> #include<malloc.h> #include<stdlib.h>  typedef struct LNode { 	int coe; 	int exp; 	struct LNode *next; }LNode,*linklist;  void creat(linklist &L,int n); void merge(linklist la,linklist lb,linklist &lc); void format(int coe,int exp); void show(linklist L); int main() { 	linklist ha,hb,hc; 	int Na,Nb;  	printf("请输入多项式ha的项数Na：");  	scanf("%d",&Na);  	printf("请输入多项式ha各项的系数和指数："); 	creat(ha,Na);  	show(ha); 	printf("请输入多项式hb的项数Nb：");  	scanf("%d",&Nb);  	printf("请输入多项式hb各项的系数和指数："); 	creat(hb, Nb);  	show(hb); 	merge(ha,hb,hc); 	printf("合并后的hc为:"); 	show(hc); 	return 0;                                                                                                                          }  void creat(linklist &L,int n) { 	int i; 	linklist q,p; 	L = (linklist)malloc(sizeof(LNode)); 	if (!L) 		exit(0); 	L->next = NULL; 	q = L; 	for(i=1;i<=n;i++) 	{ 		p = (linklist)malloc(sizeof(LNode)); 		if (!p) 			exit(0); 		scanf("%d%d", &p->coe,&p->exp); 		q->next = p; 		q = q->next; 	} 	p->next = NULL; }  void merge(linklist la,linklist lb,linklist &lc) { 	linklist pa, pb, pc, ptmp; 	pa = la->next; 	pb = lb->next; 	lc = la; 	pc = la;  	while(pa && pb) 	{ 		if(pa->exp < pb->exp) 		{ 			pc->next = pa; 			pc = pa; 			pa = pa->next; 		} 		else if(pa->exp > pb->exp)  		{ 			pc->next = pb; 			pc = pb; 			pb = pb->next; 		} 		else 		{ 			pa->coe = pa->coe + pb->coe; 			pc->next = pa; 			pc = pa; 			ptmp = pb->next; 			free(pb); 			pa = pa->next; 			pb = ptmp; 		} 	} 	if(pa)  		pc->next = pa; 	else  		pc->next=pb; }  void format(int coe, int exp) { 	if(coe!=0) 		printf("%dX^%d", coe, exp); }  void show(linklist L) { 	linklist p; 	p = L->next; 	while (p->next) 	{ 		format(p->coe, p->exp); 		if(p->next!=NULL)  			printf("+"); 		p = p->next; 	} 	format(p->coe,p->exp); 	printf("\n"); }  参考一下吧  导致程序异常退出的是hc是野指针，并没有从merge函数里将新链表带到main函数内，因此需要使用引用。 另外merge函数内应将lb中和la相同的删除，防止内存泄露； 其他地方建议对比代码找一下原因。

user: user-4
content: 建议不要对象之间来回赋值 这样容易内存泄漏 而且也容易出错 建议用指针  程序问题 可以设几个断点debug一下

user: user-0
content: 一运行就成这样了，请问这是怎么回事

user: user-0