算法与数据结构:线性数据结构实现与应用

**

实验1:线性数据结构实现与应用

**

实验目的:通过实验达到:
⑴ 理解和掌握线性结构的概念及其典型操作的算法思想;
⑵ 熟练掌握基本线性结构的顺序存储结构、链式存储结构及其操作的实现;
⑶ 理解和掌握受限线性结构——堆栈、队列的概念及其典型操作的算法思想、实现。
实验题目:
题目1:一元多项式的操作
实验要求:
设有两个一元多项式:
p(x)=p0+p1x+p2x2+···+pnxn
q(x)=q0+q1x+q2x2+···+qmxm
:多项式项的系数为实数,指数为整数,设计实现一元多项式操作的程序:
① 多项式链表建立:以(系数,指数)方式输入项建立多项式,返回所建立的链表的头结点;
② 多项式排序:将所建立的多项式按指数非递减(从小到大)进行排序;
③ 多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头指针;
④ 多项式相减:实现两个多项式相减操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头指针;
⑤ 多项式的输出:按照p0+p1x+p2x2+···+pnxn格式输出多项式;
⑥ 主函数通过调用多项式链表建立函数,通过文件读取输入两个多项式并分别输出;输出排序后的两个多项式;调用多项式相加函数实现多项式相加、相减操作,输出操作结果。
测试数据:自行设计2组测试数据,两个多项式尽量不少于4项,并且需要有同类项,至少一个同类项系数相同,另一个同类项系数相反。

以下是C++的做法

  • test_all.h
#ifndef TESTRUN_TEST_ALL_H
#define TESTRUN_TEST_ALL_H
#pragma once

#include <bits/stdc++.h>

using namespace std;
typedef struct ploy {
    float coef;         /*  系数部分  */
    int expn;         /*  指数部分  */
    struct ploy *next;
} PloyNode, *PNode;


//多项式链表建立:返回所建立的链表的头结点
PNode createPoly(const string &fileName) {
    //poly只是一个虚拟的头节点,他的下一个结点才是真正存有数据的第一个结点
    PNode poly = (PloyNode *) malloc(sizeof(PloyNode));
    poly->expn = 0;
    poly->expn = 0;
    PNode head = poly;
    ifstream inFile(fileName);

    if (!inFile) {
        cout << "无法打开" << fileName << "文件" << endl;
        exit(0);
    }

    while (!inFile.eof()) {
        PNode node = (PloyNode *) malloc(sizeof(PloyNode));
        inFile >> node->coef >> node->expn;
        poly->next = node;
        poly = node;
    }

    inFile.close();
    return head->next;
}

bool cmp(pair<float, int> node1, pair<float, int> node2) {
    return node1.second < node2.second;
}

//多项式排序:将所建立的多项式按指数非递减(从小到大)进行排序
void sortPoly(PNode poly) {
    vector<pair<float, int>> temp;
    PNode head = poly;
    while (head) {
        temp.emplace_back(make_pair(head->coef, head->expn));
        head = head->next;
    }
    sort(temp.begin(), temp.end(), cmp);
    head = poly;
    int i = 0;
    while (head) {
        head->coef = temp[i].first;
        head->expn = temp[i++].second;
        head = head->next;
    }
}

//多项式相加:返回生成的多项式的头指针;时间复杂度O(n)
PNode addPoly(PNode poly1, PNode poly2) {
    PNode res = (PloyNode *) malloc(sizeof(PloyNode));
    res->expn = 0;
    res->coef = 0;
    PNode head = res;
    PNode node1 = poly1, node2 = poly2;
    unordered_map<int, float> map;

    while (node1) {
        map[node1->expn] += node1->coef;
        node1 = node1->next;
    }
    while (node2) {
        map[node2->expn] += node2->coef;
        node2 = node2->next;
    }

    for (auto it: map) {
        PloyNode *node = (PloyNode *) malloc(sizeof(PloyNode));
        node->coef = it.second;
        node->expn = it.first;
        head->next = node;
        head = node;
    }
    head->next = NULL;
    return res->next;
}

//多项式相减:返回生成的多项式的头指针,poly1 - poly2
PNode subPoly(PNode poly1, PNode poly2) {
    PNode res = (PloyNode *) malloc(sizeof(PloyNode));
    res->expn = 0;
    res->coef = 0;
    PNode head = res;
    PNode node1 = poly1, node2 = poly2;
    unordered_map<int, float> map;

    while (node1) {
        map[node1->expn] += node1->coef;
        node1 = node1->next;
    }
    while (node2) {
        map[node2->expn] -= node2->coef;//减去poly2的每项即可
        node2 = node2->next;
    }

    for (auto it: map) {
        PloyNode *node = (PloyNode *) malloc(sizeof(PloyNode));
        node->coef = it.second;
        node->expn = it.first;
        head->next = node;
        head = node;
    }
    head->next = NULL;
    return res->next;
}

//显示多项式
void showPoly(PNode poly) {

    PNode head = poly;
    sortPoly(head);
    while (head) {
        cout << head->coef << "*X^" << head->expn;
        if (head->next && head->next->coef >= 0) cout << " + ";
        else if (head->next && head->next->coef < 0) cout << " ";
        head = head->next;
    }
    cout << endl;
}

#endif //TESTRUN_TEST_ALL_H

在这里插入图片描述

  • test_all.cpp
#include "test_all.h"

int main() {
	//绝对路径
    string fileName1 = "/Users/program/cpp_program/clionProject/testRun/testAll/file1.txt";
    string fileName2 = "/Users/program/cpp_program/clionProject/testRun/testAll/file2.txt";
    PNode a = createPoly(fileName1);
    PNode b = createPoly(fileName2);
    cout<<"第一个多项式: ";
    showPoly(a);
    cout<<"第二个多项式: ";
    showPoly(b);
    PNode addRes = addPoly(a, b);
    PNode subRes = subPoly(a, b);
    cout << "相加的结果: ";
    showPoly(addRes);
    cout << "相减的结果: ";
    showPoly(subRes);
    return 0;
}

在这里插入图片描述

  • 运行结果
    在这里插入图片描述
  • 至于输出美观之类的自己设计吧,不是重点

题目2:顺序循环队列的基本操作(选做题)
实验要求:
设计采取少用一个存储单元的方法解决队列满和队列空判断。设队列的元素类型为char,实现顺序循环队列的各种基本操作:
① 初始化队列Q;
② 判断队列Q是否为空;
③ 入队操作;
④ 出队操作;
⑤ 输出队列元素个数;
⑥ 输出队列序列;
⑦ 编写一个测试主函数,要求将若干个元素(不少于8个)入队;出队若干个元素(同时输出出队元素);输出队列序列;通过函数调用实现以上各项操作。

#include<stdio.h>
#include<string.h>
#define maxsize 1000

typedef char DataType;
typedef struct
{
	DataType queue[maxsize];
	int rear;
	int front;
	int count;
}Squeue;

void QueueInit(Squeue *Q);
int QueueEmpty(Squeue Q);
int QueueEmpty(Squeue Q);
int QueuePush(Squeue *Q, DataType elem);
int QueuePop(Squeue *Q, DataType *elem);
void printCount(Squeue Q);
void printQueue(Squeue Q);

/*初始化队列*/
void QueueInit(Squeue *Q)
{
	Q->front = 0;
	Q->rear = 0;
	Q->count = 0;
}

/*判断队列Q是否为空, 空返回1*/
int QueueEmpty(Squeue Q)
{
	if (Q.count == 0)return 1;
	else return 0;
}

/*入队列*/
int QueuePush(Squeue *Q, DataType elem)
{
	if ((Q->rear + 1) % maxsize == Q->front)//少用一个存储单元判断队满
	{
		printf("队列已满\n");
		return 0;
	}
	else
	{
		Q->queue[Q->rear] = elem;
		Q->rear = (Q->rear + 1) % maxsize;
		Q->count++;
		return 1;
	}
}

/*出队列*/
int QueuePop(Squeue *Q, DataType *elem)
{
	if (Q->rear == Q->front)
	{
		printf("队列为空\n");
		return 0;
	}
	else
	{
		*elem = Q->queue[Q->front];
		Q->front = (Q->front + 1) % maxsize;
		Q->count--;
		return 1;
	}
}

/*输出元素的个数*/
void printCount(Squeue Q)
{
	printf("当前队列元素的个数为:%d\n", Q.count);
}

/*输出队列顺序*/
void printQueue(Squeue Q)
{
	if (!QueueEmpty(Q))
	{
		for (int i = 0; i < Q.count; i++)
		{
			printf("%c ", Q.queue[i]);
		}
		printf("\n");
	}
	else printf("队列为空,无法输出元素\n");
}
int main()
{
	Squeue myQueue;
	char *name = "inputmyname", ch;
	QueueInit(&myQueue);
	printCount(myQueue);

	//入队列
	for (int i = 0; i < strlen(name); i++)
		QueuePush(&myQueue, name[i]);
	printf("入队完毕\n");
	printCount(myQueue);
	printf("出队顺序为:\n");
	printQueue(myQueue);
	while(myQueue.count)
	{
		QueuePop(&myQueue, &ch);//出队列
		printf("%c 出队\n", ch);
	}
	return 0;
}

题目3:有序顺序表的设计(P42,2-24)(选做题)
实验要求:
1.有序顺序表的操作,包括初始化,求数据元素个树,插入,删除和取数据元素。放在头文件中(注意有序顺序表的操作与课本上的操作有所不同,需要重写一些操作,如ListInsert(L,x),不需要参数i);
2.设计合并函数ListMerge(L1,L2,L3),其功能是把有序表L1和L2中的数据合并到L3中,要求L3中的数据依然保持有序。(要求时间复杂度O(n), n= n1+n2,n1、n2分别为两个顺序表的长度);
3.设计一个测试主函数实际验证所设计有序表的各项操作以及合并函数的正确性。

题目3难度和题目2差不多的,多了个非递减序和合并,排序方式有多种做法,可以在ListInsert里面实现
补个合并

/*合并函数,时间复杂度为n1+n2*/
void ListMerge(SList L1, SList L2, SList *L3)
{
	int len1 = L1.size, len2 = L2.size;
	int len3 = len2 + len1, i;
	for (i = 0; i < len1; i++)
		ListInsert(L3, L1.list[i]);
	for (i = 0; i < len2; i++)
		ListInsert(L3, L2.list[i]);
	L3->size = len3;
}

题目4:堆栈操作及应用:将一个非负十进制整数转换成d进制,并输出(选做题)
算法思想:十进制数N转换成其他d进制数的转换公式

N=(N DIV d)*d+N MOD d,

其中:DIV为整除运算,MOD为求余运算(取模),d为进制数。计算过程是按照低位到高位的顺序依次产生二进制的各个数位,对打印输出来说,一般应该按照从高位到低位的顺序进行,而这恰好和计算过程相反。因此,如果设置一个顺序栈,将计算过程中得到的二进制数的各个数位顺序进栈,然后再按照出栈序列打印输出,这样就可以得到与输入相对应的二进制数。

#include<stdio.h>
#define maxsize 1000
typedef int Datatype;
typedef struct 
{
	Datatype stack[maxsize];
	int top;
}SStack;
/*初始化*/
void StackInit(SStack *S)
{
	S->top = 0;
}

/*判断是否为空*/
int IsEmpty(SStack S)
{
	if (S.top == 0) return 1;
	else return 0;
}
/*入栈*/
int StackPush(SStack *S, Datatype elem)
{
	if (S->top >= maxsize)
	{
		printf("栈已满\n");
		return 0;
	}
	else
	{
		S->stack[S->top] = elem;
		S->top++;
		return 1;
	}
}
/*出栈*/
int StackPop(SStack *S, Datatype *elem)
{
	if (IsEmpty(*S))
	{
		printf("栈为空\n");
		return 0;
	}
	else
	{
		S->top--;
		*elem = S->stack[S->top];
		return 1;
	}
}

int main()
{
	int N, d, temp;
	SStack S;
	StackInit(&S);
	printf("请输入一个十进制数:");
	scanf("%d", &N);
	printf("想要转换的进制数\n");
	scanf("%d", &d);
	while (N)
	{
		StackPush(&S, N%d);
		N /= d;
	}
	while (!IsEmpty(S))
	{
		StackPop(&S, &temp);
		printf("%d", temp);
	}
	printf("\n");
	return 0;
}
  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值