?? file1.cpp
字號:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20 /* 數(shù)組最大界限 */
typedef int ElemType; /* 數(shù)據(jù)元素類型 */
typedef struct
{ ElemType a[MAXSIZE]; /* 一維數(shù)組 子域 */
int length; /* 表長度子域 */
}SqList; /* 順序存儲的結(jié)構(gòu)體類型 */
SqList a,b,c;
/* 函數(shù)聲明 */
SqList creat_list(SqList *L);
void out_list(SqList L);
void insert_sq(SqList *L,int i,ElemType e);
void Combine_list(SqList *,SqList * );
ElemType delete_sq(SqList *L,int i);
int locat_sq(SqList L,ElemType e);
/* 主函數(shù) */
main()
{ int i,k,loc,d=0; ElemType e,x; char ch;
SqList q[3];
system("graftabl 936");
clrscr();
do { printf("\n\n\n");
printf("\n 1. 建立線性表 " );
printf("\n 2. 在i位置插入元素e");
printf("\n 3. 刪除第i個(gè)元素,返回其值");
printf("\n 4. 查找值為 e 的元素");
printf("\n 5. 將兩個(gè)有序順序表合;并成一個(gè)新的有序順序表");
printf("\n 6. 結(jié)束程序運(yùn)行");
printf("\n======================================");
printf("\n 請輸入您的選擇(1,2,3,4,5,6)");
scanf("%d",&k);
switch(k)
{ case 1:{ q[d++]=creat_list(&a); out_list(a);
} break;
case 2:{ printf("\n i,e=?"); scanf("%d,%d",&i,&e);
insert_sq(&q[d-1],i,e); out_list(a);
} break;
case 3:{ printf("\n i=?"); scanf("%d",&i);
x=delete_sq(&q[d-1],i); out_list(a);
printf("\n x=%d",x);
} break;
case 4:{ printf("\n e=?"); scanf("%d",&e);
loc=locat_sq(q[d-1],e);
if (loc==-1) printf("\n 未找到 %d",loc);
else printf("\n 已找到,元素位置是 %d",loc);
} break;
case 5:{ Combine_list(&q[0],&q[d-1]);out_list(q[0]);}break;
} /* switch */
}while(k!=6);
printf("\n 再見!");
printf("\n 打回車鍵,返回。");
/* ch=getch(); */
} /* main */
/* 建立線性表 */
SqList creat_list(SqList *L)
{ int i;
printf("\n n=?"); scanf("%d",&L->length);
for(i=0;i<L->length;i++){ printf("\n data %d=?",i);
scanf("%d",&(L->a[i]));
}
return *L;
} /* creat_list */
/* 輸出線性表 */
void out_list(SqList L)
{ int i; char ch;
printf("\n");
for(i=0;i<=L.length-1;i++) printf("%10d",L.a[i]);
printf("\n\n 打回車鍵,繼續(xù)。");
/* ch=getch(); */
} /* out_list */
/* 在線性表的第i個(gè)位置插入元素e */
void insert_sq(SqList *L,int i,ElemType e)
{ int j;
if (L->length==MAXSIZE) printf("\n overflow !");
else if(i<1||i>L->length+1) printf("\n erroe i !");
else { for(j=L->length-1; j>i-1; j--) L->a[j+1]=L->a[j];
/* 向后移動數(shù)據(jù)元素 */
L->a[i-1]=e; /* 插入元素 */
L->length++; /* 線性表長加1 */
}
} /* insert_sq */
/* 刪除第i個(gè)元素,返回其值 */
ElemType delete_sq(SqList *L, int i)
{ ElemType x; int j;
if( L->length==0) printf("\n 是空表。underflow !");
else if(i<1||i> L->length){ printf("\n error i !");
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 的元素,返回它的位置 */
void Combine_list(SqList *L1,SqList *L2 )
{ int j=L1->length;
int i=0;
L1->length=L1->length+L2->length;
printf("the length of L1:%d",L1->length);
while(i<L2->length)
{
L1->a[j+i]=L2->a[i];
i++;
}
}
int locat_sq(SqList L, ElemType e)
{ int i=0;
while(i<=L.length-1 && L.a[i]!=e) i++;
if(i<=L.length-1) return(i+1);
else return(-1);
}/* locat_sq */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -