Data structures implemented >>> >> sequence table are sequentially stored.

Order of the array:

  • Structure Description
  • initialization
  • Insert \ Delete
  • Invert
  • Seek
  • Sort (bubble)

Here is a concrete implementation:

  1. Structure Description:
typedef struct{
	int *felem;//第一个元素地址
	int len;//数组的最大容纳量
	int cnt;//当前数组有效元素个
}sqlist;

2. Initialization:

void init_sqlist(sqlist *L ,int length)
{
	L->felem =(int *)malloc(sizeof(int)*length);
	if(L->felem==NULL)
	{
		cout<<"动态内存分配失败!"<<endl;
		exit(-1);
	}
	else
	{
		L->len =length;
		L->cnt =0;
	}
	return ;
}

3. Insert / Delete:

bool insert_sqlist(sqlist *L,int pos,int val)
{
	int i;
	cout<<"请输入需要插入元素的位置和值"<<endl;
	cin>>pos>>val;
	if(is_full(L))
		return false;
	if(pos<1||pos>L->cnt +1)
		return false;
	for(i=L->cnt -1;i>=pos-1;--i)
		L->felem [i+1]=L->felem [i];
	L->felem [pos-1]=val;
	L->cnt ++;
	return true;
}

bool delete_sqlist(sqlist *L,int pos,int *pval)
{
	//bool is_empty(sqlist *L);
	int i;
	if(is_empt(L))
		return false;
	if(pos<1||pos>L->cnt)
		return false;
	*pval=L->felem [pos-1];
	for(i=pos;i<L->cnt ;++i)
		L->felem [i-1]=L->felem [i];
	L->cnt--;
	return true;
}

4. Invert:

void inversion_sqlist(sqlist *L)
{
	int i=0;
	int j=L->cnt -1;
	int t;
	while(i<j)
	{
		t=L->felem [i];
		L->felem [i]=L->felem [j];
		L->felem [j]=t;
		++i;
		--j;
	}
	return ;
}

5. Look for:

int locate_sqlist(sqlist L,int e)
{
	int i=1;
	while(i<=L.len &&L.felem [i-1]!=e)
		i++;
	if(i<=L.len )
		return i;
	else
		return 0;
}

6. Sorting (bubbling)

void sort_sqlist(sqlist*L)
{
	int i,j,t;
	for(i=0;i<L->cnt ;i++)
	{
		for(j=i+1;j<L->cnt ;++j)
		{
			if(L->felem [i]>L->felem [j])
			{
				t=L->felem [i];
		        L->felem [i]=L->felem [j];
		        L->felem [j]=t;
			}
		}
	}
}

------ "source code:

#include<iostream>
#include<malloc.h>
#include<stdlib.h>
using namespace std;

//1.结构描述:
typedef struct{
	int *felem;//第一个元素地址
	int len;//数组的最大容纳量
	int cnt;//当前数组有效元素个
}sqlist;

//2.初始化:
void init_sqlist(sqlist *L ,int length)
{
	L->felem =(int *)malloc(sizeof(int)*length);
	if(L->felem==NULL)
	{
		cout<<"动态内存分配失败!"<<endl;
		exit(-1);
	}
	else
	{
		L->len =length;
		L->cnt =0;
	}
	return ;
}

//判空:
bool is_empt(sqlist *L)
{
	if(L->cnt==0)
		return true;
	else
		return false;
}

//判满:
bool is_full(sqlist *L)
{
	if(L->cnt==L->len)
		return true;
	else
		return false;
}

//显示:
void show_sqlist(sqlist *L)
{
	if(is_empt(L))
		cout<<"数组为空!"<<endl;
	else
	{
		for(int i=0;i<L->cnt ;++i)
			cout<<L->felem [i]<<'\t';
		cout<<endl;
	}
}

//填充:
bool fill_sqlist(sqlist *L,int val)
{
	if(is_full(L))
		return false;
	L->felem [L->cnt ]=val;
	(L->cnt )++;
	return true;
}

//3.插入/删除:
bool insert_sqlist(sqlist *L,int pos,int val)
{
	int i;
	cout<<"请输入需要插入元素的位置和值"<<endl;
	cin>>pos>>val;
	if(is_full(L))
		return false;
	if(pos<1||pos>L->cnt +1)
		return false;
	for(i=L->cnt -1;i>=pos-1;--i)
		L->felem [i+1]=L->felem [i];
	L->felem [pos-1]=val;
	L->cnt ++;
	return true;
}

bool delete_sqlist(sqlist *L,int pos,int *pval)
{
	//bool is_empty(sqlist *L);
	int i;
	if(is_empt(L))
		return false;
	if(pos<1||pos>L->cnt)
		return false;
	*pval=L->felem [pos-1];
	for(i=pos;i<L->cnt ;++i)
		L->felem [i-1]=L->felem [i];
	L->cnt--;
	return true;
}

//4.倒置:
void inversion_sqlist(sqlist *L)
{
	int i=0;
	int j=L->cnt -1;
	int t;
	while(i<j)
	{
		t=L->felem [i];
		L->felem [i]=L->felem [j];
		L->felem [j]=t;
		++i;
		--j;
	}
	return ;
}

//5.查找:
int locate_sqlist(sqlist L,int e)
{
	int i=1;
	while(i<=L.len &&L.felem [i-1]!=e)
		i++;
	if(i<=L.len )
		return i;
	else
		return 0;
}

//6.排序(冒泡)
void sort_sqlist(sqlist*L)
{
	int i,j,t;
	for(i=0;i<L->cnt ;i++)
	{
		for(j=i+1;j<L->cnt ;++j)
		{
			if(L->felem [i]>L->felem [j])
			{
				t=L->felem [i];
		        L->felem [i]=L->felem [j];
		        L->felem [j]=t;
			}
		}
	}
}

int main()
{

	int val=2,pos,len,x,loc;
	len=10;pos=5;
    sqlist sq;
	init_sqlist(&sq,len);
	show_sqlist(&sq);

	fill_sqlist(&sq,1);
    fill_sqlist(&sq,-3);
	fill_sqlist(&sq,6);
	fill_sqlist(&sq,45);
	fill_sqlist(&sq,13);
	fill_sqlist(&sq,34);
	if(fill_sqlist(&sq,36))
		cout<<"填充成功!"<<endl;
	else
		cout<<"填充失败!"<<endl;
	cout<<"填充之后的数组内容是:"<<endl;
	show_sqlist(&sq);

	if(insert_sqlist(&sq,pos,val))
	{
		cout<<"插入成功!"<<endl;
		cout<<"插入的元素是第"<<pos<<"个元素"<<endl;
		cout<<"插入的元素是:"<<val;
	}
	else
		cout<<"插入失败!"<<endl;
	cout<<"插入之后的数组内容是:"<<endl;
	show_sqlist(&sq);

	if(delete_sqlist(&sq,pos,&val))
	{
		cout<<"删除成功!"<<endl;
		cout<<"删除的元素是第"<<pos<<"个元素"<<endl;
		cout<<"删除的元素是:"<<val<<endl;
	}
	else
		cout<<"删除失败!"<<endl;
	cout<<"删除之后的数组内容是:"<<endl;
	show_sqlist(&sq);


	inversion_sqlist(&sq);
	cout<<"倒置之后的数组内容是:"<<endl;
	show_sqlist(&sq);


	cout<<"请输入需要查找的元素的值:"<<endl;
	cin>>x;
	loc=locate_sqlist(sq,x);
	if(loc>0)
	{
		cout<<"找到了!"<<endl;
		cout<<x<<"是线性表中第"<<loc<<"个元素"<<endl;
	}
	else
		cout<<"查找失败!"<<endl;

	sort_sqlist(&sq);
	cout<<"排序之后的数组内容是:"<<endl;
	show_sqlist(&sq);
	return 0;
}

*** Note points:
### ambiguous symbol simply, this happens because the program uses the namespace already have a variable name, replace the variable name it, choose a less pit father 2 variable names, specify a namespace, so the compiler can know in the end is a variable which namespace. (Is_empty () function in c ++ is <iostream>already defined in the library file, so here we are to judge the function named empty is_empt(), otherwise ambiguous.
)

Order table data structure is the most basic structure provides the foundation for the realization of a number of other senior structures and algorithms.

Guess you like

Origin blog.csdn.net/qq_43595030/article/details/90726213