当前位置: 首页 > news >正文

2.22 作业

顺序表

运行结果

fun.c

#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

#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

#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;
}

链表

//尾插
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;}
}

http://www.lryc.cn/news/307172.html

相关文章:

  • office word保存pdf高质量设置
  • 微服务设计模式
  • 10.网络游戏逆向分析与漏洞攻防-游戏网络架构逆向分析-接管游戏发送数据的操作
  • 将SU模型导入ARCGIS,并获取高度信息,多面体转SHP文件(ARCMAP)
  • 【电子通识】为什么单片机芯片上会有多组VDD电源?
  • 跟我学C++中级篇——单实例和静态化
  • 下载 axios.js 文件到本地【linux】
  • 一些matlab的常用用法。在MATLAB中,如何实现数据的导入和导出?
  • 数学建模【插值与拟合】
  • 汽修专用产品---选型介绍 汽修示波器 汽车示波器 汽车电子 汽修波形 汽车传感器波形 汽车检测
  • 如何将简历项目部署到自己的域名下
  • Redisson - 实现Java的Redis分布式和可扩展解决方案
  • 如何利用EXCEL批量插入图片
  • django rest framework 学习笔记-实战商城3
  • WPF真入门教程29--MVVM常用框架之MvvmLight
  • QT-Day4
  • 代码随想录算法训练营第三天
  • 蓝桥杯刷题1
  • 前端学习---- 前端HTML基本元素的介绍
  • 力扣思路题:丑数
  • C# this关键字的作用
  • Ubuntu18.04虚拟机磁盘扩容-lvm
  • 低代码开发:数字赋能智能制造的未来
  • janus-gateway的videoroom插件的RTP包录制功能源码详解
  • nginx+keepalived实现nginx高可用集群以及nginx实现Gateway网关服务集群
  • 主键、外键、建表范式、MySQL索引、用户管理
  • 探究前端路由hash和history的实现原理(包教包会)
  • 幻兽帕鲁服务器多少钱?有买过的吗?
  • MCU独立按键单控LED实现
  • [数据集][目标检测]游泳者溺水数据集VOC+YOLO格式2类别895张