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

数据结构/作业/2024/7/7

搭建个场景:
将学生的信息,以顺序表的方式存储(堆区),并且实现封装函数︰1】顺序表的创建,
2】判满、
3】判空、
4】往顺序表里增加学生、5】遍历、
6】任意位置插入学生、7】任意位置删除学生、8】修改、
9】查找(按学生的学号查找)、10】去重、
11】销毁顺序表
 

main.c

#include "head.h"
int main(int argc,const char *argv[])
{//创建学生的顺序表seqlist_ptr p=create_list();//判断顺序表是否为满int p1=full_doubt(p);//判断顺序表是否为空int p2=empty_doubt(p);//顺序表中添加学生数据add(p,6666);add(p,1001);add(p,1002);add(p,1003);add(p,1004);//顺序表中输出学生数据               output(p);//在任意位置插入学生insert(p,3,1111); output(p);//删除任意位置的学生del(p,3);output(p);//更改学生IDchange_index(p,3,6666);output(p);//查找学生IDfind(p,6666);//去重del_same(p);output(p);//释放my_free(p);return 0;
}

fun.c

  1 #include "head.h"                                                                                      2                                                                                                        3                                                                                                        4 //1.创建学生的顺序表                                                                                   5 seqlist_ptr create_list()                                                                              6 {                                                                                                      7     //申请堆区的空间                                                                                   8     seqlist_ptr p=(seqlist_ptr)malloc(sizeof(seqlist));                                                9     if(NULL==p)                                                                                        10     {                                                                                                  11         printf("顺序表创建失败\n");                                                                    12         return NULL;                                                                                   13     }                                                                                                  14                                                                                                        15     p->len=0;//将顺序表中的长度清零                                                                    16     //将数组的长度清零                                                                                 17     memset(p->ID,0,sizeof(p->ID));                                                                     18     printf("创建顺序表成功\n");                                                                        19     return p;                                                                                          20 }                                                                                                      21                                                                                                        22                                                                                                        23 //2.判断顺序表是否为满                                                                                 24 int full_doubt(seqlist_ptr p)                                                                          25 {                                                                                                      26     if(NULL==p)                                                                                        27     {                                                                                                  28         printf("顺序表不合法,无法判断");                                                               29         return -1;                                                                                     30     }                                                                                                  31     else if(p->len==MAX)                                                                               32     {                                                                                                  33         printf("顺序表满\n");                                                                          34         return 1;                                                                                      35     }                                                                                                  36                                                                                                        37     return 0;                                                                                          38 }                                                                                                      39                                                                                                        40                                                                                                        41 //3.判断顺序表是否为空                                                                                 42 int empty_doubt(seqlist_ptr p)                                                                         43 {                                                                                                      44     if(NULL==p)                                                                                        45     {                                                                                                  46         printf("顺序表不合法,无法判断");                                                               47     }                                                                                                  48     else if(p->len==0)                                                                                 49     {                                                                                                  50         printf("顺序表为空\n");                                                                        51         return 1;                                                                                      52     }                                                                                                  53     return 0;                                                                                          54 }                                                                                                      55                                                                                                        56                                                                                                        57 //4.顺序表数据的增加(添加学生的id号)                                                                   58 int add(seqlist_ptr p,datatype a)                                                                      59 {                                                                                                      60     if(NULL==p || full_doubt(p))                                                                       61     {                                                                                                  62         printf("无法增加\n");                                                                          63         return 0;                                                                                      64     }                                                                                                  65     p->ID[p->len]=a;                                                                                   66     p->len++;                                                                                          67     return 1;                                                                                          68 }                                                                                                      69                                                                                                        70                                                                                                        71 //5.顺序表中输出学生数据                                                                               72 int output(seqlist_ptr p)                                                                              73 {                                                                                                      74     if(NULL==p || empty_doubt(p))                                                                      75     {                                                                                                  76         printf("无法输出i\n");                                                                         77         return 0;                                                                                      78     }                                                                                                  79     for(int i=0;i<p->len;i++)                                                                          80     {                                                                                                  81         printf("%d    ",p->ID[i]);                                                                     82     }                                                                                                  83     printf("\n");                                                                                      84     return 1;                                                                                          85 }                                                                                                      86                                                                                                        87                                                                                                        88 //6.在任意位置插入学生数据                                                                             89 int insert(seqlist_ptr p,int index,datatype e)                                                         90 {                                                                                                      91     if(NULL==p || index>=MAX || index<=0 || empty_doubt(p))                                            92     {                                                                                                  93         printf("插入失败\n");                                                                          94         return -1;                                                                                     95     }                                                                                                  96     //此时的index表示数组的下标                                                                        97     index-=1;                                                                                          98     for(int i=0;i<p->len-index;i++)                                                                    99     {   //p->len表示未赋值的那个元素                                                                   
100         p->ID[p->len-i]=p->ID[p->len-1-i];                                                             
101     }                                                                                                  
102     //赋值                                                                                             
103     p->ID[index]=e;                                                                                    
104     //长度+1                                                                                           
105     p->len++;                                                                                          
106     return 1;                                                                                          
107 }                                                                                                      
108                                                                                                        
109                                                                                                        
110 //7.删除任意位置的学生                                                                                 
111 int del(seqlist_ptr p,int index)                                                                       
112 {                                                                                                      
113     if(NULL==p || index>MAX || index<=0 || empty_doubt(p))                                             
114     {                                                                                                  
115         printf("删除失败\n");                                                                          
116         return -1;                                                                                     
117     }                                                                                                  
118     //此时index表示数组的下标                                                                          
119     index-=1;                                                                                          
120     for(int i=index;i<p->len;i++)                                                                      
121     {                                                                                                  
122         p->ID[index]=p->ID[index+1];                                                                   
123     }                                                                                                  
124     p->len--;                                                                                          
125     return 1;                                                                                          
126 }                                                                                                      
127                                                                                                        
128                                                                                                        
129 //8.任意位置更改学生ID                                                                                 
130 int change_index(seqlist_ptr p,int index,datatype e)                                                   
131 {                                                                                                      
132     if(NULL==p || index>MAX || index <=0 || empty_doubt(p))                                            
133     {                                                                                                  
134         printf("更改失败\n");                                                                          
135         return -1;                                                                                     
136     }                                                                                                  
137     index-=1;                                                                                          
138     p->ID[index]=e;                                                                                    
139     return 1;                                                                                          
140 }                                                                                                      
141                                                                                                        
142                                                                                                        
143 //9.查找学生ID                                                                                         
144 int find(seqlist_ptr p,datatype e)                                                                     
145 {                                                                                                      
146     if(NULL==p || empty_doubt(p))                                                                      
147     {                                                                                                  
148         printf("查找失败\n");                                                                          
149         return -1;                                                                                     
150     }                                                                                                  
151     int flag=0;                                                                                        
152     for(int i=0;i<p->len;i++)                                                                          
153     {                                                                                                  
154         if(p->ID[i]==e)                                                                                
155         {                                                                                              
156             flag=1;                                                                                    
157             printf("查找的学生是第%d位学生\n",i+1);                                                    
158             return i;                                                                                  
159         }                                                                                              
160                                                                                                        
161         if(flag=0)                                                                                     
162         {                                                                                              
163             printf("未查找到学生ID\n");                                                                
164             return 0;                                                                                  
165         }                                                                                              
166     }                                                                                                  
167 }                                                                                                      
168                                                                                                        
169                                                                                                        
170 //10.去重                                                                                              
171 int del_same(seqlist_ptr p)                                                                            
172 {                                                                                                      
173     if(NULL==p || empty_doubt(p))                                                                      
174     {                                                                                                  
175         printf("去重失败\n");                                                                          
176         return -1;                                                                                     
177     }                                                                                                  
178     for(int i=0;i<p->len;i++)                                                                          
179     {                                                                                                  
180         for(int j=i+1;j<p->len;j++)                                                                    
181         {                                                                                              
182             if(p->ID[i]==p->ID[j])                                                                     
183             {                                                                                          
184                 del(p,j+1);                                                                            
185                 j--;                                                                                   
186             }                                                                                          
187         }                                                                                              
188     }                                                                                                  
189     return 1;                                                                                          
190 }                                                                                                      
191                                                                                                        
192                                                                                                        
193 //11 释放                                                                                              
194 int my_free(seqlist_ptr p)                                                                             
195 {                                                                                                      
196     if(NULL==p)                                                                                        
197     {                                                                                                  
198         printf("释放失败\n");                                                                          
199         return -1;                                                                                     
200     }                                                                                                  
201     free(p);                                                                                           
202         printf("释放成功\n");                                                                          
203     return 1;                                                                                          
204                                                                                                        
205 }                                                                                                      
~                                                                                                          

head.h

#ifndef __HEAD_H__                                   
#define __HEAD_H__                                   
#include <stdio.h>                                   
#include <stdlib.h>                                  
#include <string.h>                                  
//顺序标容器存储学生个数的最大值                     
#define MAX 30                                       
//宏替换ID的数据类型                                 
typedef int datatype;                                
//创建顺序表用于存储学生的信息                       
typedef struct sequence                              
{                                                    datatype ID[MAX];                                //存储学生的个数                                 int len;                                         
}seqlist,*seqlist_ptr;                               //1.创建学生的顺序表                                 
seqlist_ptr create_list();                           
//2.判断顺序表是否为满                               
int full_doubt(seqlist_ptr p);                       
//3.判断顺序表是否为空                               
int empty_doubt(seqlist_ptr p);                      
//4.顺序表数据的增加(添加学生的id号)                 
int add(seqlist_ptr p,datatype a);                   
//5.顺序表中输出学生数据                             
int output(seqlist_ptr p);                           
//6.在任意位置插入学生数据                           
int insert(seqlist_ptr p,int index,datatype e);      
//7.删除任意位置的学生                               
int del(seqlist_ptr p,int index);                    
//8.任意位置更改学生ID                               
int change_index(seqlist_ptr p,int index,datatype e);
//9.查找学生ID                                       
int find(seqlist_ptr p,datatype e);                  
//10.去重                                            
int del_same(seqlist_ptr p);                         
//11 释放                                            
int my_free(seqlist_ptr p);                          
#endif                                               

 

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

相关文章:

  • 隔离级别-隔离级别中的锁协议、隔离级别类型、隔离级别的设置、隔离级别应用
  • 【数据结构与算法】希尔排序
  • 【机器学习】(基础篇一) —— 什么是机器学习
  • VitePress安装部署
  • Spring的事务传播机制和隔离级别
  • 华为路由器静态路由配置(eNSP模拟实验)
  • antd实现简易相册,zdppy+vue3+antd实现前后端分离相册
  • PIP换源的全面指南
  • 陶建辉当选 GDOS 全球数据库及开源峰会荣誉顾问
  • Drools开源业务规则引擎(二)- Drools规则语言(DRL)
  • PTA甲级1005:Spell It Right
  • Vue笔记11-Composition API的优势
  • rancher管理多个集群
  • 某大会的影响力正在扩大,吞噬了整个数据库世界!
  • PostgreSQL主从复制:打造高可用数据库架构的秘籍
  • Fast R-CNN(论文阅读)
  • 视觉语言模型:融合视觉与语言的未来
  • 【CSAPP】-linklab实验
  • UE C++ 多镜头设置缩放 平移
  • 代码随想录Day69(图论Part05)
  • 53-1 内网代理3 - Netsh端口转发(推荐)
  • 慧哥充电桩开源平台小程序、PC后、手机商户端历经2年的无数次迭代。
  • 四、(1)网络爬虫入门及准备工作(爬虫及数据可视化)
  • 2024华为OD机试真题-分月饼-(C++/Python)-C卷D卷-200分
  • Git 查看提交历史
  • 力扣双指针算法题目:快乐数
  • 【Tools】了解人工通用智能 (AGI):未来的智能体
  • 华媒舍:8种网站构建推广方法全揭密!
  • 【Scrapy】 深入了解 Scrapy 下载中间件的 process_exception 方法
  • DevEco Studio无法识别本地模拟器设备的解决方法