11.23整理

1头文件

#ifndef __HEAD_H__
#define __HEAD_H__
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define MAXSIZE 10

typedef int data_type;


typedef struct list{

	data_type data[MAXSIZE];
	int len;

}sqlist;

sqlist *creat_space(void);
int insert_rear(sqlist *p,data_type element);
int full_sqlist(sqlist *p);
int  output(sqlist *p);
int empty_sqlist(sqlist *p);
int delete_rear(sqlist *p); 
void chazhao(sqlist *p,int index);
int  xiugai(sqlist *p,int xiugai_index);
int  charu(sqlist *p,int charu_index);
int shanchu(sqlist *p,int del_index);
int key_find(sqlist *p);
int delete_bykey(sqlist *p);
int change_bykey(sqlist *p); 
int remove_same(sqlist *p);
int sort(sqlist *p);

void combin(sqlist *a,sqlist *b,sqlist *c,int m,int n);
	
sqlist *pfree(sqlist *p);



#endif

2函数部分

#include "head.h"

sqlist *creat_space()
{
	sqlist *p=(sqlist *)malloc(sizeof(sqlist));
	if(NULL==p)
		return NULL;
	memset(p->data,0,sizeof(p->data));
	p->len=0;
	return p;

}


int insert_rear(sqlist *p,data_type element)
{
	if(NULL==p||full_sqlist(p))
		return -1;
	p->data[p->len++]=element;

return 0;
}


int full_sqlist(sqlist *p)
{
	if(p->len>=MAXSIZE)
		return -1;
	else return 0;
}


int  output(sqlist *p)
{
	if(p==NULL||empty_sqlist(p))
		return -1;
	int i=0;
	for(i=0;ilen;i++)
	printf("%d ",p->data[i]);
	printf("\n");

return 0;
}



int empty_sqlist(sqlist *p)
{
	if(p->len==0)
		return -1;

return 0;
}


int delete_rear(sqlist *p)
{
	if(NULL==p||empty_sqlist(p))
		return -1;
	p->len--;
	return 0;
}


void chazhao(sqlist *p,int index)
{
	if(NULL==p||empty_sqlist(p)||index<0||index>=p->len)
		printf("error\n");
	printf("下标%d对应值%d\n",index,p->data[index]);

}



int  xiugai(sqlist *p,int xiugai_index)
{
	if(NULL==p||empty_sqlist(p)||xiugai_index<0||xiugai_index>p->len)
		return -1;
	printf("修改为:");
	int i=0;int op_flag;
	scanf("%d",&i);
	p->data[xiugai_index]=i;

return 0;
}



int  charu(sqlist *p,int charu_index)
{
	if(NULL==p||full_sqlist(p)||charu_index<0||charu_index>p->len)
		return -1;
	int m;
	data_type i;
	printf("插入值:");
	scanf("%d",&m);
	for(i=p->len-1;i>=charu_index;i--)
		p->data[i+1]=p->data[i];
	p->data[charu_index]=m;
	p->len++;

#if 0
	int op_flag;
	op_flag=output(p);
	if(op_flag==-1)
	printf("empty\n");

#endif

#if 0
	for(i=0;ilen+1;i++)
		printf("%d ",p->data[i]);
	printf("\n");
#endif

return 0;
}

int shanchu(sqlist *p, int del_index)
{
	if(NULL==p||empty_sqlist(p)||del_index<0||del_index>=p->len)
		return -1;
	int i;
	for(i=del_index;ilen-1;i++)
		p->data[i]=p->data[i+1];
	p->len--;



return 0;
}

int key_find(sqlist *p)
{
#if 0
	if(NULL==p||empty_sqlist(p))
		return NULL;

	int key,i;
	int j=0;
	int key_index[p->len];
	int *q=key_index;

	printf("查找值:");
	scanf("%d",&key);

	for(i=0;ilen;i++)
	{
		if(key==p->data[i])
		{
			key_index[j++]=i;		
		}

	}
return q;
#endif

#if 1

	if(NULL==p||empty_sqlist(p))
		return -1;
	int key;
	printf("input key:");
	scanf("%d",&key);
	int i;int m;
	for(i=0;ilen;i++)
	{
		if(key==p->data[i])	
			m=i;
	}
return m;
#endif
}

int delete_bykey(sqlist *p)
{
	
	int key_index;
	key_index=key_find(p);
	if(key_index==-1)
		return -1;
	int sc_flag=shanchu(p,key_index);	
	if(sc_flag==-1)
		return -1;
return 0;
}

int  change_bykey(sqlist *p)
{
	
	int key_index;
	key_index=key_find(p);
	if(key_index==-1)
		return -1;
	int xg_flag=xiugai(p,key_index);
	if(xg_flag==-1)
		return -1;

return 0;
}



int remove_same(sqlist *p)
{
	
	int key_index;
	key_index=key_find(p);
	if(key_index==-1)
		return -1;
	int i=0;
	for(i=0;ilen;i++)
	{
		if(p->data[key_index]==p->data[i])
		{
			 shanchu(p,i);
			 i--;
		}
	}

return 0;
}

int sort(sqlist *p)
{
	if(p==NULL||empty_sqlist(p))
		return -1;
	int i=0,j=0;
	data_type t,max;
	for(i=0;ilen-1;i++)
	{
		for(j=0;jlen-1-i;j++)
			if(p->data[j]>p->data[j+1])
			{
				t=p->data[j];
				p->data[j]=p->data[j+1];
				p->data[j+1]=t;
			}
	
	}

return 0;
}
void combin(sqlist *a,sqlist *b,sqlist *c,int m,int n)
{
	int a_index=0,b_index=0,c_index=0;
	while(a_indexdata[a_index]data[b_index])
		c->data[c_index++]=a->data[a_index++];
	else 
		c->data[c_index++]=b->data[b_index++];
}

while(a_indexdata[c_index++]=a->data[a_index++];
while(b_indexdata[c_index++]=b->data[b_index++];


}

sqlist *pfree(sqlist *p)
{
	if(p!=NULL)
		 return NULL;
	free(p);
	p=NULL;
	return p;
}

有序合并

11.23整理_第1张图片

排序

11.23整理_第2张图片

去重

11.23整理_第3张图片

按值修改

11.23整理_第4张图片

按下标删除

11.23整理_第5张图片

你可能感兴趣的:(算法)