2.22 作业

顺序表

运行结果

fun.c

cs 复制代码
#include "fun.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;  
	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 insert_head(seq_p L,int data)
{
	if(L==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(seq_full(L))
	{
		printf("表已满,不能插入\n");
		return;
	}
	for(int i=L->len-1;i>=0;i--)
	{
		L->data[i+1] = L->data[i];
	}

	L->data[0]=data;
	L->len++;  
}
void insert_tail(seq_p L,int value)
{
	if(L==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(seq_full(L))
	{
		printf("表已满,不能插入\n");
		return;
	}
	L->data[L->len]=value;
	L->len++;
}

void out_put(seq_p L)
{
	for(int i=0;i<L->len;i++)
	{
		printf("%d\n",L->data[i]);
	}
}

void insert_pos(seq_p L,int value,int pos)
{	
	if(L==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	if(seq_full(L))
	{
		printf("表已满,不能插入\n");
		return;
	}
	if(pos>L->len||pos<0)
	{
		printf("位置不合理");
		return;
	}
	for(int i=L->len-1;i>=pos;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)
	{
		printf("入参为空,请检查\n");
		return;
	}	
	if(seq_empty(L))
	{
		printf("表为空,无需删除\n");
		return;
	}
	for(int i=pos;i<L->len-1;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=i+1;j<L->len;j++)
		{
			if(L->data[i]==L->data[j])
			{
				del_pos(L,j);
				j--;
			} 
		}
	}
}

fun.h

cs 复制代码
#ifndef __SEQ_LIST_H__
#define __SEQ_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef struct seq_list
{
	int data[MAX];
	int len;
}
seq_list,*seq_p;
seq_p create_seq_list();
int seq_empty(seq_p L);//判满
int seq_full(seq_p L);//判空
void insert_head(seq_p L,int data);//头插
void insert_tail(seq_p L,int value);//尾插
void out_put(seq_p L);//输出
void insert_pos(seq_p L,int value,int pos);//按下标插入
void del_pos(seq_p L,int pos);//按下标删除
void del(seq_p L);//去重
#endif

main.c

cs 复制代码
#include "fun.h"
int main()
{
	seq_p L = create_seq_list();
	insert_head(L,10);
	insert_head(L,20);
	insert_tail(L,20);
	insert_tail(L,30);
	out_put(L);
	putchar(10);
	insert_pos(L,50,2);
	out_put(L);
	putchar(10);
	del_pos(L,2);
	out_put(L);
	putchar(10);
	del(L);
	out_put(L);
	return 0;
}

链表

cs 复制代码
//尾插
void insert_tail(link_p H,link_p T,datatype data)
{
	if(T==NULL)
	{
		printf("入参为空,请检查\n");
		return;
	}
	link_p new = create_node(data);
	T->next=new;
	H->len++
}
//输出
void out_put(link_p H)
{
	for(int i=0;i<H->len;i++)
	{
		printf("%d\n",(H->next)->data);
		H->next=(H->next)->next;
	}
}
相关推荐
月亮不月亮10 分钟前
月亮商场购物打折Java
java·eclipse
guozhetao20 分钟前
【ST表、倍增】P7167 [eJOI 2020] Fountain (Day1)
java·c++·python·算法·leetcode·深度优先·图论
技术思考者20 分钟前
基础很薄弱如何规划考研
java·经验分享·考研
●VON44 分钟前
重生之我在暑假学习微服务第二天《MybatisPlus-下篇》
java·学习·微服务·架构·mybatis-plus
老华带你飞44 分钟前
口腔助手|口腔挂号预约小程序|基于微信小程序的口腔门诊预约系统的设计与实现(源码+数据库+文档)
java·数据库·微信小程序·小程序·论文·毕设·口腔小程序
枫叶丹41 小时前
【Qt开发】信号与槽(二)-> 信号和槽的使用
开发语言·qt
hqxstudying1 小时前
J2EE模式---服务层模式
java·数据库·后端·spring·oracle·java-ee
GM_8281 小时前
【最新最完整】SpringAI-1.0.0开发MCP Server,搭建MCP Client 实战笔记(进阶+详细+完整代码)
java·后端·ai编程·springai·mcp
都叫我大帅哥1 小时前
Java DelayQueue:时间管理大师的终极武器
java
秋千码途1 小时前
小架构step系列27:Hibernate提供的validator
java·spring·架构·hibernate