SeqList.c
#include "./SeqList.h"
#include <stdio.h>
#include <stdlib.h>
//创建顺序表
SeqList* create_seqList()
{
SeqList *list = (SeqList*)malloc(sizeof(SeqList));
if(NULL == list)
{
return NULL;
}
list -> pos = 0;//初始化要操作的位置为0
return list;
}
//插入数据
void insert_SeqList(SeqList* list,datatype num)
{
//判断顺序表是否为满
if(list->pos >= N)
{
printf("%d顺序表为满,插入失败!\n",num);
return;
}
list->data[list->pos] = num;
(list->pos)++;
printf("%d插入成功!\n",num);
return;
}
//遍历顺序表
void show_SeqList(SeqList* list)
{
for(int i=0;list->pos>i;i++)
{
printf("%d ",list->data[i]);
}
printf("\n");
return;
}
//从尾部删除
datatype del_SeqList(SeqList *list)
{
int num;
if(0 == list->pos)
{
printf("顺序表为空!\n");
return 0;
}
(list->pos)--;
num = list->data[list->pos];
return num;
}
//按位插入
void insert_location_SeqList(SeqList* list,datatype index,datatype num)
{
if(list->pos >= N)
{
printf("顺序表为满,插入失败!\n");
return;
}
if(index<0 || index>list->pos)
{
printf("index=%d属于非法插入!\n",index);
return;
}
int i=0;
for( i=list->pos;i>index;i--)
{
list->data[i] = list->data[i-1];
}
list->data[i] = num;
(list->pos)++;
printf("%d成功插入到下标为%d的位置!\n",num,index);
return;
}
//按位删除
datatype del_location_SeqList(SeqList* list,datatype index)
{
int i;
if(0 == list->pos)
{
printf("顺序表为空!\n");
return 0;
}
for( i=index;i<(list->pos)-1;i++)
{
list->data[i] = list->data[i+1];
}
(list->pos)--;
printf("下标为%d的数据删除成功!\n",index);
return index;
}
//按位修改
void rm_location_SeqList(SeqList* list,datatype index,datatype num)
{
if(index<0 || index>list->pos)
{
printf("index=%d属于非法修改!\n",index);
return;
}
list->data[index] = num;
printf("下标为%d的数据修改成功!\n",index);
return;
}
//按位查找
void find_location_SeqList(SeqList* list,datatype index)
{
if(0 == list->pos)
{
printf("顺序表为空!\n");
return;
}
printf("下标为%d是数据是%d\n",index,list->data[index]);
return;
}
//按数据修改
void update_SeqList(SeqList* list,datatype oldNum,datatype newNum)
{
int i=0;
for(i=0;i<list->pos;i++)
{
if(list->data[i] == oldNum)
{
list->data[i] == newNum;
}
}
printf("数据修改完成!\n");
return;
}
//按数据查找
int select_SelqList(SeqList* list,datatype num)
{
int i=0;
for(i=0;i<list->pos;i++)
{
if(list->data[i] == num)
{
return i;
}
}
return -1;
}
//顺序表去重
//1221134324534541
//12345
void delrepeat_SeqList(SeqList* list)
{
if(0 == list->pos)
{
printf("顺序表为空!\n");
return ;
}
int i,j;
for(i=0;i<(list->pos);i++)
{
for(j=i+1;j<(list->pos);j++)
{
if(list->data[i] == list->data[j])
{
del_location_SeqList(list,j);
j--;
}
}
}
printf("去重成功!\n");
return;
}
//释放顺序表
void free_SeqList(SeqList* list)
{
free(list);
list = NULL;
printf("释放成功!");
return;
}
mian.c
#include <stdio.h>
#include "./SeqList.h"
int main(int argc, const char *argv[])
{
SeqList * list = create_seqList();
/*
insert_SeqList(list,10);
insert_SeqList(list,100);
insert_SeqList(list,1000);
insert_SeqList(list,10000);
insert_SeqList(list,100000);
show_SeqList(list);
del_SeqList(list);
show_SeqList(list);
del_SeqList(list);
show_SeqList(list);
del_SeqList(list);
show_SeqList(list);
del_SeqList(list);
show_SeqList(list);
del_SeqList(list);
show_SeqList(list);
insert_location_SeqList(list,2,222);
show_SeqList(list);
del_location_SeqList(list,2);
show_SeqList(list);
rm_location_SeqList(list,2,222);
show_SeqList(list);
find_location_SeqList(list,2);
update_SeqList(list, 11, 111);
show_seqList(list);
int index = select_SelqList(list, 2222);
printf("index=%d\n", index);
*/
insert_SeqList(list,1);
insert_SeqList(list,2);
insert_SeqList(list,2);
insert_SeqList(list,1);
insert_SeqList(list,1);
insert_SeqList(list,3);
insert_SeqList(list,4);
insert_SeqList(list,3);
insert_SeqList(list,2);
insert_SeqList(list,4);
insert_SeqList(list,5);
insert_SeqList(list,3);
insert_SeqList(list,4);
insert_SeqList(list,5);
insert_SeqList(list,4);
insert_SeqList(list,1);
delrepeat_SeqList(list);
show_SeqList(list);
free_SeqList(list);
return 0;
}
SeqList.h
#ifndef __01_H__
#define __01_H__
#define N 17 //顺序表容量
typedef int datatype;
typedef struct
{
datatype data[N];
int pos;
}SeqList;
SeqList* create_seqList();
void insert_SeqList(SeqList* list,datatype num);
void show_SeqList(SeqList* list);
datatype del_SeqList(SeqList *list);
void insert_location_SeqList(SeqList* list,datatype index,datatype num);
datatype del_location_SeqList(SeqList* list,datatype index);
void rm_location_SeqList(SeqList* list,datatype index,datatype num);
void find_location_SeqList(SeqList* list,datatype index);
void update_SeqList(SeqList* list,datatype oldNum,datatype newNum);
int select_SelqList(SeqList* list,datatype num);
void delrepeat_SeqList(SeqList* list);
void free_SeqList(SeqList* list);
#endif
makefile.cfg
Target:=a.out
obj:=SeqList.o main.o
CAN:=-c -o
CC:=gcc
makefile
-include ./makefile.cfg
$(Target):$(obj)
$(CC) $^ -o $@
%.o:%.c
$(CC) $< $(CAN) $@
.PHONY:clean
clean:
rm $(obj) $(Target)