废话不多说,数据结构自己写代码见识了太多的bug,看来还是自己写代码的功夫不到家啊,进入正题。直接上代码。
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct {ElemType *elem;int length;
}SqList;
//算法2.3 构造一个空的线性表
bool InitList_Sq(SqList &L)
{L.elem=new ElemType[MAXSIZE];if(!L.elem)return false;L.length=0;return true;
}
//给顺序表赋值
void Set_SqList(SqList &L)
{int num;int i=-1;printf("Please enter some numbers,then enter 9999 to end.\n");while (scanf("%d",&num)){if(num==9999)break;L.elem[++i]=num;L.length++;}printf("Successfully set SqList.\n");printf("Your SqList is:\n");for(int i=0;i<L.length;i++)printf("%d ",L.elem[i]);printf("\n");
}
//算法2.4
ElemType ListInsert_Sq(SqList &L, int i, ElemType e)
{ // 在顺序线性表L的第i个位置之前插入新的元素e// i的合法值为1<=i<=ListLength_Sq(L) + 1if(i<1 || i>L.length+1) return ERROR; if(L.length>=MAXSIZE) return ERROR;for(int j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j];L.elem[i-1]=e;++L.length;return OK;
}
// 算法2.5
ElemType ListDelete_Sq(SqList &L,int i,ElemType &e)
{ // 在顺序线性表L中删除第i个元素, 并用e返回其值// i的合法值为1<=i<=ListLength_Sq(L)if((i<1)||(i>L.length))return ERROR;e=L.elem[i-1];for(int j=i;j<L.length;j++){L.elem[j-1]=L.elem[j];}--L.length;return OK;
}//算法 找Max
ElemType FindMax(SqList L)
{int Max=L.elem[0];for(int i=0;i<L.length;i++){if(L.elem[i]>Max)Max=L.elem[i];}return Max;
}
//算法 找Min
ElemType FindMin(SqList L)
{int Min=L.elem[0];for(int i=0;i<L.length;i++){if(L.elem[i]<Min){Min=L.elem[i];} }return Min;
}
//算法 删除所有值大于min而且小于max的元素
void DeleteNumbers(SqList &L,ElemType Max,ElemType Min)
{ElemType element;int k=0;while(k<=L.length-1){ //当k>L.length-1时,说明删完了if(L.elem[k]>Min && L.elem[k]<Max){ListDelete_Sq(L,k+1,element); //K是数组下标,而删除的是以1开头的下标,所以加1}else{k++;}}for(int i=0;i<L.length;i++)printf("%d ",L.elem[i]);printf("\n");
}
int main()
{SqList L; //构造一个线性表ElemType ret;ret=InitList_Sq(L); //构造一个空的线性表,并把返回值给retif(ret){printf("Successfully initialized SqList.\n");}elseprintf("Failed initialized SqList.\n"); Set_SqList(L); //给表赋值printf("What a number do you want to insert?\n");int number;scanf("%d",&number);printf("Where do you want to insert it?\n");int position;scanf("%d",&position);ret=ListInsert_Sq(L,position,number);if(ret){printf("Successfully insert SqList.\n");printf("After inserting,your SqList is:\n");for(int i=0;i<L.length;i++)printf("%d ",L.elem[i]);printf("\n");}elseprintf("Failed insert SqList.\n");printf("Which element in the SqList do you want to delete?\n");int position2;scanf("%d",&position2);ElemType element;ret=ListDelete_Sq(L,position2,element);if(ret){printf("Successfully delete SqList.\n");printf("The element you delete is: %d\n",element);printf("After deletting,your SqList is:\n");for(int i=0;i<L.length;i++)printf("%d ",L.elem[i]);printf("\n"); }elseprintf("Failed delete SqList.\n");ElemType Max=FindMax(L); //最大值printf("The max number is: %d\n",Max);ElemType Min=FindMin(L); //最小值printf("The min number is: %d\n",Min);printf("After delete the numbers between min and max,then the SqList is:\n");DeleteNumbers(L,Max,Min);return 0;/*Sample Input:0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0*/
}
输出
Successfully initialized SqList.
Please enter some numbers,then enter 9999 to end.
0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
9999
Successfully set SqList.
Your SqList is:
0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
What a number do you want to insert?
9999
Where do you want to insert it?
1
Successfully insert SqList.
After inserting,your SqList is:
9999 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
Which element in the SqList do you want to delete?
1
Successfully delete SqList.
The element you delete is: 9999
After deletting,your SqList is:
0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0
The max number is: 5
The min number is: 0
After delete the numbers between min and max,then the SqList is:
0 5 0 5 0--------------------------------
Process exited after 14.98 seconds with return value 0
请按任意键继续. . .
让我折腾了不少时间的bug就是——删除所有值大于min而且小于max的元素 的实现。删除顺序表中的元素,直接调用ListDelete_Sq()函数,既然是删除所有值大于min而且小于max的元素,那就需要遍历出所有值大于min而且小于max的元素。既然是遍历,自然会想到用for循环了,用for循环时,删除算法执行后,应该 i--;因为删除元素后,后面的元素移动到当前位置来了,需要再次判是否符合删除条件,如果符合,i才能++。也可以用while()循环,while循环的做法需要增加一条语句,if else 。if()里面的条件成立,执行删除算法。如果不成立,下标i需要++。
还有一点需要注意的是,循环的终止条件是什么,中止条件就是,当下标+1>Length后才能中止,不能是>=,大于等于可能会漏掉最后一个不符合条件的数。
还有一点,就是删除所有值大于min而且小于max的元素的判断条件应该怎么写。应该是
L.elem[k]>Min && L.elem[k]<Max
或者
L.elem[k]!=Min && L.elem[k]!=Max
不能用或——|| !!!