数据结构day3

顺序表的按位置插入、按位置删除和去重

main.c

#include "seq_list.h"
int main()
{
	seq_p L = create_seq_list();
	//printf("%d\n",seq_empty(L));
	//printf("%d\n",seq_full(L));
	insert_pos(L,1,0);
	insert_pos(L,3,1);
    insert_pos(L,2,1);
	out_put(L);
	putchar(10);
	
	del_pos(L,1);
	out_put(L);
	putchar(10);
	
	insert_pos(L,2,1);
	insert_pos(L,3,3);
    insert_pos(L,2,4);
    insert_pos(L,4,5);
    insert_pos(L,2,6);
	del(L);
	out_put(L);


	return 0;
}

seq_list.c

#include "seq_list.h"
//创建顺序表
seq_p create_seq_list()
{
	seq_p L = (seq_p)malloc(sizeof(seq_list));
	//在堆区申请一个顺序表的空间
	if(L==NULL)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	L->len = 0;  //长度置0
	bzero(L,sizeof(L->data)); //给数组置0
	return L;
}
//判空
int seq_empty(seq_p L)
{
	//1\容错判断
	if(L==NULL)
	{
		return -1;  //和判断结果区分开
	}
	return L->len==0?1:0;
}

//判满
int seq_full(seq_p L)
{
	//1\容错判断
	//
	if(L==NULL)
	{
		return -1;  //和判断结果区分开
	}
	return L->len==MAX?1:0;
}
//打印顺序表
void out_put(seq_p L)
{
	if(L==NULL)
	{
		return;
	}
	if(seq_empty(L))
	{
		printf("表为空\n");
		return;
	}
	//循环打印顺序表
	for(int i=0;i<L->len;i++)
	{
		printf("%d\n",L->data[i]);
	}
}
void insert_pos(seq_p L,datatype value,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=pos;i<L->len;i++)
	{
		L->data[i+1]=L->data[i];
	}
	L->data[pos]=value;
	L->len++;
}
void del_pos(seq_p L,int pos)
{
	if(L==NULL)
	{
		return;
	}
	if(seq_empty(L))
	{
		printf("表为空,无需删除\n");
		return;
	}
	for(int i=pos;i<L->len;i++)
	{
		L->data[i]=L->data[i+1];
	}
	L->len--;
}
void del(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=0;j<L->len;j++)
		{
			if(i!=j&&L->data[i]==L->data[j])
			{
				del_pos(L,j);
			}
		}
	}
}

seq_list.h

#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#define MAX 7
typedef int datatype;
typedef struct seq_list
{
	datatype data[MAX];
	int len;
}seq_list,*seq_p;
seq_p create_seq_list();
int seq_full(seq_p L);
int seq_empty(seq_p L);
void out_put(seq_p L);
void insert_pos(seq_p L,datatype value,int pos);
void del_pos(seq_p L,int pos);
void del(seq_p L);
#endif 

运行结果

相关推荐

  1. 数据结构DAY3--栈与队列

    2024-02-23 10:52:01       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 10:52:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 10:52:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 10:52:01       87 阅读
  4. Python语言-面向对象

    2024-02-23 10:52:01       96 阅读

热门阅读

  1. 【安全】大模型安全综述

    2024-02-23 10:52:01       49 阅读
  2. K8S NFS持久存储配置

    2024-02-23 10:52:01       51 阅读
  3. 释放 SQL Server 缓存

    2024-02-23 10:52:01       42 阅读
  4. 【PHP进阶】Redis批处理缓存

    2024-02-23 10:52:01       64 阅读
  5. 手机中有哪些逆向进化的功能

    2024-02-23 10:52:01       50 阅读
  6. react项目中的redux以及react-router-dom

    2024-02-23 10:52:01       47 阅读
  7. 两种方法实现批量修改Word文件页眉

    2024-02-23 10:52:01       59 阅读
  8. Sublime text 3 配置

    2024-02-23 10:52:01       50 阅读
  9. Object转List

    2024-02-23 10:52:01       44 阅读