王道数据结构顺序表的代码实现:
讲道理,实现才有意思。
#include
#include
//静态分配
#define MaxSize 50
typedef int ElemType;
//typedef struct{
// ElemType data[MaxSize];
// int length;
//}SqList;
//动态分配
#define InitSize 100
typedef struct{
ElemType *data;
int length;
}SqList;
void show();
bool ListInsert(SqList &L, int i, int e){
//本算法实现将元素e插入到顺序表中L中第i个位置
int j;
if( i<1||i>L.length+1 ){
printf("输入位置不合法!\n");
return false;
}
if(L.length>=InitSize ) {
printf("没有足够空间!\n");
return false;
}
for( j=L.length; j>=i; j-- )
L.data[j] = L.data[j-1];
L.data[i-1]=e;
L.length++;
return true;
}
bool ListDelete(SqList &L, int i){
int j;
if( i<1|| i>L.length ){
printf("输入位置不合法");
return false;
}
for( j=i; j=0)
printf("创建成功!\n");
return L;
}
bool ListDeleteMin(SqList &L){ //删除最小节点 P18页综合应用题第 1 题 。
int i, t=0, k;
if( L.length>0 ){
k=L.data[0];
printf("k=%d\n",k);
for( i=1; i L.data[i] ){
k=L.data[i];
t=i;
}
L.data[t]=L.data[L.length-1];//由于代码中本人特意为length多加了一个位置,所以减一。
printf("L.data[%d]=%d\n", t, L.data[t]);
L.data[L.length-1] = 0;
L.length--;
return true;
} else
return false;
}
void ReSqList(SqList &L){ // P18页综合应用题第 2 题 。
int i=0, k=L.length-1, t=0;
for( i=0; i<(L.length-1)/2; i++, k-- )
{
t = L.data[i];
L.data[i] = L.data[k];
L.data[k] = t;
}
}
void ValueDelete(SqList &L, int e){ // P18页综合应用题第 3 题 。
int i=0, k=0, j=0;
for( i=0; ie )
{
for( i=e-1, j=u; j=e && L.data[K]<=u ){
printf("L.data[K=%d]=%d\n", K, L.data[K]);
for( j=K; jC.length)
return false;
int i=0, j=0, k=0;
while(i=right||right>=arraySize)
return ;
int mid=(left+right)/2;
for( int i=0; i<=mid-left; i++ ){
int temp = A.data[left+i];
A.data[left+i]=A.data[right-i];
A.data[right-i] = temp;
}
}
void Exchange(SqList &A, int m, int n, int arraySize){ // P18页综合应用题第 8 题 。(2)
Reverse( A, 0, m+n-1, arraySize );
Reverse( A, 0, n-1, arraySize );
Reverse( A, n, m+n-1, arraySize );
} //1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1
void ValueExchange(SqList &A, int e){ // P18页综合应用题第 9 题 方法 1
if(A.length==0)
return ;
int i, j=0, t , k=0, u;
for( i=0; ie)
break;
}
if( e>A.data[A.length-1] ){
A.data[A.length]=e;
A.length++;
return ;
}
if(j==0 ) {
for( k = A.length; k>i; k-- )
A.data[k] = A.data[k-1];
A.data[i]=e;
A.length++;
}
}
void SearchExchangeInsert(SqList A, int e){ // P18页综合应用题第 9 题 方法 2
int low=0, high=A.length-2, mid, t, i;
printf("L=%d\n", A.length-2);
while(low<=high){
mid = (low+high)/2;
if(A.data[mid]==e) break;
else if(A.data[mid]high){
for( i=A.length-2; i
#include
#include
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
void show();
LinkList List_HeadInsert(LinkList &L){ //头插法 带头结点的
int n;
L = (LinkList)malloc(sizeof(LNode));//创建空列表
LNode *s;
L->next = NULL;
scanf("%d",&n);
while(n!=9999){
s = (LNode*)malloc(sizeof(LNode));
s->data = n;
s->next = L->next;
L->next = s;
scanf("%d",&n);
}
return L;
}
LinkList List_TailInsert(LinkList &L){ //尾插法
int x;
L = (LinkList)malloc(sizeof(LNode));
LNode *s, *r=L;
scanf("%d", &x);
while(x!=9999){
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = NULL;
return L;
}
LNode *GetElem(LinkList L, int i) //根据位置找元素
{
int j=1;
LNode *p=L->next;//头指针赋给P
if(i==0)
return L;
if(i<1)
return NULL;
while(p&&jnext;
j++;
}
return p;//返回第i个结点的指针,若i大于表长则返回NULL
}
int LocateElem(LinkList L, int e) //数值位置找元素
{
LNode *p = L->next;
int i=0;
while(p!=NULL&&p->data!=e)
{
p=p->next; i++;
}
if(p==NULL)
i=0;
return i;
}
void Insert(LinkList L, LNode *s, int i){ //插入元素 n=5
LNode *p;
p=GetElem(L, i-1);
s->next = p->next;
p->next = s;
/*-----------------------------------
temp = p->data;
p->data = s->data;
s->data = temp;
------------------------------------- 转为后插法 */
}
void ListDelete( LinkList L, int i ){ //删除元素 n=6
LNode *p, *q;
p=GetElem(L, i-1);
q=p->next;
//p->data = p->next->data; //和后继结点交换数据域
p->next = q->next;
free(q);
}
void Del_X_3(LinkList &L/*加'&'符号,用来引导地址*/, int x) // 综合题第一题,不带头结点
{
LNode *p;
if(L==NULL)
return;
if(L->data==x){
p=L;
L=L->next;
free(p);
Del_X_3(L, x);
}else
Del_X_3(L->next, x);
}
void Del_X_2(LinkList &L/*加'&'符号,用来引导地址*/, int x) //综合提第二题 ①
{
LNode *p=L->next, *pre=L, *q;
while(p!=NULL){
if(p->data==x){
q=p;
p=p->next;
pre->next=p;
free(q);
}else{
pre=p;
p=p->next;
}//else
}//while
}
void Del_X_1(LinkList &L/*加'&'符号,用来引导地址*/, int x) //综合提第二题 ②
{
LNode *p=L->next, *r=L, *q;//r指向尾指针,其初始值为头结点
while(p!=NULL){
if(p->data!=x){
r->next = p;
r=p;
p=p->next;
}else{
q=p;
p=p->next;
free(q);
}
} //while
r->next = NULL;
}
void Print(LinkList L) //综合提第三题 反向输出链表
{
if(L->next!=NULL) Print(L->next);
if(L!=NULL) printf("%d ", L->data);
}
void Del_Min( LinkList L ) //综合题第四题 删除最小值
{
LNode *pre=L, *p=pre->next;
LNode *minpre = pre, *minp=p;
while( p!=NULL ){
if( p->data < minp->data ){
minp=p;
minpre=pre;
}
pre=p;
p=p->next;
}
minpre->next = minp->next;
free(minp);
}
void Reverse_1(LinkList L) //综合题第五题 ①
{
LNode *p, *r;
p=L->next;
L->next=NULL;
while(p!=NULL){
r=p->next;
p->next=L->next;
L->next=p;
p=r;
}
}
void Reverse_2(LinkList L) //综合题第五题 ②
{
LNode *pre, *p=L->next, *r=p->next;
p->next=NULL;
while(r!=NULL){
pre=p;
p=r;
r=r->next;
p->next=pre;
}
L->next=p;
}
void Sort(LinkList L) //综合题第六题 链表排序
{
LNode *p=L->next, *pre;
LNode *r=p->next;
p->next=NULL;
p=r;
while(p!=NULL){
r=p->next;
pre=L;
while(pre->next!=NULL&&pre->next->datadata)
pre=pre->next;
p->next=pre->next;
pre->next=p;
p=r;
}
}
void Del_deter(LinkList &L, int a, int b ) //综合题第七题 删除值域
{
LNode *p=L->next, *pre=L;
while(p!=NULL){
if(p->data>a&&p->datanext=p->next;
free(p);
p=pre->next;
}else {
pre=p;
p=p->next;
}
}
}
void Min_Delete(LinkList &head) //综合题第九题
{
LNode *pre, *p, *u;
while(head->next!=NULL){
pre=head;
p=pre->next;
while(p->next!=NULL){
if( p->next->data < pre->next->data )
pre=p;
p=p->next;
}
printf("%d ", pre->next->data );
u=pre->next;
pre->next=u->next;
free(u);
} //while
free(head);
}
void Div_List( LinkList &L )
{
LNode *pre=L, *p=pre->next, *O, *D;
D = (LinkList)malloc(sizeof(LNode));
O = (LinkList)malloc(sizeof(LNode));
LNode *ro, *ra, *s1=D, *s2=O;
while( p!=NULL ){
if(p->data%2==0){
ro=(LNode*)malloc(sizeof(LNode));
ro->data=p->data;
s1->next=ro;
s1=ro;
}else{
ra=(LNode*)malloc(sizeof(LNode));
ra->data=p->data;
s2->next=ra;
s2=ra;
}
p=p->next;
}
s1->next=NULL;
s2->next=NULL;
while(O->next!=NULL){
O=O->next;
printf("%d ",O->data);
}
printf("\n");
while(D->next!=NULL){
D=D->next;
printf("%d ",D->data);
}
}
LinkList DisCreat_1(LinkList A) //综合题第十题
{
int i=0;
LNode *B=(LinkList)malloc(sizeof(LNode)), *p;
B->next=NULL;
LNode *ra=A,*rb=B;
p=A->next;
A->next=NULL;
while(p!=NULL){
i++;
if(i%2==0){
rb->next=p;
rb=p;
}else{
ra->next=p;
ra=p;
}
p=p->next;
}
ra->next=NULL;
rb->next=NULL;
return B;
}
LinkList DIsCreat_2(LinkList &A) //综合题第十一题
{
LinkList B=(LinkList)malloc(sizeof(LNode)); //创建B表头
B->next=NULL;
LNode *p=A->next, *q;
LNode *ra=A;
while(p!=NULL){
ra->next=p;
ra=p;
p=p->next;
if(p!=NULL)
q=p->next;
p->next=B->next;
B->next=p;
p=q;
}
ra->next=NULL;
while(B!=NULL){
B=B->next;
printf("%d ", B->data);
}
return B;
}
void Del_same(LinkList L) //综合题十二题 ①
{
LNode *pre=L->next, *p=L->next, *s=p->next, *d;
while(s!=NULL){
while(s!=NULL){
if(p->data==s->data){
d=s;
s=s->next;
pre->next=s;
free(d);
s=pre;
}else{
pre=pre->next;
s=s->next;
}
}
p=p->next;
pre=p;
s=pre->next;
}
}
void Del_Same(LinkList L) //综合题十二题 ②
{
LNode *p=L->next, *q;
if(p==NULL)
return ;
while(p->next!=NULL){
q=p->next;
if(p->data==q->data){
p->next=q->next;
}else
p=p->next;
}
}
LinkList Merger(LinkList A, LinkList B) //综合题第十三题
{
LNode *preA=A, *preB=B, *pa=A->next, *pb=B->next, *L;
L = (LinkList)malloc(sizeof(LNode));
L->next=NULL;
while(preA->next!=NULL&&preB->next!=NULL){
if(pa->datadata){
preA->next=pa->next;
pa->next=L->next;
L->next=pa;
pa=preA->next;
}else{
preB->next=pb->next;
pb->next=L->next;
L->next=pb;
pb=preB->next;
}
}
while(preA->next!=NULL){
preA->next=pa->next;
pa->next=L->next;
L->next=pa;
pa=preA->next;
}
while(preB->next!=NULL){
preB->next=pb->next;
pb->next=L->next;
L->next=pb;
pb=preB->next;
}
free(preA);
free(preB);
free(pa);
free(pb);
return L;
}
void MergeList(LinkList &La, LinkList &Lb) //
{
LNode *r, *pa=La->next, *pb=Lb->next;
La->next=NULL;
while(pa&&pb)
if( pa->data <= pb->data ){
r=pa->next;
pa->next=La->next;
La->next=pa;
pa=r;
}else{
r=pb->next;
pb->next=La->next;
La->next=pb;
pb=r;
}
if(pa)
pb=pa;
while(pb){
r=pb->next;
pb->next=La->next;
La->next=pb;
pb=r;
}
while(La->next!=NULL){
La=La->next;
printf("%d ",La->data);
}
free(Lb);
}
void Same_Merge(LinkList A, LinkList B) //综合题第14题
{
LNode *p=A->next, *q=B->next, *r, *s;
LinkList C=(LinkList)malloc(sizeof(LNode));
r=C;
while( q!=NULL&&p!=NULL ){
if(p->datadata)
p=p->next;
else if(p->data>q->data)
q=q->next;
else{
s=(LNode*)malloc(sizeof(LNode));
s->data=p->data;
r->next=s;
r=s;
p=p->next;
q=q->next;
}
}
r->next=NULL;
while(C->next!=NULL){
C=C->next;
printf("%d ", C->data);
}
}
LinkList Intersection_Merge(LinkList A, LinkList B) //综合题第15题
{
LNode *pa=A->next, *pb=B->next, *pc=A, *u;
while( pa&&pb ){
if( pa->data == pb->data ){
pc->next=pa;
pc=pa;
pa=pa->next;
u=pb;
pb=pb->next;
free(u);
} else if( pa->datadata ){
u=pa;
pa=pa->next;
free(u);
} else{
u=pb;
pb=pb->next;
free(u);
}
}
while(pa){
u=pa;
pa=pa->next;
free(u);
}
while(pb){
u=pb;
pb=pb->next;
free(u);
}
pc->next=NULL;
free(B);
return A;
}
int Pattern(LinkList A, LinkList B) //综合题第十六题
{
LNode *p=A, *pre=p, *q=B;
while(p&&q)
if(p->data==q->data){
p=p->next;
q=q->next;
}else{
pre=pre->next;
p=pre;
q=B;
}
if(q==NULL)
return 1;
else
return 0;
}
int main(){
int x, i, n, a, b;
LNode *L, *A, *B, *s, *p;
show();
printf("输入菜单序号:");
scanf("%d", &n);
while(n!=0){
if( n==1 ){
printf("输入数字序列 ,以9999结束:");
L = List_HeadInsert(L);
}
if( n==2 ){
printf("输入数字序列 ,以9999结束:");
L = List_TailInsert(L);
}
if( n==3 ){
printf("\n输入查找的序号:");
scanf("%d", &x);
B = GetElem(L, x);
printf("序号%d的数字为%d\n",x, B->data);
}
if( n==4 ){
scanf("%d", &x);
i = LocateElem(L, x);
if(i!=0)
printf("元素%d在第%d个位置.\n",x, i+1);
if(i==0)
printf("元素不在链表中。\n");
}
if( n==5 ){
printf("输入插入的位置,元素:\n");
scanf("%d %d", &i, &x);
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
Insert(L, s, i);
}
if( n==6 ){
printf("输入要删除的结点位置:");
scanf("%d", &x);
ListDelete(L, x);
}
if( n==7 ){
LNode *u=L;
while(u->next!=NULL){
u=u->next;
printf("%d ", u->data);
}
}
if( n==8 ){ //不带头结点
printf("输入要删除的元素:");
scanf("%d", &x);
Del_X_3(L, x);
}
if( n==9 ){
printf("输入要删除的元素:");
scanf("%d", &x);
Del_X_2(L,x);
}
if( n==10 ){
printf("输入要删除的元素:");
scanf("%d", &x);
Del_X_1(L,x);
}
if( n==11 ){
Print(L);
}
if( n==12 ){
Del_Min(L);
}
if( n==13 ){
//Reverse_1(L);//第二种方法
Reverse_2(L);
}
if( n==14 ){
Sort(L);
}
if( n==15 ){
printf("输入两个固定值");
scanf("%d %d", &a, &b);
Del_deter( L, a, b );
}
if( n==16 ){
Min_Delete(L);
}
if( n==17 ){
Div_List( L );
}
if( n==18 ){
B = DisCreat_1(L);
while(B->next!=NULL){
B=B->next;
printf("%d ", B->data);
} printf("\n");
while(L->next!=NULL){
L=L->next;
printf("%d ",L->data);
}
}
if( n==19 ){
B=DIsCreat_2(L);
printf("aaaa\n");
}
if( n==20 ){
Del_same(L);
}
if( n==21 ){
Del_Same(L);
}
if( n==22 ){
printf("请输入第一行数列:");
A=List_TailInsert(L);
printf("请输入第二行数列:");
B=List_TailInsert(L);
L = Merger(A, B);
printf("合并后链表:");
while(L->next!=NULL){
L=L->next;
printf("%d ",L->data);
}
}
if( n==23 ){
printf("请输入第一行数列:");
A=List_TailInsert(L);
printf("请输入第二行数列:");
B=List_TailInsert(L);
MergeList(A, B);
}
if( n==24 ){
printf("请输入第一行数列:");
A=List_TailInsert(L);
printf("请输入第二行数列:");
B=List_TailInsert(L);
Same_Merge(A, B);
}
if( n==25 ){
printf("请输入第一行数列:");
A=List_TailInsert(L);
printf("请输入第二行数列:");
B=List_TailInsert(L);
L=Intersection_Merge(A , B);
printf("合并后链表:");
while(L->next!=NULL){
L=L->next;
printf("%d ",L->data);
}
}
if( n==26 ){
printf("请输入第一行数列:");
A=List_TailInsert(L);
printf("请输入第二行数列:");
B=List_TailInsert(L);
x=Pattern(A,B);
if(x)
printf("1\n");
else
printf("0\n");
}
printf("\n输入菜单序号:");
scanf("%d", &n);
}
return 0;
}
void show(){
printf("\n");
printf(" |------------------------------链表---------------------------|\n");
printf(" | 0.退出 22.倒序合并 ① |\n");
printf(" | 1.头插法建立链表 23.倒序合并 ② |\n");
printf(" | 2.尾插法建立链表 24.交集合并(新链) |\n");
printf(" | 3.序号查找 25.交集合并(旧链) |\n");
printf(" | 4.元素查找 26.判断 |\n");
printf(" | 5.插入元素 |\n");
printf(" | 6.删除元素 |\n");
printf(" | 7.显示元素 |\n");
printf(" | 8.删除指定值(递归) N |\n"); //8.不带头结点
printf(" | 9.删除指定值 Y① |\n"); //带头结点
printf(" | 10.删除指定值 Y② |\n"); //带头结点
printf(" | 11.反向输出链表 |\n"); //带头结点
printf(" | 12.删除最小值 |\n"); //带头结点
printf(" | 13.逆转链表 |\n"); //带头结点
printf(" | 14.链表排序 |\n"); //带头结点
printf(" | 15.删除值域 |\n"); //带头结点
printf(" | 16.递增输出删除 |\n"); //带头结点
printf(" | 17.奇偶数分链 |\n"); //带头结点
printf(" | 18.奇偶序号分链 |\n"); //带头结点
printf(" | 19.正倒序分链 |\n"); //带头结点
printf(" | 20.删除相同元素 ① |\n"); //带头结点
printf(" | 21.删除相同元素 ② |\n"); //带头结点
printf(" |-------------------------------------------------------------|\n");
}
一边刷课本习题一边更新。