예제 #1
0
void SortPolyn(polynomail *p) {
	Link *p1, *p2;						//采用直接插入排序
	for (p1 = p->head->next; p1; p1 = p1->next) {
		for (p2 = p->head->next; p2 != p1; p2 = p2->next) {
			if (p2->data->expn == p1->data->expn) {	//若后边待排序的节点的指数跟前面已排序
				Link *pri;						//节点的指数相同,则应该合并两项,即删除后者,并入前者
				p2->data->coef += p1->data->coef;
				pri = PriorPos(*p, p1);
				DelFirst(pri, &p1);
				FreeNode(p1);
				--p->len;
				if (fabs(p2->data->coef) < 1e-6) {	//如果合并后系数为0,那也要把这个节点删除
					pri = PriorPos(*p, p2);
					DelFirst(pri, &p2);
					FreeNode(p2);
					--p->len;
				}
				p1 = pri;
				break;
			}
			else if (p1->data->expn < p2->data->expn) {
				Link *pri1, *pri2;
				pri1 = PriorPos(*p, p1);
				DelFirst(pri1, &p1);
				pri2 = PriorPos(*p, p2);
				InsFirst(pri2, p1);
				p1 = pri1;
				break;
			}
		}
	}
	p->tail = GetLast(*p);
}
예제 #2
0
파일: Test.c 프로젝트: wugsh/wgs
main()
{
	DList *plist = NULL;
	PNode p = NULL;
	
	plist = InitList();
	p = InsFirst(plist,MakeNode(1));
	InsBefore(plist,p,MakeNode(2));
	InsAfter(plist,p,MakeNode(3));

	printf("p前驱位置的值为%d\n",GetItem(GetPrevious(p)));
	printf("p位置的值为%d\n",GetItem(p));
	printf("p后继位置的值为%d\n",GetItem(GetNext(p)));
	
	
	printf("遍历输出各节点数据项:\n");
	ListTraverse(plist,print);
	printf("除了头节点该链表共有%d个节点\n",GetSize(plist));
	FreeNode(DelFirst(plist));
	printf("删除第一个节点后重新遍历输出为:\n");
	ListTraverse(plist,print);
	printf("除了头节点该链表共有%d个节点\n",GetSize(plist));
	DestroyList(plist);
	printf("链表已被销毁\n");
}
예제 #3
0
/**
 * 算法2.20,在带头结点的单链线性表L的第i个元素之前插入元素e
 */
Status ListInsert(LinkList &L, int i, ElemType e)
{
	Link h, s;
	if (!LocatePos(L, i-1, h))
		return ERROR;
	if (!MakeNode(s, e))
		return ERROR;
	if (h == L.tail) 
		L.tail = s;   // 修改尾指针
	InsFirst(h, s);       //对于从第不个结点开始的链表,第i-1个结点是它的头结点
	L.len++;
	return OK;
}
예제 #4
0
Status InsBefore(LinkList *linklist, Link **p, Link *h) {
	Link *t;
	if (!linklist || linklist->len == 0 || !h)
		return ERROR;
	for (t = linklist->head; t->next&&t->next != *p; t = t->next);
	if (!t->next)			// p不是链表中的节点
		return ERROR;
	if (InsFirst(t, h) == ERROR)
		return ERROR;
	++linklist->len;
	*p = h;
	return OK;
}
예제 #5
0
Status LinkListInsert(LinkList *linklist, int position, ElemType e) {
	Link *t;									//只能插入到[0,len)之间的位置,不会改变尾结点
	Link *newnode;
	if (!linklist)
		return ERROR;
	if (position != 0) {
		if (LocatePos(*linklist, position - 1, &t) == ERROR)return ERROR;
	}
	else {
		t = linklist->head;
	}
	if (MakeNode(&newnode, e) == ERROR)return ERROR;
	if (InsFirst(t, newnode) == ERROR)return ERROR;
	linklist->len++;
	return OK;
}
예제 #6
0
Link *sub_MultiplyPolyn(Link *p, polynomail *pb) {
	Link *pt, *head = NULL, *newnode = NULL;
	Link *t;
	ElemType e = NULL;
	double coef, coef_ori = p->data->coef;
	int expn, expn_ori = p->data->expn;
	for (pt = pb->head->next; pt; pt = pt->next) {		//遍历pb的每个结点,分别于p相乘
		coef = coef_ori;							//得到的新数据包装成结点并链成新链表
		expn = expn_ori;							//然后返回这一串结点的首节点地址
		coef *= pt->data->coef;
		expn += pt->data->expn;
		if (SetPolyn(&e, coef, expn) == ERROR)return NULL;	//组成数据域
		if (MakeNode(&newnode, e) == ERROR)return NULL;	//包装成行指针域的节点
		if (pt == GetHead(*pb))head = t = newnode;			//将结点插入链表
		else {
			if (InsFirst(t, newnode) == ERROR)return NULL;
			t = t->next;
		}
	}
	return head;
}