1,利用顺序表对数据进行降重
头文件
#ifndef __LIST__
#define __LIST__
typedef int intdata;
#define N 50
typedef struct
{
int pos;
intdata data[N];
}stdata;
stdata* cetan_list();
void select_list(stdata *pstr,int index,intdata mun);
void shuchu_list(stdata *pstr);
void rm_list(stdata *pstr,int index);
void jc_list(stdata *pstr);
#endif
函数
#include <stdio.h>
#include "./jiangchong_list.h"
#include <stdlib.h>
/*
* function: //创建顺序表
* @param [ in]
* @param [out]
* @return
*/
stdata* cetan_list()
{
stdata *str=(stdata*)malloc(sizeof(stdata));
if(str==NULL)
{
printf("顺序表创建失败\n");
return (stdata*)-1;
}
// str->data[0]=NULL;
str->pos=0;
return str;
}
/*
* function: //按位置插入数据
* @param [ in]
* @param [out]
* @return
*/
void select_list(stdata *pstr,int index,intdata mun)
{
if(pstr->pos>=N)
{
printf("顺序表满了,无法插入\n");
return;
}
if(index<0||index>pstr->pos)
{
printf("插入位置非法\n");
return;
}
for(int i=pstr->pos;i>index;i--)
{
pstr->data[i]=pstr->data[i-1];
}
pstr->data[index]=mun;
pstr->pos++;
return;
}
/*
* function: //按位置删除
* @param [ in]
* @param [out]
* @return
*/
void rm_list(stdata *pstr,int index)
{
if(pstr->pos<0)
{
printf("顺序为null,无法删除\n");
return;
}
if(index<0||index>pstr->pos)
{
printf("删除位置非法\n");
return;
}
for(int i=index;i<pstr->pos-1;i++)
{
pstr->data[i]=pstr->data[i+1];
}
pstr->pos--;
return;
}
/*
* function: //降重
* @param [ in]
* @param [out]
* @return
*/
void jc_list(stdata *pstr)
{
int j;
for(int i=0;i<pstr->pos;i++)
{
for(j=i+1;j<pstr->pos;j++)
{
if(pstr->data[i]==pstr->data[j])
{
rm_list(pstr,j);
j--;
}
}
}
printf("降重的数据为\n");
return;
}
/*
* function: //遍历顺序表
* @param [ in]
* @param [out]
* @return
*/
void shuchu_list(stdata *pstr)
{
for(int i=0;i<pstr->pos;i++)
{
printf("%4d",pstr->data[i]);
}putchar(10);
return;
}
main函数
#include <stdio.h>
#include "./jiangchong_list.h"
int main(int argc, const char *argv[])
{
stdata *pstr=cetan_list();
select_list(pstr,0,1);
select_list(pstr,1,2);
select_list(pstr,2,5);
select_list(pstr,3,1);
select_list(pstr,4,1);
select_list(pstr,5,3);
select_list(pstr,6,4);
select_list(pstr,7,2);
select_list(pstr,8,4);
select_list(pstr,9,1);
select_list(pstr,7,2);
select_list(pstr,8,4);
select_list(pstr,9,1);
//shuchu_list(pstr);
//rm_list(pstr,1);
shuchu_list(pstr);
jc_list(pstr);
shuchu_list(pstr);
return 0;
}
通过makefile实现
运行结果
2,链表的增删改查及遍历
头文件
#ifndef _LK_LKLIST__
#define _LK_LKLIST__
typedef int datatype;
typedef struct s
{
union{
int len;
datatype mun;
}msg;
struct s* next1;
}datahead;
datahead* cetan_lklist();
void select_lklist(datahead *datahead,datatype a);
void shuchu_lklist();
void tail_lklist(datahead *head,datatype b);
datatype rmhead_lklist(datahead *head);
datatype rmtail_lklist(datahead *head);
void add_list(datahead *head,int index,datatype c);
datatype rmweiz_lklist(datahead *head,int index);
#endif
链表的增删改查函数
#include <stdio.h>
#include "./link_list.h"
#include <stdlib.h>
/*
* function: 创建链表
* @param [ in]
* @param [out]
* @return
*/
datahead* cetan_lklist()
{
datahead *head= (datahead*)malloc(sizeof(datahead));
if(head==NULL)
{
printf("头结点创建失败\n");
return (datahead*)-1;
}
head->msg.len=0;
head->next1=NULL;
return head;
}
/*
* function: 头插法插入数据
* @param [ in]
* @param [out]
* @return
*/
void select_lklist(datahead *head,datatype a)
{
//创建新的结点
datahead *temp=(datahead *)malloc(sizeof(datahead));
if(temp==NULL)
{
printf("新的结点创建失败\n");
return;
}
//给新的结点赋值
temp->msg.mun=a;
temp->next1=NULL;
//执行插入操作
temp->next1=head->next1;
head->next1=temp;
//链表长度自增
head->msg.len++;
}
/*
* function: 尾插法插入数据
* @param [ in]
* @param [out]
* @return
*/
void tail_lklist(datahead *head,datatype b)
{
//创建新的结点
datahead *temp=(datahead*)malloc(sizeof(datahead));
if(temp==NULL)
{
printf("新的结点创建失败\n");
return;
}
temp->msg.mun=b;
temp->next1=NULL;
//遍历找到尾结点
datahead *p=head;
while(p->next1!=NULL)
{
p=p->next1;
}
//插入数据
p->next1=temp;
temp->next1=NULL;
//长度自增
head->msg.len++;
}
/*
* function: 头删法
* @param [ in]
* @param [out]
* @return
*/
datatype rmhead_lklist(datahead *head)
{
//判空
if(head->next1==NULL||head==NULL)
{
printf("链表为空,头结点为空,无法删除\n");
return (datatype)-1;
}
//备份要删除的数据
datahead *temp=head->next1;
//删除
head->next1=head->next1->next1; // head->next1=temp->next1;
//返回的数据---删除的数据
datatype s=temp->msg.mun;
//释放
free(temp);
//长度自减
head->msg.len--;
return s;
}
/*
* function: 尾删法
* @param [ in]
* @param [out]
* @return
*/
datatype rmtail_lklist(datahead *head)
{
//判空
if(head->next1==NULL||head==NULL)
{
printf("链表为空,头结点为空,无法删除\n");
return (datatype)-1;
}
//遍历找到尾结点
datahead *p=head;
datatype s=0;
while(p->next1->next1!=NULL)
{
p=p->next1;
}
//备份要删除的数据
datahead *temp=p->next1;
//删除
p->next1=temp->next1;
//返回的数据---删除的数据
s=temp->msg.mun;
free(temp);
//长度自减
head->msg.len--;
return s;
}
/*
* function: 按位置插入
* @param [ in]
* @param [out]
* @return
*/
void add_list(datahead *head,int index,datatype c)
{
datahead *temp=(datahead*)malloc(sizeof(datahead));
if(temp==NULL)
{
printf("头结点创建失败\n");
return;
}
temp->msg.mun=c;
temp->next1=NULL;
if(index<0||index>head->msg.len)
{
printf("插入位置非法\n");
return;
}
//遍历链表,找到index的位置
datahead *p=head;
for(int i=0;i<index;i++)
{
p=p->next1;
}
//插入数据
temp->next1=p->next1;
p->next1=temp;
//链表长度自增
head->msg.len++;
return;
}
/*
* function: 按位置删除
* @param [ in]
* @param [out]
* @return
*/
datatype rmweiz_lklist(datahead *head,int index)
{
//判空
if(head->next1==NULL||head==NULL)
{
printf("链表为空,头结点为空,无法删除\n");
return (datatype)-1;
}
datahead *p=head;
for(int i=0;i<index-1;i++)
{
p=p->next1;
}
//备份要删除的数据
datahead *temp=p->next1;
//删除
p->next1=temp->next1;
free(temp);
//删除的数据
datatype d=temp->msg.mun;
//链表长度自增
head->msg.len++;
return d;
}
/*
* function: //遍历链表
* @param [ in]
* @param [out]
* @return
*/
void shuchu_lklist(datahead *head)
{
while(head->next1!=NULL)
{
head=head->next1;
printf("%-3d",head->msg.mun);
}putchar(10);
return;
}
链表main函数
#include <stdio.h>
#include "./link_list.h"
int main(int argc, const char *argv[])
{
datahead *head=cetan_lklist();
//头插法插入数据
select_lklist(head,10);
select_lklist(head,20);
select_lklist(head,30);
select_lklist(head,40);
select_lklist(head,50);
shuchu_lklist(head);
//尾插法插入数据
tail_lklist(head,60);
tail_lklist(head,70);
tail_lklist(head,80);
shuchu_lklist(head);
//头删法
datatype len1;
len1=rmhead_lklist(head);
printf("tou删除的数据为:%d\n",len1);
shuchu_lklist(head);
len1=rmhead_lklist(head);
printf("tou删除的数据为:%d\n",len1);
shuchu_lklist(head);
//尾删法
datatype len2;
len2=rmtail_lklist(head);
printf("wei删除的数据为:%d\n",len2);
shuchu_lklist(head);
//按位置插入
printf("按位置插入:\n");
add_list(head,0,11);
shuchu_lklist(head);
add_list(head,6,22);
shuchu_lklist(head);
//按位置删除
printf("按位置删除:\n");
rmweiz_lklist(head,0);
shuchu_lklist(head);
rmweiz_lklist(head,6);
shuchu_lklist(head);
return 0;
}
通过makefile实现
运行结果