一、链表版
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct{
int cifang;
float xishu;
} Elemtype;
typedef struct LNode{
Elemtype data;
struct LNode *next;
}LNode;//定义链表结点
typedef int Statu;
typedef struct {
LNode *head;
int lenth;
}LinkList;//定义链序表类型
Statu Init_LinkList(LinkList * mylist){//顺序表初始化
mylist->head = (LNode *)malloc(sizeof(LNode));//头结点
if(!mylist->head){
return ERROR;
}
mylist->head->next = NULL;
mylist->lenth = 0;
return OK;
}
void Destroy_LinkList(LinkList * mylist){//销毁顺序表
LNode * p = NULL;
while(mylist->head != NULL){
p = mylist->head;
mylist->head = p->next;
free(p);
}
mylist->lenth = 0;
}
Statu Insert_LinkList(LinkList *mylist, int i, Elemtype elem){//在顺序表第i个位置(从1开始数)插入元素
//请填入代码。
int j;
LNode *pre = mylist->head, *cur = NULL;
cur = (LNode *)malloc(sizeof(LNode));
if(!cur){
return ERROR;
}
cur->data.cifang = elem.cifang;
cur->data.xishu = elem.xishu;
if(i<0 || i>mylist->lenth+1){
return ERROR;
}
for(j = 1; j<i; j++){
pre = pre->next;
}
cur->next = pre->next;
pre->next = cur;
mylist->lenth++;
return OK;
}
Statu Delete_LlinkList(LinkList *mylist, int i, Elemtype *elem){//删除链表第i个结点,并把值传给elem
//请填入代码
int j;
LNode *pre = mylist->head, *cur = NULL;
if(i < 0 || i>mylist->lenth){
return ERROR;
}
for(j = 1; j<i; j++){
pre = pre->next;
}
cur = pre->next;
pre->next = cur->next;
elem->cifang = cur->data.cifang;
elem->xishu = cur->data.xishu;
free(cur);
mylist->lenth--;
return OK;
}
Statu GetElem_LinkList(LinkList mylist, int i, Elemtype * elem){
int j;
LNode *cur = mylist.head;
if(i>0&&i<=mylist.lenth){
for(j = 1; j<=i; j++){
cur = cur->next;
}
(*elem).cifang = cur->data.cifang;
(*elem).xishu = cur->data.xishu;
return OK;
}
else {
return ERROR;
}
}
/
Statu Init_Poly_LinkList(LinkList *mylist){
if(!Init_LinkList(mylist)){
return ERROR;
}
return OK;
}
Statu PolyInsert_Link(LinkList *mylist, Elemtype elem){//按照次方从小到大的顺序插入项
//请填入代码。提示,插入项后,多项式仍然是按照次方从小到大排列,