1.定义顺序表结构体
2.创建顺序表
3.从尾部插入数据
4.遍历顺序表
5.从尾部删除数据
6.按下标插入数据
7.按下标删除数据
8.按下标修改数据
9.按下标查找数据
10.按数据修改数据
11..按数据查找位置
12.顺序表去重
删除重复数据
(提示:将先出现的数据与后面的数据对比,如果有重复的将后面的数据删除)
提示:可以直接使用按位置删除的函数
顺序表中原有:1 2 2 1 1 3 4 3 2 4 5 3 4 5 4 1
......
顺序表中存储:1 2 3 4 5
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;
}
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);
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);
#endif
makefile.cfg文章来源:https://www.toymoban.com/news/detail-828852.html
Target:=a.out
obj:=SeqList.o main.o
CAN:=-c -o
CC:=gcc
makefile文章来源地址https://www.toymoban.com/news/detail-828852.html
-include ./makefile.cfg
$(Target):$(obj)
$(CC) $^ -o $@
%.o:%.c
$(CC) $< $(CAN) $@
.PHONY:clean
clean:
rm $(obj) $(Target)
到了这里,关于对顺序表的基本操作(增删查改),并编写makefile进行编的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!