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

news/2024/7/18 7:04:23 标签: 数据结构

搭建个场景:
将学生的信息,以顺序表的方式存储(堆区),并且实现封装函数︰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);
    //更改学生ID
    change_index(p,3,6666);
    output(p);
    //查找学生ID
    find(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.niftyadmin.cn/n/5544156.html

相关文章

大连外贸建站公司wordpress主题模板

Robonaut萝卜纳特WP外贸站模板 适合用于工业机器人公司出口做外贸搭建公司官方网站使用的WordPress模板。 https://www.jianzhanpress.com/?p7091 优衣裳WordPress外贸建站模板 简洁的wordpress外贸独立站模板&#xff0c;适合服装、衣服、制衣外贸公司搭建公司官方网站使用…

Qt 基础组件速学 事件过滤器

学习目标&#xff1a;理解事件过滤器 前置环境 运行环境:qt creator 4.12 学习内容和效果演示&#xff1a; Qt 提供了事件过滤器的机制,允许我们在事件到达目标对象之前对事件进行拦截和处理。这在以下情况下非常有用: 全局事件处理: 我们可以在应用程序级别安装一个事件过…

探索数据的奥秘:sklearn中的聚类分析技术

探索数据的奥秘&#xff1a;sklearn中的聚类分析技术 在数据科学领域&#xff0c;聚类分析是一种无监督学习方法&#xff0c;它的目标是将数据集中的样本划分为多个组或“簇”&#xff0c;使得同一组内的样本相似度高&#xff0c;而不同组间的样本相似度低。scikit-learn&…

Eclipse运行main函数报 launch error

右键run as java application&#xff0c;运行main函数的时候报launch error 解决方式&#xff1a;文件右键run configurations 旧的是Project JRE&#xff0c;改成下图这个样子

得帆受邀参加中国信通院 2024低代码·无代码产业大会,共同探索低代码与大模型的新未来

2024年6月26日&#xff0c;由中国通信标准化协会主办&#xff0c;中国通信标准化协会云计算标准和开源推进委员会承办的2024低代码无代码产业大会在北京召开&#xff0c;大会以“智融低无码&#xff0c;模创新未来”为主题&#xff0c;发布领域研究成果&#xff0c;交流行业前沿…

目标检测YOLO实战应用案例100讲-基于深度学习的无人机影像小目标识别(续)

目录 3.2 实验平台和环境 3.3 实验评价指标 3.4 基础框架YOLOv5在无人机数据集上的实验 3.4.1 实验结果 3.4.2 结果分析 4基于深度学习的无人机影像目标检测算法 4.1 基于改进YOLOv5的小目标检测算法研究 4.1.1 增加注意力机制 4.1.2 增加检测层 4.1.3多尺…

关于隐藏、覆盖(重写)、重载的理解

定义区分 在派生-对象中&#xff1a;优先考虑隐藏&#xff0c;此时派生类中的覆盖(重写)也是隐藏;没有隐藏的情况下&#xff0c;子类对象才能调用父类重载函数。[此时感觉virtual没用&#xff0c;]在派生-指针或者引用中&#xff1a;只用覆盖(重写)和重载; 注&#xff1a;C Pr…

创建一个AXIS的初始IP核

参考自&#xff1a;https://www.cnblogs.com/milianke/p/17936380.html 以该博主文章为主&#xff0c;本文章做补充。 注意的点&#xff1a; edit ip 在导出axis的主机和从机的时候&#xff0c;记得选择edit ip&#xff0c;这样才能看到从机和主机的源代码&#xff0c;然后…