数据结构学习中测试代码

线性表

顺序表的一些基本性质



//#define print(x)	std::cout <<x << std::endl
//#define get(x)	std::cin>>x
#include <iostream>
#include <fstream>

using namespace std;


#define Initsize 100
#
typedef struct
{
	int* data;
	int Maxsize, length;


}SeqList;
int main()
{
	SeqList l;
	l.data = (int*)malloc(sizeof(int) * Initsize);
	l.length = 0;
	l.Maxsize = 100;
	for (int i = 0; i <=30; i++)
	{
		l.data[i] = i;
		l.length++;
	}
	
	for (int i = 0; i < l.length; i++)
	{
		std::cout << l.data[i]<<std::endl;
		std::cout << &(l.data[i]) << std::endl;
		std::cout << l.length << std::endl;
		std::cout << "------" << std::endl;
	}

}

顺序表插入

int ListInsert(SeqList &l, int num,int loc)
{
	if (loc<0 || loc>l.length + 1) return false;
	for (int i = l.length; i > loc; i--)
	{
		l.data[i + 1] = l.data[i];
	}
	l.data[loc] = num;
	l.length++;
}

指针方式

int ListInsert1(SeqList& l, int num, int loc)
{
	if (loc<0 || loc>l.length + 1) return false;
	int *q = &l.data[loc];
	for (int *i = &l.data[l.length-1]; i >q; i--)
	{
		*(i + 1) = *i;
	}
	*q = num;
	l.length++;
}

顺序表删除

int ListDelete(SeqList& l, int loc)
{
	if (loc<0 || loc>l.length + 1) return false;
	int* q = &l.data[loc];
	for (int i = loc; i <= l.length; i++)
	{
		*q = *(q + 1);
	}
	l.length--;
}

习题

删除顺序表最小的值,并且返回被删元素的值,删除值的空位由最后一个值代替

int DeleteMin(SeqList& l)
{
	int temp=l.data[0];
	int loc = 0;
	for (int i = 0; i < l.length; i++)
	{
		if (temp > l.data[i])
		{
			temp = l.data[i];
			loc = i;
		}
	}
	l.data[loc] = l.data[l.length - 1];
	l.length--;
	return temp;
}

将表顺序逆置,空间复杂度为O(1)

int ListReverse(SeqList& l)
{
	int mid = 0;
	int temp;
	mid = l.length / 2;
	for (int i = 0; i < mid; i++)
	{
		temp = l.data[i];
		l.data[i]=l.data[l.length - i-1] ;
		l.data[l.length - i-1] = temp;
	}
	return true;
}

时间复杂度为O(n),空间复杂度为O(1),删除所有值为x的数据元素。

int DeleteSimple(SeqList& l, int x)
{
	int k = 0;
	for (int i = 0; i < l.length; i++)
	{
		if (l.data[i] != x) {
			l.data[k] = l.data[i];
			k++;
		}
	}
	l.length = k;
	return true;
}

从顺序表删除值在s和t之间(包含s和t且s<t)的所有元素,若s或t不合理或者顺序表为空,则显示出错并退出运行

int DeleteRan(SeqList& l, int s, int t)
{
	int k = 0;
	if (s >= t || (t - s) > l.length || l.length <= 0) return false;
	for (int i = 0; s < t; i++)
	{
		if (l.data[i] >= s && l.data[i] <= t)
		{
			k++;
		}
		else {
			l.data[i - k] = l.data[i];
		}
	} 
	l.length -= k;

	std::cout << l.length << std::endl;
	return true;
}

删除有序顺序表值重复的元素,使表中值均不相同

我写了一个错误的算法,只能删除重复一次的

int DeleteEqual(SeqList& l)
{
	if (l.length == 0)return false;
	for (int i = 0, j = 1; j < l.length; j++)
	{
		if (l.data[i] == l.data[j])
		{
			for (int t = i; t < l.length-1; t++)
			{
				l.data[t] = l.data[t + 1];
			}
			l.length--;
		}
		i++;
	}
	return true;
}

还有一个算法

bool delete_same(SeqList &l)
{
	if(l.length==0)
		return false;
	int i,j;
	for(i=0,j=1;j<l.length;j++)
	{
		if(l.data[i]!=l.data[j])
			l.data[++i]=l.data[j];//
	l.length=i+1;
	return true;
	}
}

两个表合为一个表,按照顺序排列(重要,教材同款)

bool Merge(SeqList A, SeqList B, SeqList C)
{
	if (A.length + B.length > C.Maxsize)
		return false;
	int i = 0, j = 0, k = 0;
	while (i < A.length && j < B.length)
	{
		if (A.data[i] <= B.data[j])
		{
			C.data[k++] = A.data[i++];
		}
		else {
			C.data[k++] = B.data[j++];
		}
	}
	while(i < A.length)
	{
		C.data[k++] = B.data[j++];

	}
	while (j < B.length)
	{
		C.data[k++] = B.data[j++];
	}
	C.length = k;
	return true;
}

在数组中A[m+n]中存放两个线性表a和b,a长度为m,b长度为n。将两者位置互换。

int *Exchange(int a[], int m, int n)
{
	int temp[999];
	int i = 0,k=0;
	for (i=m+1; i < n; i++)
	{
		temp[k] = a[i];
	}
	for (i = 0; i < m; i++)
	{
		temp[k] = a[i];
	}
	return temp;
}

二分法

while(low<=high)
{
	mid=(low+high)/2;
	if(A[mid==x]) break;
	else if(mid<x) low=mid+1;
	else high=mid-1;
}
posted @ 2024-01-22 20:12  魔法少女小胖  阅读(8)  评论(0编辑  收藏  举报