2.22作业

1.顺序表(按位置插入、按位置删除和去重、重新写)

//seq_list.c

#include "seq_list.h"
seq_p create_seq()
{
	seq_p L=(seq_p)malloc(sizeof(seq_list));
	if(L==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	L->len=0;
	bzero(L,sizeof(L->data));
	return L;
}

int seq_empty(seq_p L)
{
	if(L==NULL)
	{
		return -1;
	}
	return L->len==0?1:0;
}

int seq_full(seq_p L)
{
	if(L==NULL)
	{
		return -1;
	}
	return L->len==MAX?1:0;
}

void out_seq(seq_p L)
{
	if(L==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(seq_empty(L))
	{
		printf("表为空\n");
		return;
	}
	for(int i=0;i<L->len;i++)
	{
		printf("%d\t",L->data[i]);
	}
	putchar(10);
}

void seq_pos(seq_p L,datatype data,int pos)
{
	if(L==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(seq_full(L))
	{
		printf("表满\n");
		return;
	}
	if(pos>L->len||pos<0)
	{
		printf("位置不合理\n");
		return;
	}
	for(int i=L->len-1;i>=pos;i--)
	{
		L->data[i+1]=L->data[i];
	}
	L->data[pos]=data;
	L->len++;
}

void del_pos(seq_p L,int pos)
{
	if(L==NULL)
	{
		printf("入参为空\n");
		return;
	}
	if(seq_empty(L))
	{
		printf("表空\n");
		return;
	}
	if(pos>=L->len||pos<0)
	{
		printf("位置不合理\n");
		return;
	}
	for(int i=pos;i<L->len-1;i++)
	{
		L->data[i]=L->data[i+1];
	}
	L->len--;
}

void del_rep(seq_p L)
{
	if(L==NULL)
	{
		return;
	}
	if(seq_empty(L))
	{
		return;
	}
	if(L->len==1)
	{
		printf("只有一个元素\n");
		return;
	}
	for(int i=0;i<L->len;i++)
	{
		for(int j=i+1;j<L->len;j++)
		{
			if(L->data[i]==L->data[j])
			{
				del_pos(L,j);
				j--;
			}
		}
	}
}
//seq_list.h

#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 6

typedef int datatype;
typedef struct seq_list
{
	datatype data[MAX];
	int len;
}seq_list,*seq_p;

seq_p create_seq();
int seq_empty(seq_p L);
int seq_full(seq_p L);
void out_seq(seq_p L);
void seq_pos(seq_p L,datatype data,int pos);
void del_pos(seq_p L,int pos);
void del_rep(seq_p L);
#endif
//main.c

#include "seq_list.h"
int main(int argc, const char *argv[])
{
	seq_p L=create_seq();
	seq_pos(L,82,0);
	seq_pos(L,44,1);
	seq_pos(L,23,2);
	seq_pos(L,77,3);
	seq_pos(L,82,4);
	seq_pos(L,91,5);
	out_seq(L);
	putchar(10);
	del_pos(L,1);
	out_seq(L);
	putchar(10);
	del_rep(L);
	out_seq(L);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值