//头文件:
#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;
}
数据结构-day3
最新推荐文章于 2024-11-17 20:44:56 发布