1. 程式人生 > >資料結構實現順序表和多項式計算

資料結構實現順序表和多項式計算

就是簡單的,資料結構實現順序表和多項式計算

1.順序表程式碼

#include<stdlib.h>
#include<stdio.h>
#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 <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
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;
}