鍊錶
一元多項式求導 (20分)
設計函式求一元多項式的導數。
以指數遞降方式輸入多項式非零項係數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。
以與輸入相同的格式輸出導數多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。
3 4 -5 2 6 1 -2 0
12 3 -10 1 6 0
解題思路:題中已說明絕對值不超過1000,則可以知曉最大的資料量是2000,此時也可以使用陣列儲存(注意下標的對應,此處有偏移量,所以存在負指數的情況),陣列下標作為指數,陣列值為係數;用數學中的定義求導,按照題目要求,如果求導後係數為零,則不輸出。當然也有特殊情況,當求導後多項式為零時,輸出」0 0「。我分別用結構體陣列和鍊錶實現以上求導過程,並沒有使用整形陣列。
提交**:
編譯器:g++
#include #include using namespace std;
const int maxn = 1002;
struct infarr[maxn];//定義結構體陣列,cof表示係數,exp表示指數
int main()}}
if(index == 0) printf("0 0");
for(int i = 0; i < index; ++i)//輸出結果
return 0;
}
#include #include using namespace std;
typedef struct link link;
struct link;//定義鍊錶單個節點結構
int main()
} if(head==null)//輸出
printf("0 0\n");
while(head)
return 0;
}
一元多項式的乘法與加法運算 (20分)
設計函式分別求兩個一元多項式的乘積與和。
輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入乙個多項式非零項係數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。
輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的係數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0
。
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
解題思路:
採用函式,封裝加法和乘法。先考慮加法,如果指數相同則進行加法操作,如果不同,則將鍊錶按指數遞增方式排列。
對於乘法,我們使用分配律,每乙個元素與多項式相乘必然的到乙個新的多項式,之後的操作無非就是將這若干新的多項式重新做加法運算。
可以在紙上模擬一下該過程,在結合**體會。
提交**
編譯器:g++
#include #include #include using namespace std;
typedef struct inf ply;
struct inf;//定義鍊錶結構
ply *create(ply *p,int n);//建立鍊錶
ply *add(ply *p1,ply *p2);//加法
ply *mul(ply *p1,ply *p2);//乘法
void output(ply *p);//輸出
int main(int argc, const char * argv)
ply *create(ply *p,int n)
return p;
}ply *add(ply *p1,ply *p2)
else if(p1->expexp)
else//指數相同時,係數相加;否則鍊錶按遞增排列
if(tmp->cof)//判斷係數是否為零
tail->next=tmp,tail=tail->next;
}while (p1)//將剩餘的元素放入鍊錶
tail->next=p1,p1=p1->next,tail=tail->next;
while (p2)
tail->next=p2,p2=p2->next,tail=tail->next;
return p->next;
}ply *mul(ply *p1,ply *p2)
p=p->next;
}mul=add(mul, add);//將得到新鍊錶,與之前的結果做加法操作
p1=p1->next;
}return mul;
}void output(ply *p)
printf("\n");
}
兩個有序鍊錶序列的合併 (20分)
已知兩個非降序鍊錶序列s1與s2,設計函式構造出s1與s2的並集新非降序鍊錶s3。
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1-1−1
表示序列的結尾(−1-1−1
不屬於這個序列)。數字用空格間隔。
在一行中輸出合併後新的非降序鍊錶,數字間用空格分開,結尾不能有多餘空格;若新鍊錶為空,輸出null
。
1 3 5 -1
2 4 6 8 10 -1
1 2 3 4 5 6 8 10
解題思路:將相同元素過濾,取其中乙個,並按非遞減排列
提交**:
編譯器:g++
#include #include #include using namespace std;
typedef struct node node;
struct node;
node *create(void);
node *merge(node *l1, node *l2);//做合併操作
void output(node *l);
int main()
node *create(void)
}while(num != -1);
return head;
}node *merge(node *l1, node *l2)
while(l1) tail->next = l1, l1 = l1->next, tail = tail->next;
while(l2) tail->next = l2, l2 = l2->next, tail = tail->next;
return head;
}void output(node *l)
}
兩個有序鍊錶序列的交集 (20分)
已知兩個非降序鍊錶序列s1與s2,設計函式構造出s1與s2的交集新鍊錶s3。
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1-1−1
表示序列的結尾(−1-1−1
不屬於這個序列)。數字用空格間隔。
在一行中輸出兩個輸入序列的交集序列,數字間用空格分開,結尾不能有多餘空格;若新鍊錶為空,輸出null
。
1 2 5 -1
2 4 5 8 10 -1
2 5
解題思路:
將鍊錶中相同的元素取出,並捨棄其餘不同元素。
提交**:
編譯器:g++
#include #include #include using namespace std;
typedef struct node node;
struct node;
node *create(void);
node *merge(node *l1, node *l2);//採用類似合併的操作
void output(node *l);
int main()
node *create(void)
}while(num != -1);
return head;
}node *merge(node *l1, node *l2)
}return head;
}void output(node *l)
}
線性結構 單向鍊錶
一 介紹 單向鍊錶 單鏈表 是鍊錶的一種,其特點是鍊錶的鏈結方向是單向的,對鍊錶的訪問要通過順序讀取從頭部開始 鍊錶是使用指標進行構造的列表 又稱為結點列表,因為鍊錶是由乙個個結點組裝起來的 其中每個結點都有指標成員變數指向列表中的下乙個結點 二 我們建立節點nodeclass node 為了顯示方...
線性結構 雙向鍊錶
雙向鍊錶 1.單向鍊錶的特點 1.只能從頭遍歷到尾,或者從尾遍歷到頭 一般是從頭到尾 即鍊錶數單向的 2.單向鍊錶存在的問題 2.而實際中,我們需要從某個節點嗲它的上乙個節點。如文字編輯中,通過前後箭頭可以使得游標前後移動 3.雙向鍊錶的特點 1.既可以從頭遍歷到尾,也可以從尾遍歷到頭 4.雙向鍊錶...
java實現線性鍊錶結構
package com.hephec.ds public class linkedlist 有參的構造方法 public node t data,node next 儲存該鍊錶的頭節點 private node header 儲存該鍊錶的尾節點 private node tail 儲存該鍊錶中以及包...