数据结构:线性表
不好意思大家由于上段时间比较忙没有更新最近学的。这一学期学的是数据结构先写第二章线性表所写代码有结合听的网科和上课学的因为是新手中间可能有好多错误希望大家可以谅解也欢迎大家指出我的错误谢谢大家线性表n个数据元素的有限序列其中n个数据是相同数据类型的元素个数n定义为线性表长度。相同数据类型的集合)一顺序表一下是顺序表关于C的初始化添加元素遍历取值求顺序表的长度查找插入删除#includestdio.h#define MAXSIZE 100typedef int ElemType;typedef struct{ElemType data[MAXSIZE];int length;}Seqlist;//初始化void initlist(Seqlist *l){l-length0;}//添加元素和遍历int appendElem(Seqlist *l,ElemType e){if(l-length MAXSIZE){printf(顺序表已满\n);return 0;}l-data[l-length]e;l-length;}void listElem(Seqlist *l){for(int i0;i l-length;i){printf(%d ,l-data[i]);}printf(\n);}//取值int GetElem(Seqlist *l,int pos,int *e){if(pos1||posl-length)return 0;*el-data[pos-1];return 1;}//求线性表长度int Getlength(Seqlist *l){return l-length;}//查找int findElem(Seqlist *l,ElemType e){if(l-length0){printf(空表\n);}for(int i0;i,il-length;i){if(l-data[i]e){return i1;}}return 0;}//插入int insertElem(Seqlist *l,int pos,ElemType e){if(l-lengthMAXSIZE){printf(表已满了\n);return 0;}if(pos1||posl-length){printf(插入位置错误\n);return 0;}if(posl-length)for(int il-length-1;ipos-1;i--){l-data[i1]l-data[i];}l-data[pos-1]e;l-length;return 1;}//删除int deleteElem(Seqlist *l ,int pos,ElemType *e){if(l-length0){printf(空表\n);return 0;}if(pos1||posl-length){printf(删除位置错误\n);return 0;}*el-data[pos-1];if(pos l-length)for(int ipos-1;il-length;i){l-data[i]l-data[i-1];}l-length--;return 1;}int main(){Seqlist list;//初始化initlist(list);printf(初始化成功目前长度为%d\n,list.length ) ;printf(目前占用类存%zu字节\n,sizeof(list.data ));//添加元素appendElem(list,20);appendElem(list,30);appendElem(list,40);appendElem(list,50);appendElem(list,60);listElem(list);//取值int a;if(GetElem(list,3,a)){printf(第三个元素的值是%d\n,a);}//求线性表长度int lenGetlength(list);printf(线性表长度%d\n,len);//查找printf(查找的元素位置为%d\n,findElem(list,40));//插入insertElem(list,2,10);printf(插入元素是%d\n,10);listElem(list);//删除ElemType e;deleteElem(list,4,e);printf(被删除数据为%d\n,e);listElem(list);return 0;}二单链表以下是单链表关于C的初始化动态创建单链表前插法遍历取值求单链表的长度查找插入删除#includestdio.h#includestdlib.h#define MAXSIZE 200typedef int ElemType;typedef struct node{ElemType data;struct node *next;}Node;//初始化Node *initlist(){Node *head(Node*)malloc(sizeof(Node));head-data0;head-nextNULL;return head;}//动态创建单链表前插法和遍历int insertHead(Node *l,ElemType e){Node *p(Node*)malloc(sizeof(Node));p-datae;p-nextl-next;l-nextp;}void listNode(Node *l){Node *pl-next;while(p!NULL){printf(%d ,p-data);pp-next;}printf(\n);}//取值int GetElem(Node *l,int i,ElemType *e){int j;Node *pl-next;j1;while(p!NULLji){pp-next;j;}if(pNULL||ji ) return 0;*ep-data;return 1;}//求单链表长度int listlength(Node *l){Node *p;pl-next;int i0;while(p!NULL){i;pp-next;}return i;}//查找int findNode(Node *l,ElemType e){Node *pl-next;int pos1;while(p!NULLp-data!e){pp-next;pos;}if(p!NULL){return pos;}else{return 0;}}//插入int insertNode(Node *l,int pos,ElemType e){Node *pl;int i0;while(ipos-1){pp-next;i;if(pNULL){return 0;}}Node *q(Node*)malloc(sizeof(Node));q-datae;q-nextp-next;p-nextq;return 1;}//删除int deleteNode(Node *l,int pos){Node *pl;int i0;while(ipos-1){pp-next;i;if(pNULL) return 0;}if(p-nextNULL){printf(要删除的位置错误\n);return 0;}Node *qp-next;p-nextq-next;free(q);return 1;}int main(){//初始化Node *listinitlist();printf(链表初始化成功头结点地址%p\n,list) ;//动态创建单链表前插法insertHead(list,32);insertHead(list,33);insertHead(list,35);insertHead(list,36);Node *plist-next;printf(链表中前插的现第一个元素%d\n,p-data);listNode(list);//取值int a;if(GetElem(list,4,a)){printf(第四个元素的值是%d\n,a);}//求单链表长度int lenlistlength(list);printf(单链表表长度%d\n,len);//查找printf(查找的元素位置为%d\n,findNode(list,33));//插入insertNode(list,3,34);printf(插入元素是%d\n,34);listNode(list);//删除deleteNode(list,2);printf(删除元素是%d\n,35);listNode(list);free(p);free(list);return 0;}谢谢大家若有错误欢迎大家指出来谢谢大家。