线性表之顺序存储-顺序表

顺序表的操作

顺序表的存储结构

#define maxsize 100        //存储空间的分配量 

//定义顺序表数据类型 
typedef struct{
	int data[maxsize];
	int last;              //存放表中最后一个元素的下标 
}sequenlist;

顺序表的冒泡排序

void list_bubble_sort(sequenlist *p)//max to min
{
    int i,j;
    int temp;
	for(i=0; i< p->last; i++)//attention
	{
		for(j=0; j< p->last-i; j++)
		{
		    if(p->data[j] < p->data[j+1])
	        {
	            temp = p->data[j];
				p->data[j] = p->data[j+1];
				p->data[j+1] = temp;	
			}
	    }
	}	
}

顺序表的删除操作

nt delete_1(sequenlist *s,int del) //删除函数 
{
	int temp;
	for(int i=0; i <= s->last; i++)
	{
		if(del == s->data[i])
		{
			temp = i;
			for(int j=i; j<s->last; j++)
			{
				s->data[j] = s->data[j+1];
			}
			s->last = s->last - 1;
			return 0;//删除第一个与del相同的元素,函数结束 
		}
	}
	//要删的那个元素不在表中 
	printf("the element you want to delete is not in the sequenlist!\n"); 
}

顺序表中元素的查找

int search(sequenlist *s,int key)    //查找函数 
{
    for(int i=0; i<= s->last; i++)
    {
    	if(key == s->data[i])
    	{
    		printf("exist !\n");
    		return 0;
		}
	}
	printf("not found !\n");
	return 0;
}

顺序表的逆置

void reverse(sequenlist *s)//逆置函数 
{
    int i,j;
    int temp;
    int last_temp = s->last;
	for(i=0; i<= s->last/2; i++)
	{
		temp = s->data[i];
        s->data[i] = s->data[last_temp];
		s->data[last_temp] = temp;        
	    last_temp--;
	}
}

删除顺序表中的相同元素

void delete_same(sequenlist *s)//删除表中相同的元素 
{
	int i,j;
	int temp;
	for(i=0; i<=s->last; i++)
	{ 
		for(j=1; j<=s->last; j++)
		{
			if(s->data[j] == s->data[i])//元素相同 
			{
			    for(int k=j; k<s->last; k++)
				{
					s->data[k] = s->data[k+1];
				}
				s->last = s->last - 1; 
			}
		}	
	}
}

向顺序表的指定位置插入元素

int insert(sequenlist *L,int i,int x) //指定位置,插入 
{
	int j;
	if(((*L).last) >= maxsize-1)
	{
		printf("the list is overflow!\n");
		return (0);
	}
	else
	{
		if((i<1)||(i>(*L).last+2))
		{
			printf("position is not correct!\n");
			return (0);
		}
		else
		{
			for(j=(*L).last;j>=i-1;j--)
			{
				(*L).data[j+1]=(*L).data[j];				 
			}
			(*L).last=(*L).last+1;              
			(*L).data[i-1]=x;
			return (0);
		}
	}
}

向顺序表的指定位置插入元素

int insert(sequenlist *L,int i,int x) //指定位置,插入 
{
	int j;
	if(((*L).last) >= maxsize-1)
	{
		printf("the list is overflow!\n");
		return (0);
	}
	else
	{
		if((i<1)||(i>(*L).last+2))
		{
			printf("position is not correct!\n");
			return (0);
		}
		else
		{
			for(j=(*L).last;j>=i-1;j--)
			{
				(*L).data[j+1]=(*L).data[j];				 
			}
			(*L).last=(*L).last+1;              
			(*L).data[i-1]=x;
			return (0);
		}
	}
}

打印顺序表

void print_list(sequenlist *s)   //打印顺序表 
{
	int i;
	for(i=0; i<=s->last; i++)
	{
		printf("%3d",s->data[i]);
	} 
}

试着煲下汤

/*
* author: shansan.top
* date: 2018/12/12
* version: 1.0 
*/

#include<stdio.h>
#define maxsize 100

//定义顺序表数据类型 
typedef struct{
	int data[maxsize];
	int last;
}sequenlist;

int search(sequenlist *s,int key)    //查找函数 
{
    for(int i=0; i<= s->last; i++)
    {
    	if(key == s->data[i])
    	{
    		printf("exist !\n");
    		return 0;
		}
	}
	printf("not found !\n");
	return 0;
}

int delete_1(sequenlist *s,int del) //删除函数 
{
	int temp;
	for(int i=0; i <= s->last; i++)
	{
		if(del == s->data[i])
		{
			temp = i;
			for(int j=i; j<s->last; j++)
			{
				s->data[j] = s->data[j+1];
			}
			s->last = s->last - 1;
			return 0;//删除第一个与del相同的元素,函数结束  
		}
	}
	//要删的那个元素不在表中 
	printf("the element you want to delete is not in the sequenlist!\n"); 
}
 
void print_list(sequenlist *s)   //打印顺序表 
{
	int i;
	for(i=0; i<=s->last; i++)
	{
		printf("%3d",s->data[i]);
	} 
}

void reverse(sequenlist *s)//逆置函数 
{
    int i,j;
    int temp;
    int last_temp = s->last;
	for(i=0; i<= s->last/2; i++)
	{
		temp = s->data[i];
        s->data[i] = s->data[last_temp];
		s->data[last_temp] = temp;        
	    last_temp--;
	}
}

void list_bubble_sort(sequenlist *p)//max to min
{
    int i,j;
    int temp;
	for(i=0; i< p->last; i++)//attention
	{
		for(j=0; j< p->last-i; j++)
		{
		    if(p->data[j] < p->data[j+1])
	        {
	            temp = p->data[j];
				p->data[j] = p->data[j+1];
				p->data[j+1] = temp;	
			}
	    }
	}	
}

void insert_in_order_list(sequenlist *s,int value)//有序表中插入元素 
{
	int i,j;
	int count=0;
	//int temp = s->last+1;
	for(i=0; i<=s->last; i++)
	{
		count++;
		if( value <= s->data[i])
		{
			s->last = s->last + 1;
			for(j=s->last; j>i; j--)
			{
				s->data[j] = s->data[j-1];
			}
			s->data[i] = value;
			return ;//结束函数 
		}
	}
	//printf("i=%d",i);
	//printf("s->last=%d\n",s->last);
	if(i > s->last-1)
	{
		s->last = s->last + 1;
		s->data[s->last] = value;
	}
}

int insert(sequenlist *L,int i,int x) //指定位置,插入 
{
	int j;
	if(((*L).last) >= maxsize-1)
	{
		printf("the list is overflow!\n");
		return (0);
	}
	else
	{
		if((i<1)||(i>(*L).last+2))
		{
			printf("position is not correct!\n");
			return (0);
		}
		else
		{
			for(j=(*L).last;j>=i-1;j--)
			{
				(*L).data[j+1]=(*L).data[j];				 
			}
			(*L).last=(*L).last+1;              
			(*L).data[i-1]=x;
			return (0);
		}
	}
}

void delete_same(sequenlist *s)//删除表中相同的元素 
{
	int i,j;
	int temp;
	for(i=0; i<=s->last; i++)
	{ 
		for(j=1; j<=s->last; j++)
		{
			if(s->data[j] == s->data[i])//元素相同 
			{
			    for(int k=j; k<s->last; k++)
				{
					s->data[k] = s->data[k+1];
				}
				s->last = s->last - 1; 
			}
		}	
	}
}

int main()
{
	sequenlist p={{1,3,2,6,5,4,9,7,8},8};
	//这里有9个数,但数组下表是从0开始的,所以 p.last = 8 
	print_list(&p);
	printf("\n");
	
	//查找
	
	printf("please input a value which you want: ");
	int value;//C++语法可以临时定义一个变量,C语言不可以(需放在开头)。 
	scanf("%d",&value); 
	//search(&p,10);
	search(&p,value);
	print_list(&p);
	printf("\n\n");
	
	//删除表中的指定元素 
	delete_1(&p,8);
	printf("after delete:\n"); 
	print_list(&p);
	
	
	//逆置顺序表 
	printf("\n\nafter reverse:\n");
	reverse(&p);
	print_list(&p);
	
	//冒泡排序 
	printf("\nafter sort:\n");
	printf("\n");
	
	//list_bubble_sort(&try_1);
	list_bubble_sort(&p);
	print_list(&p); 
	
	
	//往有序顺序表中插入一个元素
	printf("\n\n");
    sequenlist try_1 = {{1,2,3,5,6,7},5};
    print_list(&try_1);
	
	printf("\n");
	printf("please input the value that you wan to insert into the sequenlist: ");
	int data;
	scanf("%d",&data);
	insert_in_order_list(&try_1,data);
	//insert_in_order_list(&try_1,9);
	print_list(&try_1);
	
	//删除表中相同的元素
	printf("\n\n");
	sequenlist try_2= {{1,1,2,2,3,3,4,4},7};
	print_list(&try_2);
	printf("\ndelete the same element:\n");
	delete_same(&try_2);
	print_list(&try_2);
	printf("\n");
	
	
	//另一种玩法
	int n;
	int i;
	printf("\nplease input the number of elements: ");
	scanf("%d",&n);
	printf("please input %d values:\n",n); 
	sequenlist try_3;
	try_3.last = n-1;//注意,数组的小标从0开始 
	for(i=0; i<=try_3.last; i++)
	{
		scanf("%d",&try_3.data[i]);
	}
	print_list(&try_3);
	printf("\n\n");
	
	//在指定位置插入 
	insert(&try_3,1,22);
	print_list(&try_3);
	  
    return 0;	
}

程序运行结果

posted @ 2018-12-14 20:57  yeshan333  阅读(225)  评论(0编辑  收藏  举报