void out_list(SqList L);
void insert_sq(SqList *L,int i,ElemType e); ElemType delete_sq(SqList *L,int i); int locat_sq(SqList L,ElemType e); /* 主函数 */
main()
{ int i,k,loc; ElemType e,x; char ch; do { printf(\
printf(\建立线性表 \
printf(\在i位置插入元素e\ printf(\删除第i个元素,返回其值\ printf(\查找值为 e 的元素\ printf(\结束程序运行\
printf(\ printf(\请输入您的选择(1,2,3,4,6)\ scanf(\ switch(k)
{ case 1:{ creat_list(&a); out_list(a); } break;
case 2:{ printf(\ insert_sq(&a,i,e); out_list(a); } break; case 3:{ printf(\ x=delete_sq(&a,i); out_list(a);
printf(\
} break;
case 4:{ printf(\
loc=locat_sq(a,e);
if (loc==-1) printf(\未找到 %d\
else printf(\已找到,元素位置是 %d\
} break; } /* switch */
}while(k!=6);
printf(\再见!\
printf(“\\n 打回车键,返回。“); ch=getch(); } /* main */ /* 建立线性表 */
void creat_list(SqList *L) { int i;
16
printf(\
for(i=0;i
scanf(\
} } /* creat_list */ /* 输出线性表 */
void out_list(SqList L) { int i; char ch;
printf(\
for(i=0;i<=L.length-1;i++) printf(\ printf(\打回车键,继续。“); ch=getch(); } /* out_list */
/* 在线性表的第i个位置插入元素e */ void insert_sq(SqList *L,int i,ElemType e)
{ int j;
if (L->length==MAXSIZE) printf(\
else if(i<1||i>L->length+1) printf(\
else { for(j=L->length-1; j>i-1; j--) L->a[j+1]=L->a[j]; /* 向后移动数据元素 */
L->a[i-1]=e; /* 插入元素 */ L->length++; /* 线性表长加1 */ }
} /* insert_sq */
/* 删除第i个元素,返回其值 */ ElemType delete_sq(SqList *L, int i)
{ ElemType x; int j;
if( L->length==0) printf(\是空表。underflow !\ else if(i<1||i> L->length){ printf(\ x=-1;} else { x=L->a[i-1];
for(j=i; j<=L->length-1; j++) L->a[j-1]=L->a[j]; L->length--; }
return(x);
} /* delete_sq */
/* 查找值为 e 的元素,返回它的位置 */ int locat_sq(SqList L, ElemType e) { int i=0;
while(i<=L.length-1 && L.a[i]!=e) i++;
17
if(i<=L.length-1) return(i+1); else return(-1); }/* locat_sq */
2. 线性表的链表存储表示(结构)及实现。 阅读下列程序请注意几个问题:
(1)关于线性表的链表存储结构的本质是:在逻辑上相邻的两个数据元素ai-1, ai,在存储地址中可以不相邻,既地址不连续。不同的教材的表示基本是一致的。 typedef struct LNode
{ ElemType data; /* 数据子域 */ struct LNode *next; /* 指针子域 */
}LNode; /* 结点结构类型 */
(2)本程序是一个完整的、子函数较多的源程序。目的为学生提供一个示范,提供关于链表操作的资料,供学生参考。可以看到本程序的main()与前一程序的main()函数的结构十分相似。稍加改动还可为其他题目的源程序所用。 [源程序]
#include
typedef int ElemType;
typedef struct LNode
{ ElemType data; /* 数据子域 */ struct LNode *next; /* 指针子域 */ }LNode; /* 结点结构类型 */ LNode *L; /* 函数声明 */ LNode *creat_L();
void out_L(LNode *L);
void insert_L(LNode *L,int i ,ElemType e); ElemType delete_L(LNode *L,int i); int locat_L(LNode *L,ElemType e); /* 主函数 */ main()
{ int i,k,loc; ElemType e,x; char ch; do { printf(\
printf(\建立线性链表 \
printf(\在i位置插入元素e\
printf(\删除第i个元素,返回其值\ printf(\查找值为 e 的元素\ printf(\结束程序运行\
18
printf(\
printf(\请输入您的选择 (1,2,3,4,5)\ switch(k)
{ case 1:{ L=creat_L( ); out_L(L); } break;
case 2:{ printf(\ insert_L(L,i,e); out_L(L); } break; case 3:{ printf(\ x=delete_L(L,i); out_L(L); if(x!=-1) printf(\ } break;
case 4:{ printf(\
loc=locat_L(L,e);
if (loc==-1) printf(\未找到 %d\
else printf(\已找到,元素位置是 %d\ } break;
} /* switch */
printf(\
}while(k>=1 && k<5);
printf(\再见!\
printf(“\\n 打回车键,返回。“); ch=getch(); } /* main */
/* 建立线性链表 ( 11,22 ,33 ) */ LNode *creat( )
{ LNode *h,*p,*s; ElemType x;
h=(LNode *)malloc(sizeof(LNode)); /* 分配头结点 */ h->next=NULL; p=h;
printf(\输入第一个数据元素 */ while( x!=-111) /* 输入-111,结束循环 */ { s=(LNode *)malloc(sizeof(LNode)); /* 分配新结点 */ s->data=x; s->next=NULL;
p->next=s; p=s;
printf(\输入下一个数据*/ }
return(h);
} /* creat_L */
19
h ∧ h 11 22 建成后的链表h
33 ∧
/* 输出单链表中的数据元素*/ void out_L(LNode *L) { LNode *p; char ch;
p=L->next; printf(\
while(p!=NULL) { printf(\ };
printf(\打回车键,继续。“); ch=getch(); } /* out_link */
/* 在i位置插入元素e */
void insert_L(LNode *L,int i, ElemType e) { LNode *s,*p,*q; int j;
p=L; /* 找第i-1个结点 */
j=0;
while(p!=NULL && j
} /* insert_L */
/* 删除第i个元素,返回其值 */ ElemType delete_L(LNode *L,int i) { LNode *p,*q; int j; ElemType x; p=L; j=0;
while(p->next!=NULL && j
if(p->next==NULL) {printf(\ else { q=p->next; x=q->data;
p->next=q->next; free(q); return(x); }
} /* delete_L */
/* 查找值为 e 的元素, 返回它的位置 */ int locat_L(LNode *L,ElemType e) { LNode *p; int j=1; p=L->next;
while(p!=NULL && p->data!=e) {p=p->next; j++;}
20