数据结构:线性表的应用(最小差值)

数据结构:线性表的应用(最小差值)

【问题描述】

必须利用实验一实现的线性表ADT,完成下面的题目。
对于给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值。

【输入形式】

输入第一行包含一个整数n。   
第二行包含n个正整数,相邻整数之间使用一个空格分隔。

【输出形式】

输出一个整数,表示答案。

【样例输入】

5
1 5 4 8 20

【样例输出】

1

【样例说明】

相差最小的两个数是5和4,它们之间的差值是1。

【样例输入】

5
9 3 6 1 3

【样例输出】

0

【样例说明】

有两个相同的数3,它们之间的差值是0。

【数据规模和约定】

对于所有评测用例,2 ≤ n ≤ 1000,每个给定的整数都是不超过10000的正整数。

【代码提交及评分要求】

源代码请提交工程压缩包,压缩包内至少包含以下三个文件:

1)XXX.h:线性表ADT的定义和声明

2)XXX.h:线性表ADT的实现

3)XXX.cpp:主程序

【代码】

List.h

//#ifndef LIST
//#define LIST
#include<iostream>
using namespace std;

template <typename E> class Node
{
	public:
		E element;
		Node* next;
		Node(Node *pnext=NULL)
		{
			element=0;
			next=pnext;
		}
		Node(const E& it,Node *pnext=NULL)
		{
			element=it;
			next=pnext;
		}
};


template <typename E> class List
{
	private:
		void operator=(const List&){}
		List(const List&){}
	public:
		List(){}
		~List(){}
		virtual void clear()=0;
		virtual void push_back(const E& it)=0;
		virtual void next()=0;
		virtual void MoveToStart()=0;
		virtual const E& getValue() const=0;
};
//#endif

LList.h

#include "List.h"
#include<iostream>
using namespace std;

template <typename E> class LList:public List<E>
{
	private:
		Node<E> *curr;
		Node<E> *head;
		Node<E> *tail;
		int list_size;
		void init()
		{
			head=new Node<E>;
			tail=head;
			curr=tail;
			list_size=0;
		}
		void removeall()
		{
			while(head!=NULL)
			{
				curr=head;
				head=head->next;
				delete curr;
			}
		}
	public:
		LList(int size=100)
		{
			init();
		}
		~LList()
		{
			removeall();
		}
		void clear()
		{
			removeall();
			init();
		}
		void push_back(const E& it)
		{
			tail=tail->next=new Node<E>(it,NULL);
			list_size++;
		}
		void next()
		{
			if(curr!=NULL)
			{
				curr=curr->next;
			}
			else
			{
				return;
			}
		}
		void MoveToStart()
		{
			curr=head->next;
		}

		Node<E>* returnhead()
		{
			return head;
		}
		Node<E>* returncurr()
		{
			return curr;
		}

		const E& getValue() const
		{

			return curr->element;
		}
		
		void Remove(LList<char> &A)
		{
			A.MoveToStart();
			while(A.returncurr()!=NULL)
			{
				if(A.getValue()>='0'&&A.getValue()<='9')
				{
					A.next();
					continue;
				}
				else
				{
					cout<<A.getValue();
					A.next();
				}
			}
        }
};

main.cpp

#include "LList.h"
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main() 
{
	int aa;
	cin>>aa;
	int A[aa];
	for(int i=0;i<aa;i++)
	{
		cin>>A[i];
	}
	sort(A,A+aa);
	LList<int> a(1000);
	
	for(unsigned int i=1;i<aa;i++)
	{
		a.push_back(A[i]-A[i-1]);
	}
	a.MoveToStart();
    int x=A[1]-A[0];
	while(a.returncurr()!=NULL)
	{
		if(a.getValue()<x)
		{
			x=a.getValue();
		}
		
		a.next();
	}
	cout<<x<<endl;

	return 0;
}	 

【代码说明】

问题分析

处理对象:一组int型数据。 实现功能:找出差值(两个数差的绝对值)最小的数,输出这个差值。
结果显示:输出结果为一行,包括一个int型数据,即差值。
样例分析:
【样例输入】 5 1 5 4 8 20
【样例输出】 1
说明:对输入的五个数进行排序,即2,4,5,8,20;再计算相邻两个数的差值,即2,1,3,12;比较四个差值,输出最小的那个数即可。

物理数据对象设计

链表是由一系列称为表的结点的对象组成的。因为结点是一个独立的对象,所以它能很好地实现独立的结点类。
在这里插入图片描述
物理存储结构如下:
链式存储结构,又叫链接存储结构。在计算机中用一组任意的存储单元存储线性表的数据元素(这组存储单元可以是连续的,也可以是不连续的).

算法思想的设计

1,首先输入数的个数,再循环输入各个数,存入数组中;
2,使用sort函数对数组进行排序,并将数组相邻两个数的差值存入链表中;
3,使用最小值中间量,先将链表的第一个数赋值给最小值中间量,然后遍历链表,将链表元素逐个与最小值中间量比较,如果小于最小值中间量,则将该值赋值给最小值中间量。
4,输出最小值中间量。

关键功能的算法步骤

1,将相邻元素的差值存入链表。
for(unsigned int i=1;i<aa;i++)
{
a.push_back(A[i]-A[i-1]);
}
2,最小值中间量与链表遍历值的比较
int x=A[1]-A[0];
while(a.returncurr()!=NULL)
{ if(a.getValue()<x)
{
x=a.getValue();
}
a.next();
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值