1、顺序表按任意元素修改
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include
#include
#include
#define MAXSIZE 7
typedef int datatype;
enum num
{
FALSE=-1,
SUCCESS
};
typedef struct SQLIST
{
//数据元素
datatype data[MAXSIZE];
//顺序表长度
int len;
}sqlist;
sqlist* create();
int insert_rear(datatype element,sqlist*list);
int output(sqlist *list);
int key_element(sqlist*list,datatype key);
void cpy_index(sqlist*list,int index,datatype element1);
#endif
//主函数
#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
//文本函数
#include"head.h"
sqlist* create()
{
sqlist *list=(sqlist*)malloc(sizeof(sqlist));
if(NULL==list)
return NULL;
//对数据元素清0
memset(list->data,0,sizeof(list->data));
//对顺序表长度清0
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;ilen;i++)
{
printf("%-5d",list->data[i]);
}
puts("");
return SUCCESS;
}
int key_element(sqlist*list,datatype key)
{
//1、判断顺序表是否创建成功
//2、判断顺序表是否为空
if(NULL==list||empty(list))
return FALSE;
//3、循环一 一查找
//如果key重复 则返回最后一个key下表
int index=-1;
for(int i=0;ilen;i++)
{
if(key==list->data[i])
{
index=i;
}
}
return index;
}
void cpy_index(sqlist*list,int index,datatype element1)
{
if(NULL==list||empty(list)||index<0||index>=list->len)
{
puts("error");
return;
}
list->data[index]=element1;
}
2、顺序表按任意元素删除
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include
#include
#include
#define MAXSIZE 7
typedef int datatype;
enum num
{
FALSE=-1,
SUCCESS
};
typedef struct SQLIST
{
//数据元素
datatype data[MAXSIZE];
//顺序表长度
int len;
}sqlist;
sqlist* create();
int insert_rear(datatype element,sqlist*list);
int output(sqlist *list);
int key_element(sqlist*list,datatype key);
void det_index(sqlist*list,int index);
#endif
//主函数
#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
//文本函数
#include"head.h"
sqlist* create()
{
sqlist *list=(sqlist*)malloc(sizeof(sqlist));
if(NULL==list)
return NULL;
//对数据元素清0
memset(list->data,0,sizeof(list->data));
//对顺序表长度清0
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;ilen;i++)
{
printf("%-5d",list->data[i]);
}
puts("");
return SUCCESS;
}
int key_element(sqlist*list,datatype key)
{
//1、判断顺序表是否创建成功
//2、判断顺序表是否为空
if(NULL==list||empty(list))
return FALSE;
//3、循环一 一查找
//如果key重复 则返回最后一个key下表
int index=-1;
for(int i=0;ilen;i++)
{
if(key==list->data[i])
{
index=i;
}
}
return index;
}
void det_index(sqlist*list,int index)
{
//1、判断顺序表是否创建成功
//2、判断顺序表是否为空
//3、判断下表是否合法
if(NULL==list||empty(list)||index<0||index>list->len)
{
puts("error");
return;
}
//4、删除
for(int i=index+1;ilen;i++)
{
list->data[i-1]=list->data[i];
}
list->len--;
}
3、顺序表排序
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include
#include
#include
#define MAXSIZE 7
typedef int datatype;
enum num
{
FALSE=-1,
SUCCESS
};
typedef struct SQLIST
{
//数据元素
datatype data[MAXSIZE];
//顺序表长度
int len;
}sqlist;
sqlist* create();
int insert_rear(datatype element,sqlist*list);
int output(sqlist *list);
int sort(sqlist*list);
#endif
//主函数
#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
//文本函数
#include"head.h"
sqlist* create()
{
sqlist *list=(sqlist*)malloc(sizeof(sqlist));
if(NULL==list)
return NULL;
//对数据元素清0
memset(list->data,0,sizeof(list->data));
//对顺序表长度清0
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;ilen;i++)
{
printf("%-5d",list->data[i]);
}
puts("");
return SUCCESS;
}
int sort(sqlist*list)
{
if(NULL==list||empty(list))
return FALSE;
for(int i=1;ilen;i++)
{
for(int j=0;jlen-i;j++)
{
if(list->data[j]>list->data[j+1])
{
datatype t=list->data[j];
list->data[j]=list->data[j+1];
list->data[j+1]=t;
}
}
}
return SUCCESS;
}
4、顺序表释放空间
//头文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include
#include
#include
#define MAXSIZE 7
typedef int datatype;
enum num
{
FALSE=-1,
SUCCESS
};
typedef struct SQLIST
{
//数据元素
datatype data[MAXSIZE];
//顺序表长度
int len;
}sqlist;
sqlist* create();
int insert_rear(datatype element,sqlist*list);
int output(sqlist *list);
sqlist *free_space(sqlist*list);
#endif
//主函数
#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
//文本函数
#include"head.h"
sqlist* create()
{
sqlist *list=(sqlist*)malloc(sizeof(sqlist));
if(NULL==list)
return NULL;
//对数据元素清0
memset(list->data,0,sizeof(list->data));
//对顺序表长度清0
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;ilen;i++)
{
printf("%-5d",list->data[i]);
}
puts("");
return SUCCESS;
}
sqlist *free_space(sqlist*list)
{
if(NULL==list)
return NULL;
free(list);
list=NULL;
puts("");
return list;
}
5、思维导图