for(i=0;i<n;i++)(scanf("%d",&A[i]);}return n,)void disp(sqlist A,int n))1/输出顺序表int i,printf("输出顺序表:");if (n==0)printf("empty");for(i=0;i<n;i++)printf("%d",A[i]);}#include"sq.c"int comb(sqlist A,int na,sqlist B,int nb)(int n=na,m=nb,if(na+nb>MaxLen)return 0;while(nb>0)if(na==Oll(A[na-1]<B[nb-1])A[na+nb-1]=B[nb-1];nb--3]else(A[na+nb-1]-=A[na-1];na--:}na=n+m;,return na,3void mainOsqlist A,B;int na,nb;na=create(A);7
7 for(i=0;i<n;i++) {scanf("%d",&A[i]);} return n;} void disp(sqlist A,int n) //输出顺序表 { int i; printf("输出顺序表:"); if (n==0)printf("empty"); for(i=0;i<n;i++) printf("%d",A[i]);} #include "sq.c" int comb(sqlist A,int na,sqlist B,int nb) {int n=na,m=nb; if(na+nb>MaxLen)return 0; while(nb>0) if(na==0||(A[na-1]<B[nb-1])) { A[na+nb-1]=B[nb-1]; nb-;} else {A[na+nb-1]=A[na-1]; na-;} na=n+m; return na; } void main() { sqlist A,B; int na,nb; na=create(A);
nb=create(B);disp(A,na);disp(B,nb);na=comb(A,na,B,nb):disp(A,na);)2、#include"sq.c"#include<stdio.h>void invert(sqlist A,int n)int m=n/2,ielemtype temp,for(i=0;i<m;i++)temp=A[i];A[i]=A[n-i-1];A[n-i-1]-temp;1void main()sqlist A;int n;n=create(A);disp(A,n);invert(A,n);disp(A,n);3、#include<stdio.h>void main([int a[11]={12,13,14,16,22,25,26,27,29,30,54];int temp1,temp2,number,end,i,j;8
8 nb=create(B); disp(A,na); disp(B,nb); na=comb(A,na,B,nb); disp(A,na);} 2、#include "sq.c" #include<stdio.h> void invert(sqlist A,int n) { int m=n/2,i; elemtype temp; for(i=0;i<m;i++) { temp=A[i];A[i]=A[n-i-1];A[n-i-1]=temp; } } void main() { sqlist A; int n; n=create(A); disp(A,n); invert(A,n); disp(A,n); } 3、#include <stdio.h> void main() {int a[11]={12,13,14,16,22,25,26,27,29,30,54}; int temp1,temp2,number,end,i,j;
printf("初始线性表如下:");for(i=0;<10;i++)printf("%5d",a[i]);printf("\n");printf("输入插入的数据:");scanf("%d",&number);end=a[9];if(number>end)a[10]=number;else(for(i=0;i<10;i++)(if(a[i]>number)(temp]=a[i];a[i]=number;for(j=i+1:j<11:j++){temp2=a[i];a[i]=templ;temp1=temp2;break;3)printf("插入后线性表如下:");for(i=0;i<11;i++)printf("%5d",a[i]);三、思考题如果按由表尾至表头的次序输入数据元素,应如何建立顺序表?9
9 printf("初始线性表如下:"); for(i=0;i<10;i++) printf("%5d",a[i]); printf("\n"); printf("输入插入的数据:"); scanf("%d",&number); end=a[9]; if(number>end) a[10]=number; else {for(i=0;i<10;i++) {if(a[i]>number) {temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break;}}} printf("插入后线性表如下:"); for(i=0;i<11;i++) printf("%5d",a[i]); } 三、思考题 如果按由表尾至表头的次序输入数据元素,应如何建立顺序表?
1.3线性表的链式存储结构一、实验目的1、掌握线性表的链式存储结构一一单链表的定义及C语言实现。2、理解和掌握单链表的类型定义方法和结点生成方法。3、掌握线性表在链式存储结构一一单链表中的各种基本操作。4、利用线性表在链式存储结构解决实际问题。二、实验内容(一)验证实验1、建立一个单链表,并从屏幕显示单链表元素列表。2、现要删除链表某位置上的元素,并保持链表原有的顺序不变,请在给出的程序中加入一个删除函数,实现上述功能要求。ElemTypedelete_L(LNode*L,inti)为删除函数的原型,L表示链表,i表示插入位置。注意菜单中给出了菜单项,请在switch语句给出调用语句,在主函数中加入册删除函数,并注意判断表为空的情况实验程序:#include <stdio.h>#include<stdlib.h>#include<math.h>#include<conio.h>typedef intElemTypetypedefstructLNode数据子域[ElemTypedata)I指针子域struct LNode *next,//结点结构类型}LNode;LNode *L;I函数声明LNode *creat _LO;10
10 1.3 线性表的链式存储结构 一、实验目的 1、掌握线性表的链式存储结构——单链表的定义及 C 语言实现。 2、理解和掌握单链表的类型定义方法和结点生成方法。 3、掌握线性表在链式存储结构——单链表中的各种基本操作。 4、利用线性表在链式存储结构解决实际问题。 二、实验内容 (一)验证实验 1、建立一个单链表,并从屏幕显示单链表元素列表。 2、现要删除链表某位置上的元素,并保持链表原有的顺序不变,请在给出的程序 中加入一个删除函数,实现上述功能要求。 ElemType delete_L(LNode *L,int i)为删除函数的原型,L 表示链表,i 表示插入 位置。注意菜单中给出了菜单项,请在 switch 语句给出调用语句,在主函数中加入删 除函数,并注意判断表为空的情况 实验程序: #include <stdio.h> #include <stdlib.h> #include <math.h> #include<conio.h> 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);I主函数void main()( int i,k,loc, ElemType e,x, char ch;do printf("nln");printf("nln线性链表基本操作");printf("n");printf("nln1.建立线性链表“");printf("\nln2.在i位置插入元素e")printf("\nln3.删除第i个元素,返回其值");4.查找值为e的元素");printf("nlnprintf("\nln5.结束程序运行");printf("\n2请输入您的选择(1,2,3,4,5)");scanf("%d"&k);printf("lnswitch(k)1case I:( L=creat L(); out L(L);↓ break,case 2: printf("ln i,e=?"); scanf("%d,%d",&i,&e),insert _L(L,i,e),out L(L)↓ break,case 3:( printf("ln i=?"),scanf("%d",&i);x=delete L(L,i):if(x/=-1) printf("In x=%d)n",x);out_L(L),) break;case 4:(printf("n e=?"), scanf("%d",&e),11
11 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); // 主函数 void main() { int i,k,loc; ElemType e,x; char ch; do { printf("\n\n"); printf("\n\n 线性链表基本操作 "); printf("\n======================================"); printf("\n\n 1. 建立线性链表 "); printf("\n\n 2. 在 i 位置插入元素 e"); printf("\n\n 3. 删除第 i 个元素,返回其值"); printf("\n\n 4. 查找值为 e 的元素"); printf("\n\n 5. 结束程序运行"); printf("\n======================================"); printf("\n 请输入您的选择 (1,2,3,4,5)"); scanf("%d",&k); switch(k) { case 1:{ L=creat_L( ); out_L(L); } break; case 2:{ printf("\n i,e=?"); scanf("%d,%d",&i,&e); insert_L(L,i,e); out_L(L); } break; case 3:{ printf("\n i=?"); scanf("%d",&i); x=delete_L(L,i); if(x!=-1) printf("\n x=%d\n",x); out_L(L);} break; case 4:{ printf("\n e=?"); scanf("%d",&e);