其他分享
首页 > 其他分享> > S4_笔记(队列,线性结构应用实例)

S4_笔记(队列,线性结构应用实例)

作者:互联网

“ Ctrl AC!一起 AC!”

目录

队列

定义

抽象数据类型描述

顺序存储

顺序存储主要操作

链式储存

链式储存主要操作

线性结构应用实例

一元多项式加法运算

一元多项式乘法运算

链表反转


队列

定义

定义描述:

特点:

先进先出。

抽象数据类型描述

顺序存储

1.主要实现方式:

 2.代码描述:

struct QNode {
	ElementType Data[MaxSize];
	int rear;
	int front;
};
typedef struct QNode* Queue;

 3.顺环队列:

当rear到达数组尾时而front附件还有空位时,可以将rear移动到数组头,从而形成了顺环队列。

 通过rear-front可以得到数组里的队列元素,大小为n的数组最多放n-1个队列元素,方便计算。

顺序存储主要操作

1.入队:

void AddQ(Queue PtrQ, ElementType item) {
	if ((PtrQ->rear + 1) % Maxsize == PtrQ->front) {
		cout << "队列满了" << endl;
		return;
	}
	PtrQ->rear = (PtrQ->rear + 1) % MaxSize;
	PtrQ->Data[PtrQ->rear] = item;
}

2.出队:

ElementType DeleteQ(Queue PtrQ) {
	if (PtrQ->front == PtrQ->rear) {
		cout << "队列空" << endl;
		return Error;
	}
	else {
		PtrQ->front = (PtrQ->front + 1) % MaxSize;
		return PtrQ->Data[PtrQ->front];
	}
}

链式储存

1.主要实现: 

 头对头,尾对尾。

2.代码描述:

struct Node {
	ElementType Data;
	struct Node* Next;
};
struct QNode {
	struct Node* rear;
	struct Node* front;
};
typedef struct QNode* Queue;
Queue PtrQ;

3.内部结构:

 链式储存主要操作

(无头结点实例)

1.入队:

void AddQ(Queue PtrQ, ElementType item) {
	struct Node* temp;
	temp = (struct Node*)malloc(sizeof(struct Node));
	temp->Data = item;
	temp->Next = NULL;
	if (PtrQ->front == NULL) {
		PtrQ->front = PtrQ->rear = temp;
	}
	else {
		PtrQ->rear->Next = temp;
		PtrQ->rear = temp;
	}
	return;
}

2.出队:

ElementType DeleteQ(Queue PtrQ) {
	strcut Node* Front;
	ElementType FrontData;
	if (PtrQ->front == NULL) {
		cout << "队列空" << endl; return Error;
	}
	Front = PtrQ->front;
	if (PtrQ->front == PtrQ->rear) {//说明只有一个元素
		PtrQ->front = PtrQ->rear = NULL;
	}
	else {
		PtrQ->front = PtrQ->front->Next;
	}
	FrontData = Front->Data;
	free(Front);
	return FrontData;
}

线性结构应用实例

一元多项式加法运算

1.思路:

 2.数据结构类型:

 3.数据结构代码描述:

struct PolyNode {
	int coef;//系数
	int expon;//指数
	struct PolyNode* link;
};
typedef struct PolyNode* Polynomial;
Polynomial P1, P2;

4.算法思路:

 5.核心代码:

Polynomial PolyAdd(Polynomial P1, Polynomial P2) {
	Polynomial front, rear, temp;
	int sum;
	rear = (Polynomial)malloc(sizeof(struct PolyNode));
	front = rear;
	while (P1 && P2) {
		switch (Compare(P1->expon, P2->expon)) {
		case 1:
			Attach(P1->coef, P1->expon, &rear);
			P1 = P1->link;
			break;
		case -1:
			Attach(P2->coef, P2->expon, &rear);
			P2 = P2->link;
			break;
		case 0:
			sum = P1->coef + P2->coef;
			if (sum) Attach(sum, P1->expon, &rear);
			P1 = P1->link;
			P2 = P2->link;
			break;
		}
	}
	for (; P1; P1 = P1->link) Attach(P1->coef, P1->expon, &rear);
	for (; P2; P2 = P2->link) Attach(P2->coef, P2->expon, &rear);
	rear->link = NULL;
	temp = front;
	front = front->link;
	free(temp);
	return front;
}

其中的函数:

int Compare(int e1, int e2) {
	if (e1 > e2) return 1;
	else if (e1 < e2) return -1;
	else return 0;
}
void Attach(int c, int e, Polynomial* pRear) {//传址(二级指针)
	Polynomial P;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->coef = c;
	P->expon = e;
	P->link = NULL;
	(*pRear)->link = P;
	*pRear = P;
}

一元多项式乘法运算

1.题目:

输入:每行为每个多项式的信息:个数、系数,指数、系数、指数...

输出:乘积的系数、指数、系数、指数...

2.程序框架:

3.各部分代码:

//头结点法:
Polynomial ReadPoly() {
	Polynomial P, Rear, t;
	int c, e, N;
	cin >> N;
	P = (Polynomial)malloc(sizeof(struct PolyNode));
	P->link = NULL;
	Rear = P;
	while (N--) {
		cin >> c >> e;
		Attach(c, e, &Rear);//见上文
	}
	t = P; P = P->link; free(t);
	return P;
}
Polynomial Mult(Polynomial P1, Polynomial P2) {
	Polynomial P, Rear, t1, t2, t;
	int c, e;
	if (!P1 || !P2) return NULL;
	t1 = P1; t2 = P2;
	P = (Polynomial)malloc(sizeof(struct PolyNode)); P->link = NULL;
	Rear = P;
	while (t2) {
		Attach(t1->coef * t2->coef, t1->expon + t2->expon, &Rear);
		t2 = t2->link;
	}
	t1 = t1->link;
	while (t1) {
		t2 = P2; Rear = P;
		while (t2) {
			e = t1->expon + t2->expon;
			c = t1->coef * t2->coef;
			while (Rear->link && Rear->link->expon > e) {
				Rear = Rear->link;
			}
			if (Rear->link && Rear->link->expon == e) {
				if (Rear->link->coef + c) {
					Rear->link->coef += c;
				}
				else {//等于0,就删掉
					t = Rear->link;
					Rear->link = t->link;
					free(t);
				}
			}
			else {
				t = (Polynomial)malloc(sizeof(struct PolyNode));
				t->coef = c; t->expon = e;
				t->link = Rear->link;
				Rear->link = t;
				Rear = Rear->link;
			}
			t2 = t2->link;
		}
		t1 = t1->link;
	}
	t2 = P; P = P->link; free(t2);
	return P;
}
void PrintPoly(Polynomial P) {
	int flag = 0;
	if (!P) {
		cout << 0 << endl; return;
	}
	while (P) {
		if (!flag) flag = 1;
		else cout << " ";
		cout << P->coef << P->expon;
		P = P->link;
	}
}

链表反转

题目:链表的每K个结点反转一次。

分析:

new为已经逆转的头,old为未逆转的头,temp记录后面的结点,防丢失。

感谢阅读!!!

“ Ctrl AC!一起 AC!”

标签:队列,PtrQ,Polynomial,实例,link,Rear,front,S4,rear
来源: https://blog.csdn.net/m0_62434776/article/details/122520458