链性表的练习
发布日期:2021-09-16 12:20:07 浏览次数:8 分类:技术文章

本文共 2100 字,大约阅读时间需要 7 分钟。

#include
#include
#define OK 1#define ERROR 0#define OVERLOW -2typedef int ElemType;typedef int Status;typedef struct sqlist{ ElemType data;//数据域 struct sqlist *next;//指针域 }SqList;//得到该线性表的长度int GetLen(SqList *L){ if(!L){ printf("线性表为空,可能未创建或被销毁\n"); return ERROR; } int len=0; while(L!=NULL){ len++; L = L->next; } return len;} //打印线性表的信息Status PrintSqList(SqList *L){// int GetLen(SqList *L); //声明要调用的函数,如果该函数在本函数的上面,那么就可以不用声明 if(!L){ printf("线性表为空,可能未创建或被销毁\n"); } int len=GetLen(L); printf("长度:%d\n",len); printf("元素列表:"); while(L!=NULL){ printf("%d,",L->data); L=L->next; } printf("\n"); return OK;} //初始化线性表SqList * InitSqList(SqList *L){ L=(SqList *)malloc(sizeof(SqList));//作为头结点 L->data=-1; L->next=NULL; return L;}//对该线性表添加内容SqList * AddElem(SqList *L,ElemType e,int i){ if(!L){ printf("线性表为空,可能未创建或被销毁\n"); return NULL; } int length=GetLen(L); if(i<1 || i>length+1){//可以插入在最后 printf("插入的位置错误,不在线性表的范围之内"); return L; } SqList *head=L; SqList *temp=(SqList *)malloc(sizeof(ElemType)); temp->data=e; for(int j=0;j<=length;j++){ if(j==i-1){//第一个 temp->next=L->next; L->next=temp; } L=L->next; } return head;} //对该线性表删除第几个元素SqList * DelElem(SqList *L,int i){ if(!L){ printf("线性表为空,可能未创建或被销毁\n"); return NULL; } int length=GetLen(L); if(length==0){ printf("表已经为空,不用进行删除操作\n"); } if(i<1 || i>length){ printf("删除的位置有误,不在线性表的范围之内\n"); } SqList *head=L; for(int j=0;j
0 ||j==i-1&&j==0){//得到当前删除的前一个节点, SqList *temp=L->next; L->next=L->next->next; free(temp);//释放要删除的节点 return head; } L=L->next; } return head;} //取得第几个元素,在当前线性表中ElemType GetElem(SqList *L,int i){ if(!L){ printf("线性表为空,可能未创建或被销毁\n"); return NULL; } int length=GetLen(L); if(i<1 || i>length){ printf("获得元素的位置有误,不在线性表的范围之内\n"); return OVERLOW; } SqList *p=L->next;//获得第一个元素 ,因为L是为头结点而不是第一个元素 for(int j=0;j
data; } p=p->next; } return ERROR;} int main(){ SqList *L; L=InitSqList(L); PrintSqList(L); AddElem(L,8,1); PrintSqList(L); AddElem(L,4,1); PrintSqList(L);// DelElem(L,1);// PrintSqList(L); int e=GetElem(L,1); printf("%d\n",e); return 0;}

转载地址:https://blog.csdn.net/wxwzy738/article/details/7495106 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:栈的练习
下一篇:顺序表的练习

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月21日 18时20分10秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【深度学习笔记】PixelShuffle 2019-04-30
【python3学习笔记】斜杠和双斜杠运算符的区别 2019-04-30
【深度学习笔记】torch.nn.Sequential(* args) 与 torch.nn.Module 2019-04-30
【深度学习笔记】用torch.nn.Sequential()搭建神经网络模型 2019-04-30
【深度学习笔记】用torch.nn.ModuleList搭建神经网络 2019-04-30
【解决错误】AttributeError: module ‘scipy.misc‘ has no attribute ‘imread‘ 2019-04-30
【解决错误】复现RCAN的时候遇到了ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘skimage‘ 2019-04-30
【深度学习笔记】pytorch的点乘(dot product) 2019-04-30
【深度学习笔记】残差 2019-04-30
【错误解决】cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\sr 2019-04-30
【python学习笔记】读取指定文件夹中的图片,结合边缘保留滤波EPF 2019-04-30
【工具和环境】Linux下安装pycharm 2019-04-30
【Accumulation】The last two sentences of the abstract 2019-04-30
【Accumulation】The definition of SISR 2019-04-30
【工具与环境】Windows下安装Sublime Text 3 2019-04-30
【解决错误】ValueError: some of the strides of a given numpy array are negative. 2019-04-30
【工具与环境】Excel中批量插入行 2019-04-30
【个人实验注意事项】 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘tqdm‘ 2019-04-30