数据结构-day3

//头文件:
#ifndef _HEAD_H_
#define _HEAD_H_
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
enum num
{
FALSE=-1,
SUCCESS
};
 
#define MAXSIZE 10
typedef int datatype;
typedef struct SQLIST
{
	datatype data[MAXSIZE];
	int len;
}sqlist;
 
int insert_rear(datatype element,sqlist *list);
int full(sqlist *list);
sqlist* create();
int output(sqlist *list);
int delete_rear(sqlist *list);
void search_index(sqlist*list,int index);
void change(sqlist*list,int index,int element);
void insert_front(sqlist*list,int index,int element);
void delete(sqlist*list,int index);
int find(sqlist*list,int element);
void irrepeat(sqlist*list);
void rank(sqlist*list);
sqlist*free_space(sqlist*list);
#endif
//main函数:
#include "head.h"
int main(int argc, const char *argv[])
{
	//创建顺序表
	sqlist *list=create();
	//循环调用尾插函数
	int n;
	datatype element;
	printf("please enter n:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("please enter %d inster element:",i+1);
		scanf("%d",&element);
		int flag=insert_rear(element,list);
		if(flag==FALSE)
		{
			puts("NULL or FULL!");
			break;
		}
	}
	output(list);
	//删除最后一个下标的数
	delete_rear(list);
	printf("After deleting the last number:\n");
	output(list);
	//按任意下标查找对应的数
	int index;
	printf("please enter the search index:");
	scanf("%d",&index);
	search_index(list,index);
	//根据输入的下标改变对应的值
	printf("please enter the change index:");
	scanf("%d",&index);	
	
	printf("please enter the change num:");
	scanf("%d",&element);//输入对应的值
	change(list,index,element);
	output(list);
	//任意的位置前插
	printf("please enter the insert front index:");
	scanf("%d",&index);
	printf("please enter the font insert num:");
	scanf("%d",&element);
 
	insert_front(list,index,element);
	output(list);
	//任意位置删除
	printf("please enter the delete index:");
	scanf("%d",&index);
	delete(list,index);
	output(list);
	//按任意元素查找
	printf("please enter the num what you find:");
	scanf("%d",&element);
	index=find(list,element);
	printf("The index of you finding is %d\n",index);
	//循环去重
	
	irrepeat(list);
	output(list);
	//顺序表按任意元素修改
	printf("please enter the element what you want to amend:");
	scanf("%d",&element);
	index=find(list,element);
	printf("please enter new num:");
	scanf("%d",&element);
	change(list,index,element);
	output(list);
	//顺序表按任意元素删除
	printf("please enter the element you want to delete:");
	scanf("%d",&element);
	index=find(list,element);
	delete(list,index);
	output(list);
	//顺序表排序
	rank(list);
	printf("The modified list is:\n");
	output(list);
	//释放顺序表内存
	list=free_space(list);
	list=NULL;
 
	return 0;
}
//自定义函数:
#include "head.h" 
sqlist* create()
{
	sqlist *list=(sqlist *)malloc(sizeof(sqlist));
	if(NULL==list)
		return NULL;
	//成功返回地址
	//对数据元素清0
	memset(list->data,0,sizeof(list->data));
	list->len=0;
	return list;
}
int full(sqlist *list)
{
	return list->len==MAXSIZE?FALSE:SUCCESS;
}
int insert_rear(datatype element,sqlist *list)
{
	//1、判断顺序表是否创建成功
	//2、判断顺序表是否为满
	if(NULL==list || full(list))
		return FALSE;
	list->data[list->len++]=element;
	return SUCCESS;
}
int empty(sqlist*list)
{
	return list->len==0?FALSE:SUCCESS;
}
 
int output(sqlist *list)
{
	if(NULL==list || empty(list))
	{
		return FALSE;
	}
	for(int i=0;i<list->len;i++)
	{
		printf("%-5d",list->data[i]);
	}
	puts("");
	
}
int delete_rear(sqlist *list)
{
	if(NULL==list || empty(list))
		return FALSE;
	list->len--;
	return SUCCESS;
}
void search_index(sqlist*list,int index)
{
	//1、判断顺序表是否创建成功
	//2、判断顺序表是否为空
	//3、判断下标是否合法
	if(NULL==list || empty(list)||index<0||index>=list->len)
		return;
	printf("search by index element is %d\n",list->data[index]);
 
}
void change(sqlist*list,int index,int element)
{
	if(NULL==list||empty(list)||index<0||index>=list->len)
	{
		puts("ERROR!");
		return;
	}
 
	list->data[index]=element;
}
void insert_front(sqlist*list,int index,int element)
{
		if(NULL==list||full(list)||index<0||index>=list->len)
	{
		puts("ERROR!");
		return;
	}
	for(int i=list->len-1;i>=index;i--)
	{
		list->data[i+1]=list->data[i];
	}
	list->data[index]=element;
	list->len++;
}
void delete(sqlist*list,int index)
{
	if(NULL==list||empty(list)||index<0||index>=list->len)
	{
		puts("ERROR!");
		return;
	}
	for(int i=index+1;i<list->len;i++)
	{
		list->data[i-1]=list->data[i];
	}
	list->len--;
}
int find(sqlist*list,int element)
{
	if(NULL==list||empty(list))
	{
		puts("ERROR!");
		return -1;
	}
	for(int i=0;i<list->len;i++)
	{
		if(element==list->data[i])
			return i;
	}
	return -1;
	
}
void irrepeat(sqlist*list)
{
	for(int i=0;i<list->len;i++)
	{
		for(int j=i+1;j<list->len;j++)
		{
			if(list->data[i]==list->data[j])
			{
				delete(list,j);
				j--;
			}
		}
	}
 
}
void rank(sqlist*list)
{
	for(int i=0;i<list->len;i++)
	{
		for(int j=0;j<(list->len)-i;j++)
		{
			if(list->data[j]>list->data[j+1])
			{
				int t=list->data[j];
				list->data[j]=list->data[j+1];
				list->data[j+1]=t;
			}
		}
	}
}
sqlist*free_space(sqlist*list)
{
	if(NULL==list)
		return NULL;
	free(list);
	return list;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值