大二(上) 数据结构 实现顺序表和多项式计算

就是简单的,数据结构实现顺序表和多项式计算

1.顺序表代码

#include
#include
#define ERROR 0;
#define OK 1;
typedef int ElemType;
typedef struct
{
	int n;
	int maxLength;
	ElemType *element;
} SeqList;

typedef int Status;
Status Init(SeqList *L, int mSize)
{
	L->maxLength = mSize;
	L->n = 0;
	L->element =(ElemType*) malloc(sizeof(ElemType)*mSize);
	if (!L->element)
		return ERROR;
	return OK;
}

//查找
Status Find(SeqList L, int i, ElemType *x)     
{
	if(i<0 || i>L.n - 1)
		return  ERROR;
	*x = L.element[i];
	return OK;
}

//插入
Status Insert(SeqList *L, int i, ElemType x)    
{
	int j;
	if(i<-1 || i>L->n - 1)
		return ERROR;
	if(L->n == L->maxLength)
		return ERROR;
	for (j = L->n - 1; i > i; j++)
		L->element[j + 1] = L->element[j];
	L->element[i + 1] = x;
	L->n = L->n + 1;
	return OK;
}
//删除
Status Delete(SeqList *L, int i) {           
	int j;
	if (i<0 || i>L->n - 1)
		return ERROR;
	for (j = i + 1; j < L->n; j++)
		L -> element[j - 1] = L->element[j];
	L->n--;
	return OK;
}

//输出
Status Output(SeqList L)                   
{
	int i;
	if (!L.n)
		return ERROR;
	for (i = 0; i <= L.n - 1; i++)
		printf("%d", L.element[i]);
	printf("\n");
	return OK;
}

//撤销
void Destroy(SeqList *L)                   
{
	(*L).n = 0;
	(*L).maxLength = 0;
	free((*L).element);
}

void main()
{
	int i;
	SeqList list;
	Init(&list, 10);
	for (i = 0; i < 9; i++)
		Insert(&list, i - 1, i);
	Output(list);
	Delete(&list, 0);
	Output(list);
	Destroy(&list);
	system("pause");
}

2.多项式加减法代码

#include 
#include 
#include 
#include 
using namespace std;

typedef struct node {
	int coef, exp;
	node *next;
}*LinkList, node;

//多项式的初始化
void InitList(LinkList &L)                                
{
	L = new node;
	L->exp = -1;
	L->coef = -1;
	L->next = NULL;

}
int n, x, y;

//多项式的输入
void Input(LinkList &h1, LinkList &h2)
{                                                    
	LinkList l1 = h1;
	LinkList l2 = h2;
	printf("第一个多项式的项数为:\n");
	scanf_s("%d", &n);
	printf("降序依次输入系数与指数:\n");
	for (int i = 0; i < n; i++)
	{
		scanf_s("%d%d", &x, &y);
		node *p1;
		p1 = new node;
		p1->coef = x;
		p1->exp = y;
		p1->next = NULL;
		l1->next = p1;
		l1 = l1->next;
	}
	printf("\n第二个多项式的项数为:\n");
	scanf_s("%d", &n);
	printf("降序依次输入系数与指数:\n");
	for (int i = 0; i < n; i++)
	{
		scanf_s("%d%d", &x, &y);
		node *p2;
		p2 = new node;
		p2->coef = x;
		p2->exp = y;
		p2->next = NULL;
		l2->next = p2;
		l2 = l2->next;
	}
}

//输出系数与指数
void Output(LinkList h) {                         
	h = h->next;
	if (h == NULL) {
		printf("0 0");
	}
	else {
		int cnt = 0;
		while (h != NULL) {
			if (!cnt) {
				printf("%d x^%d  |  ", h->coef, h->exp);
			}
			else {
				printf("%d x^%d  |  ", h->coef, h->exp);
			}
			cnt++;
			h = h->next;
		}
	}
}

//多项式加法
LinkList add(LinkList h1, LinkList h2)              
{
	LinkList r1 = h1;
	LinkList r2 = h2;
	LinkList l3;
	InitList(l3);
	LinkList h3 = l3;
	
//判断h1与h2是否为空 ,为空时直接返回r2或者r1
	if (h1->next == NULL)                                
		return r2;
	if (h2->next == NULL)
		return r1;
	r1 = r1->next;
	r2 = r2->next;
	
 //当r1指数大于或者小于r2的指数时,指向下一个节点
	while (r1 != NULL && r2 != NULL)
	{
		if (r1->exp > r2->exp) {                         
			l3->next = r1;
			l3 = l3->next;
			r1 = r1->next;
		}
		else if (r1->exp < r2->exp) {
			l3->next = r2;
			l3 = l3->next;
			r2 = r2->next;
		}
		//当两个指数相等时,系数相加
		else {                                            
			node *tmp = new node;
			tmp->exp = r1->exp;
			tmp->coef = r1->coef + r2->coef;
			
			//判断系数是否为0
			if (tmp->coef == 0) {                         
			//系数为0,到下一个节点
				r1 = r1->next;                            
				r2 = r2->next;
			}
			//系数不为0,赋值给l3
			else {                                        
				r1 = r1->next;
				r2 = r2->next;
				l3->next = tmp;
				l3 = l3->next;
			}
		}
	}
	if (r1 != NULL) {
	//当r1中无相同指数时,直接赋值给l3
		while (r1 != NULL) {                              
			l3->next = r1;
			l3 = l3->next;
			r1 = r1->next;
		}
	}
	if (r2 != NULL) {
		while (r2 != NULL) {
			l3->next = r2;
			l3 = l3->next;
			r2 = r2->next;
		}
	}
	return h3;

}

//多项式乘法
LinkList mul(LinkList h1, LinkList h2) {            
	LinkList l3;
	InitList(l3);
	LinkList l1 = h1;
	l1 = l1->next;
	 //历遍第一个多项式
	while (l1 != NULL) {                           
		LinkList l4;
		InitList(l4);
		LinkList h4 = l4;
		LinkList l2 = h2;
		l2 = l2->next;
		//历遍第二个多项式
		while (l2 != NULL) {                      
			node* p;
			p = new node;
			 //指数相加,系数相乘
			p->exp = l1->exp + l2->exp;             
			p->coef = l1->coef * l2->coef;
			l4->next = p;
			l4 = l4->next;
			l2 = l2->next;
		}
		//清空l4
		l4->next = NULL;                            
		l1 = l1->next;
		LinkList hh4 = h4;
		//将该次结果加到l3上
		l3 = add(l3, hh4);                        
	}
	return l3;
}

int main()
{
	LinkList h1, h2;
	InitList(h1);
	InitList(h2);
	Input(h1, h2);
	printf("\n两个多项式分别为:\n");
	Output(h1);
	printf("\n");
	Output(h2);
	LinkList h4 = mul(h1, h2);
	LinkList h3 = add(h1, h2);
	printf("\n多项式相乘结果为:\n");
	Output(h4);
	printf("\n");
	printf("多项式相加结果为:\n");
	Output(h3);
	printf("\n");
	system("pause");
	return 0;
}

你可能感兴趣的:(数据结构)