⑴ 數據結構(c語言)用單鏈表存儲一元多項式,並實現兩個多項式的相加運算,怎麼做
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*單項鏈表的聲明*/
typedef struct PolynNode{
int coef; // 系數
int expn; // 指數
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)輸入n個元素的值,建立帶表頭結構的單鏈線性表*/
/*指數系數一對一對輸入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
下載
原文檔已轉碼為如下格式,以便移動設備查看
數據結構(c語言)用單鏈表存儲一元多項式,並實現兩個多項式的相加運算【最新】
閱讀:1037次 頁數:36頁 2016-03-21 舉報
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef int ElemType;
/*單項鏈表的聲明*/
typedef struct PolynNode{
int coef; // 系數
int expn; // 指數
struct PolynNode *next; }PolynNode,*PolynList;
/*正位序(插在表尾)輸入n個元素的值,建立帶表頭結構的單鏈線性表*/
/*指數系數一對一對輸入*/ void CreatePolyn(PolynList &L,int n)
{
int i;
PolynList p,q;
L=(PolynList)malloc(sizeof(PolynNode)); // 生成頭結點
L->next=NULL;
q=L;
printf("成對輸入%d個數據 ",n);
for(i=1;i<=n;i++)
{
p=(PolynList)malloc(sizeof(PolynNode));
scanf("%d%d",&p->coef,&p->expn); //指數和系數成對輸入
q->next=p;
q=q->next;
}
p->next=NULL;
}
// 初始條件:單鏈表L已存在
// 操作結果: 依次對L的每個數據元素調用函數vi()。一旦vi()失敗,則操作失敗
void PolynTraverse(PolynList L,void(*vi)(ElemType, ElemType)) {
PolynList p=L->next;
while(p)
{
vi(p->coef, p->expn);
if(p->next)
{
printf(" + "); //「+」號的輸出,最後一項後面沒有「+」
}
p=p->next;
}
printf(" ");
}
/*ListTraverse()調用的函數(類型要一致)*/ void visit(ElemType c, ElemType e) {
if(c != 0)
{
printf("%dX^%d",c,e); //格式化輸出多項式每一項
}
}
/* 多項式相加,原理:歸並 */ /* 參數:兩個已經存在的多項式 */ /* 返回值:歸並後新的多項式的頭結點 */
PolynList MergeList(PolynList La, PolynList Lb) {
PolynList pa, pb, pc, Lc;
pa = La->next;
pb = Lb->next;
Lc = pc = La; // 用La的頭結點作為Lc的頭結點
while(pa&&pb)
{
if(pa->expn < pb->expn)
{
pc->next = pa; //如果指數不相等,pc指針連上指數小的結
點,
pc = pa;
pa = pa->next; //指向該結點的指針後移
}
else if (pa ->expn > pb->expn )
{
pc->next = pb; //pc指針連上指數小的結點,
pc = pb;
pb = pb->next; //指向該結點的指針後移
}
else //(pa ->expn = pb->expn )
{
pa->coef = pa->coef + pb->coef; //指數相等時,系數相加
pc->next = pa;
pc = pa;
pa = pa->next; //兩指針都往後移
pb = pb->next;
}
}
pc->next = pa ? pa:pb; // 插入剩餘段
return Lc;
}
void main()
{
PolynList ha,hb,hc;
printf("非遞減輸入多項式ha, ");
CreatePolyn(ha,5); // 正位序輸入n個元素的值
printf("非遞減輸入多項式hb, ");
CreatePolyn(hb,5); // 正位序輸入n個元素的值
⑵ C語言:用單鏈表實現任意兩個一元多項式的加、減法運算
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 7
typedef enum { add, nul, sub, div1, yu, l, r }OP;
int a[N][N] = {
{ 0, 0, -1, -1, -1, 1, 2 },
{ 0, 0, -1, -1, -1, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
{ 1, 1, 0, 0, 0, 1, 2 },
};
int top;
OP beg;
int b[1024];
OP op[1024];
void init_nu( ){ top = 0; }
void push_nu( int term ){ b[top++] = term; }
int pop_nu( ){ return b[--top]; }
int is_empty_nu( ){ return top == 0; }
void destory_nu( ) { top = 0; }
void init_sign( ){ beg = 0; }
void push_sign( OP sign ){ op[beg++] = sign; }
void destory_sign( ){ beg = 0; }
OP pop_sign( ){ return op[--beg];}
OP get_sign( ){ return op[beg - 1]; }
int is_empty_sign( ){return beg == 0; }
int eval()
{
int i, j;
i = pop_nu();
j = pop_nu();
switch( pop_sign() )
{
case '+': push_nu( j + i ); break;
case '-': push_nu( j - i ); break;
case '*': push_nu( j * i ); break;
case '/': push_nu( j / i ); break;
case '%': push_nu( j & i ); break;
defult: break;
}
}
int change( char *s )
{
int i;
int n = strlen( s );
for( i = 0; i < n; i++ )
{
if( s[i] >= '0' && s[i] <= '9' )
push_nu( 0 );
while( s[i] >= '0' && s[i] <= '9' )
push_nu( 10 * pop_nu() + s[i++] - '0' );
switch( s[i] )
{
case '+': while( a[add][get_sign()] <= 0 )
eval();
push_sign( add ); break;
case '-': while( a[nul][get_sign()] <= 0 )
eval();
push_sign( nul ); break;
case '*': while( a[sub][get_sign()] <= 0 )
eval();
push_sign( sub ); break;
case '/': while( a[div1][get_sign()] <= 0 )
eval();
push_sign( div1 ); break;
case '%': while( a[yu][get_sign()] <= 0 )
eval();
push_sign( yu ); break;
case '(': push_sign( l ); break;
case ')': while( (get_sign()) != l )
eval();
pop_sign();
break;
defult: break;
}
}
return pop_nu();
}
int main( void )
{
char *s = "((5-3)*2+4/2&2+1)";
init_nu();
init_sign();
printf( "%d\n", change( s ));
destory_nu();
destory_sign();
return 0;
}
以前編的,希望對你有幫助。
別忘了給我分。