使用單鏈表實(shí)現(xiàn)多項(xiàng)式計(jì)算示例
#include <stdio.h>
#include <stdlib.h>
//比較函數(shù)返回值
#define A_EQUAL_B 0
#define A_LARGE_B 1
#define A_LOWER_B -1
//錯誤返回之集合
#define SUCCESS 0
#define POINT_ARG_IS_NULL -1
#define LINKLIST_IS_NULL -2
#define NOT_FOUND -3
typedef struct
{
int cst_term;//常數(shù)項(xiàng)
int idx_term;//指數(shù)
}factor;
typedef factor data_t;
typedef struct linklist
{
data_t data;
struct linklist *next;
}linklist;
/*
*
* malloc頭節(jié)點(diǎn),頭節(jié)點(diǎn)內(nèi)的數(shù)據(jù)是無效的
*
*/
linklist *creat_lnklst()
{
linklist *head = (linklist *)malloc(sizeof(linklist));
head->next = NULL;
return head;
}
/*
* pll是插入節(jié)點(diǎn)的前一項(xiàng)
* data是數(shù)據(jù)
* 為新節(jié)點(diǎn)分配內(nèi)存,之后插入
*
*/
int ins_head_lnklst(linklist *pll, const data_t *data)
{
linklist *newnode = NULL;
if(NULL == pll || NULL == data)
return POINT_ARG_IS_NULL;
newnode = (linklist *)malloc(sizeof(linklist));
newnode->data = *data;
newnode->next = pll->next;
pll->next = newnode;
return SUCCESS;
}
/*
*
* pll是要刪除節(jié)點(diǎn)的前一個
* data存放刪除的數(shù)據(jù)
* 檢查pll是否是空
* 檢查鏈表是否為空
* data是NULL就不用保存刪除的數(shù)據(jù)
*
*/
int del_head_lnklst(linklist *pll, data_t *data)
{
linklist *tmp = NULL;
if(NULL == pll)
return POINT_ARG_IS_NULL;
if(pll->next == NULL)
return LINKLIST_IS_NULL;
tmp = pll->next;
if(NULL != data)
*data = tmp->data;
pll->next = tmp->next;
free(tmp);
return SUCCESS;
}
/*
*
* 內(nèi)部支持data_t大小比較函數(shù)
* 如果data_t修改成其他類型則需要修改此方法
*
*/
static int cmp_data(const data_t *a, const data_t *b)
{
int res = 0;
if(a == NULL || b == NULL)
return POINT_ARG_IS_NULL;
res = a->idx_term - b->idx_term;
if(res == 0)
return A_EQUAL_B;
else if(res > 0)
return A_LARGE_B;
else
return A_LOWER_B;
}
/*
*
* 實(shí)現(xiàn)在鏈表中查找data匹配的前一項(xiàng)
* compare是實(shí)現(xiàn)虛擬數(shù)據(jù)類型比較函數(shù)
*
*/
linklist * locate_lnklst(linklist *pll, const data_t *data, int (*compare)(const data_t *, const data_t *))
{
if(NULL == pll || data == NULL)
return NULL;
if(NULL == compare)
compare = cmp_data;
while(pll->next)
{
if(A_EQUAL_B == compare(&(pll->next->data), data))
return pll;
pll = pll->next;
}
return NULL;
}
/*
*
* 打印鏈表數(shù)據(jù)
* 檢查參數(shù)空指針
* 檢查空鏈表
*
*/
int print_lnklst(linklist *pll)
{
if(NULL == pll)
return POINT_ARG_IS_NULL;
if((pll = pll->next) == NULL)
{
printf("%s\n", "no element in linklist.\n");
return LINKLIST_IS_NULL;
}
while(pll)
{
if(pll->next == NULL)
{
printf("%2d*X^%1d ", pll->data.cst_term, pll->data.idx_term);
}
else
{
printf("%2d*X^%1d +", pll->data.cst_term, pll->data.idx_term);
}
pll = pll->next;
}
printf("\n");
return SUCCESS;
}
/*
*
* 刪除對應(yīng)數(shù)據(jù)的節(jié)點(diǎn)
*
*/
int locate_del_lnklst(linklist *pll, const data_t *data)
{
linklist *prev = NULL;
if(NULL == pll || data == NULL)
return POINT_ARG_IS_NULL;
prev = locate_lnklst(pll, data, NULL);
if(NULL == prev)
return NOT_FOUND;
return del_head_lnklst(prev, NULL);
}
int add_linklist(linklist *a, linklist *b)
{
linklist *tmp;
if(NULL == a || NULL == b)
return POINT_ARG_IS_NULL;
if((b = b->next) == NULL)
{
return LINKLIST_IS_NULL;
}
while(b != NULL)
{
tmp = locate_lnklst(a, &b->data, NULL);
if(NULL == tmp)
ins_head_lnklst(a, &b->data);
else
tmp->next->data.cst_term += b->data.cst_term;
b = b->next;
}
return 0;
}
/*
*
* A = 1 + 2*X + 3*X^2
* B = 4 + 5*X + 6*X^2
* R = A + B
* = 5 + 7*X + 9*X^2
*
*/
int init_a_b(linklist *a, linklist *b)
{
factor tmp;
tmp.cst_term = 1;
tmp.idx_term = 0;
ins_head_lnklst(a, &tmp);
tmp.cst_term = 2;
tmp.idx_term = 1;
ins_head_lnklst(a, &tmp);
tmp.cst_term = 3;
tmp.idx_term = 2;
ins_head_lnklst(a, &tmp);
tmp.cst_term = 4;
tmp.idx_term = 0;
ins_head_lnklst(b, &tmp);
tmp.cst_term = 5;
tmp.idx_term = 1;
ins_head_lnklst(b, &tmp);
tmp.cst_term = 6;
tmp.idx_term = 2;
ins_head_lnklst(b, &tmp);
return 0;
}
int main(int argc, const char *argv[])
{
linklist *fml_a = creat_lnklst();
linklist *fml_b = creat_lnklst();
init_a_b(fml_a, fml_b);
printf("A =");
print_lnklst(fml_a);
printf("B =");
print_lnklst(fml_b);
add_linklist(fml_a, fml_b);
printf("A + B =");
print_lnklst(fml_a);
return 0;
}
上一篇:c++判斷是否為目錄的示例分享
欄 目:C語言
下一篇:udp socket客戶端和udp服務(wù)端程序示例分享
本文標(biāo)題:使用單鏈表實(shí)現(xiàn)多項(xiàng)式計(jì)算示例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3729.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求