题目:顺序表作业
代码:
功能区:
#include <stdio.h>#include <stdlib.h>#include "./d2191.h"SeqList* create_seqList(){SeqList* list = (SeqList*)malloc(sizeof(SeqList));if(NULL== list){return NULL;}list->pos=0;//初始化要操作的位置为0return list;}void insert_seqList(SeqList* list,dataype num){//判断顺序表是否为满if(list->pos >=N){printf("顺序表为满,插入失败\n");return;}//在pos位置插入数据list->data[list->pos] = num;//将pos++(list->pos)++;printf("插入成功\n");return;}void show_seqList(SeqList* list){int i;for(i=0;i<=list->pos;i++){printf("%d ",list->data[i]);}printf("\n");}dataype del_seqList(SeqList* list){//判断顺序表是否为空if(list->pos > 0){(list->pos)--;dataype num =list->data[list->pos];return num;}}//从下标插入数据void index_seqList(SeqList* list,int index,int temp){int i ;if(index<=0||index>=N||list->pos >=N){printf("要插入的下标错误或者超出");return;}for(i=list->pos;i>index;i--){list->data[i]=list->data[i-1];}(list->pos)++;list->data[index]=temp;return;}//从下标删除数据dataype deletebyindex_seqList(SeqList* list,int index){int i;if(index<=0||index>=N||list->pos >=N){printf("要删除的下标错误或者超出");return 0;}int beifen=list->data[index];for(i=index;i<list->pos;i++){list->data[i]=list->data[i+1];}(list->pos)--;return beifen;}//从下标修改数据void update_seqList(SeqList* list,int index,int newnum){if(index<0||index>list->pos){printf("非法输入\n");return;}list->data[index]=newnum;printf("修改成功\n");return;}//按下标查找数据dataype find_seqList(SeqList* list,int index){if(index<0||index>list->pos){printf("非法输入\n");return 0;}return list->data[index];}//按数据修改数据void updatebydata_seqList(SeqList* list,int oldnum,int newnum){int i;for(i=0;i<list->pos;i++){if(list->data[i]==oldnum){list->data[i]=newnum;}}printf("更新完成\n");return ;}//按数据查找位置int selectbydata_seqList(SeqList* list,int num){int i;for(i=0;i<list->pos;i++){if(list->data[i]==num){return i;}}
函数调用区:
include <stdio.h>
include "./d2191.h"
nt main(int argc, const char *argv[])//创建一个顺序表SeqList* list = create_seqList();//从尾部插入数据insert_seqList(list,10);insert_seqList(list,100);insert_seqList(list,1000);insert_seqList(list,10000);insert_seqList(list,10);//从尾部删除printf("%d\n", del_seqList(list));//从下标插入数据index_seqList(list,5,888);show_seqList(list);//从下标删除数据printf("%d\n",deletebyindex_seqList(list,3));show_seqList(list); //从下标修改数据update_seqList( list,3,66);show_seqList(list);//按下标查找数据printf("查找的数据是%d\n",find_seqList(list,3));//按数据修改数据updatebydata_seqList(list,10,11);show_seqList(list);//按数据查找位置printf("该数字在第几个%d\n",selectbydata_seqList(list,66));return 0;
头文件区:
#ifndef FUNC_C_#define FUNC_C_#define N 6//顺序表容量typedef int dataype;typedef struct{dataype data[N];//顺序表int pos;//记录要操作顺序表的位置}SeqList;SeqList* create_seqList();void insert_seqList(SeqList* list,dataype num);void show_seqList(SeqList* list);dataype del_seqList(SeqList* list) ;void index_seqList(SeqList* list,int index,int temp);dataype deletebyindex_seqList(SeqList* list,int index);void update_seqList(SeqList* list,int index,int newnum);dataype find_seqList(SeqList* list,int index);void updatebydata_seqList(SeqList* list,int oldnum,int newnum);int selectbydata_seqList(SeqList* list,int num);#endif
运行结果: