线性表的基本操作

*e=L.elem[i-1]; //将第i个数据元素的值赋给e return OK; } //GetElem_Sq

Status LocateElem_Sq (SqList L, int e) { int i; for(i=1; i<=L.length; i++) {if(L.elem[i-1] == e) return i; } }

Status PriorElem_Sq(SqList L,int i, ElemType cur_e, ElemType *pre_e) { i=LocateElem_Sq(L,cur_e); if(i==1||i> ListLength_Sq(L)) return 0;

*pre_e=L.elem[i-2] ; //将cur_e的前驱赋值给pre_e

return OK; } //PriorElem_Sq

Status NextElem_Sq(SqList L,int i, ElemType cur_e, ElemType *next_e) {

i=LocateElem_Sq(L,cur_e); if(i==ListLength_Sq(L)||i>ListLength_Sq(L)) return 0;

*next_e= L.elem[i]; //将cur_e的后继赋值给next_e return OK; } //NextElem_Sq

Status ListInsert_Sq(SqList *L, int i, ElemType e) {

//在线性表L中的第i个位置之前插入新的元素e。 ElemType *newbase,*p,*q;

if (i<1||i>(*L).length+1) return ERROR; // i值不合法 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]; // q为插入位置 for(p=&((*L).elem[(*L).length-1]);p>=q;--p) *(p+1)=*p; //插入位置及之后的元

素右移 *q=e; //插入e ++(*L).length; //表长增1 return OK; } //ListInsert_Sq

Status ListDelete_Sq(SqList *L, int i, ElemType *e) { //在线性表L中删除第i个元素,并用e返回其值 ElemType *p,*q;

if (i<1||i>(*L).length) return ERROR; p=&((*L).elem[i-1]); *e=*p; q=(*L).elem+(*L).length-1; for (++p;p<=q;++p) *(p-1)=*p; 移

--(*L).length; return OK; } //ListDelete_Sq

Status ListTraverse_Sq(SqList L,int i) { //线性表L的遍历 printf(\线性表为:\ for(i=0;i

void main(){ SqList L; ElemType e,cur_e,pre_e,next_e; int i,n,select,t; InitList_Sq(&L); CreateList_Sq(&L); printf(\ do{ printf(\:判断线性表是否为空\\n\ printf(\:线性表的长度\\n\ printf(\:查询线性表的第i个元素\\n\ printf(\:查找值为e的元素的位置\\n\ printf(\:求数据元素t的前驱\\n\ printf(\:求数据元素t的后继\\n\

// i值不合法

// p为被删除元素的位置 //被删除元素的值赋给e //表尾元素的位置 //被删除元素之后的元素左 //表长减1 printf(\:在线性表第i个位置前插入新元素e\\n\ printf(\:删除线性表第i个元素,返回其值\\n\ printf(\:遍历线性表\\n\ printf(\:清空线性表\\n\ printf(\:结束\\n\ scanf(\ switch(select) { case 1: if(ListEmpty_Sq(L)) printf(\线性表为空\\n\ else printf(\线性表非空\\n\ case 2:ListLength_Sq(L); printf(\线性表的长度为 %d\\n\ case 3: printf(\ scanf(\ if(GetElem_Sq(L,i,&e)==ERROR) printf(\值不合法\\n\ else printf(\第%d个元素的值为%d\\n\ case 4:printf(\ scanf(\ i=LocateElem_Sq (L,e); if(i<=L.length) printf(\第%d个元素的值为%d\\n\ else printf(\线性表中没有值为%d的元素\\n\ case 5:printf(\ scanf(\

if(PriorElem_Sq(L,i,t,&e)==1) printf(\元素%d的前驱为%d\\n\\n\ else printf(\元素%d无前驱\\n\ case 6:printf(\ scanf(\

if(NextElem_Sq(L,i,t,&e)==1) printf(\元素%d的后继为%d\\n\\n\ else printf(\元素%d无后继\\n\ case 7:printf(\ scanf(\ printf(\ scanf(\ if(ListInsert_Sq(&L,i,e)==ERROR) printf(\值不合法\\n\\n\

else{ printf(\新的线性表为:\ for(i=0;i

printf(\清空线性表成功,线性表长度是%d \\n\\n\ case 0:printf(\操作结束\\n\ default:printf(\输入选择出错!\\n\ } }while(select!=0); DestroyList_Sq(&L); }

题目二:

#include #include typedef int ElemType; typedef int Status; #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0

#define OVERFLOW -2 typedef struct LNode{ ElemType date; struct LNode *next; }LNode,*LinkList;

Status InitList_L(LinkList *L){ (*L)=(LinkList)malloc(sizeof(LNode)); (*L)->next=NULL; return OK;

联系客服:779662525#qq.com(#替换为@)