收藏 分销(赏)

数据结构C语言版线性表源代码.doc

上传人:仙人****88 文档编号:9198380 上传时间:2025-03-16 格式:DOC 页数:6 大小:31.50KB 下载积分:10 金币
下载 相关 举报
数据结构C语言版线性表源代码.doc_第1页
第1页 / 共6页
数据结构C语言版线性表源代码.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
#include <stdio.h> #include <conio.h> #include <malloc.h> #include <stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMENT 10 #define OK 1 #define ERROR -1 #define OVERFLOW -1 #define ENDFLAG 0 typedef int Status; typedef int ElemType; #define OUTFORMAT "%d " #define INFORMAT "%d" typedef struct{ ElemType *elem; int length; int listsize; }SqList; Status InitList(SqList *L){ L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) ////// 如果没有分配成功 exit(OVERFLOW); /////退出,显示()内内容 L->length=0; L->listsize=LIST_INIT_SIZE; return OK; } Status Inputdata(SqList *L){ ///////SqList *L定义首节点的地址 ElemType temp,*newbase; printf("\nInput the data of the sequencial list:\nNote:0 is the ending flag:\n"); scanf(INFORMAT,&temp); while(temp!=ENDFLAG){ if(L->length>=L->listsize){ newbase=(ElemType *)realloc(L->elem, (L->listsize+LISTINCREMENT)*sizeof(ElemType));////扩 大空间,把旧的地址拷贝到新空间 if(!newbase) exit(OVERFLOW); L->elem=newbase; L->listsize+=LISTINCREMENT; } L->elem[L->length++]=temp; scanf(INFORMAT,&temp); } return OK; } Status ListInsert(SqList *L,int i,ElemType e){ ElemType *p,*q,*newbase; if(i<0||i>L->length) return ERROR; if(L->length>=L->listsize) { Newbase =( elemType*)realloc(L->elem, (L->listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L->elem=newbase; L->listsize+=LISTINCREMENT; } q=&(L->elem[i-1]); for(p=&(L->elem[L->length-1]);p>=q;--p) *(p+1)=*p; *q=e; ++L->length; return OK; } void MyDisplay(SqList L){ int i; for(i=0;i<L.length;i++) printf(OUTFORMAT,L.elem[i]); printf("\n"); } void main(void){ SqList L; ElemType temp; int i; if(!InitList(&L)) { ////如果初始化失败 printf("To initialize the sequencial list fail\n"); getch(); //////如果失败,按任意键退出 exit(0); } if(!Inputdata(&L)){ printf("To input the data of the sequencial list fail\n"); getch(); exit(0); } MyDisplay(L); printf("Input the data that you want to insert:"); scanf(INFORMAT,&temp); printf("Input the insert_location:"); scanf("%d",&i); if(!ListInsert(&L,i,temp)){ printf("To insert fail\n"); getch(); exit(0); } MyDisplay(L); getch(); }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服