jsp网站开发技巧,网站注册搜索引擎的目的是,网站错误代码301,虚拟主机商废话不多说#xff0c;数据结构自己写代码见识了太多的bug#xff0c;看来还是自己写代码的功夫不到家啊#xff0c;进入正题。直接上代码。
#include stdio.h
#include stdlib.h
#define MAXSIZE 100
#define ERROR 0
#define OK 1
typedef int ElemType;…废话不多说数据结构自己写代码见识了太多的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.elemnew ElemType[MAXSIZE];if(!L.elem)return false;L.length0;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(num9999)break;L.elem[i]num;L.length;}printf(Successfully set SqList.\n);printf(Your SqList is:\n);for(int i0;iL.length;i)printf(%d ,L.elem[i]);printf(\n);
}
//算法2.4
ElemType ListInsert_Sq(SqList L, int i, ElemType e)
{ // 在顺序线性表L的第i个位置之前插入新的元素e// i的合法值为1iListLength_Sq(L) 1if(i1 || iL.length1) return ERROR; if(L.lengthMAXSIZE) return ERROR;for(int jL.length-1;ji-1;j--) L.elem[j1]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的合法值为1iListLength_Sq(L)if((i1)||(iL.length))return ERROR;eL.elem[i-1];for(int ji;jL.length;j){L.elem[j-1]L.elem[j];}--L.length;return OK;
}//算法 找Max
ElemType FindMax(SqList L)
{int MaxL.elem[0];for(int i0;iL.length;i){if(L.elem[i]Max)MaxL.elem[i];}return Max;
}
//算法 找Min
ElemType FindMin(SqList L)
{int MinL.elem[0];for(int i0;iL.length;i){if(L.elem[i]Min){MinL.elem[i];} }return Min;
}
//算法 删除所有值大于min而且小于max的元素
void DeleteNumbers(SqList L,ElemType Max,ElemType Min)
{ElemType element;int k0;while(kL.length-1){ //当kL.length-1时说明删完了if(L.elem[k]Min L.elem[k]Max){ListDelete_Sq(L,k1,element); //K是数组下标而删除的是以1开头的下标所以加1}else{k;}}for(int i0;iL.length;i)printf(%d ,L.elem[i]);printf(\n);
}
int main()
{SqList L; //构造一个线性表ElemType ret;retInitList_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);retListInsert_Sq(L,position,number);if(ret){printf(Successfully insert SqList.\n);printf(After inserting,your SqList is:\n);for(int i0;iL.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;retListDelete_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 i0;iL.length;i)printf(%d ,L.elem[i]);printf(\n); }elseprintf(Failed delete SqList.\n);ElemType MaxFindMax(L); //最大值printf(The max number is: %d\n,Max);ElemType MinFindMin(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 Input0 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需要。
还有一点需要注意的是循环的终止条件是什么中止条件就是当下标1Length后才能中止不能是大于等于可能会漏掉最后一个不符合条件的数。
还有一点就是删除所有值大于min而且小于max的元素的判断条件应该怎么写。应该是
L.elem[k]Min L.elem[k]Max
或者
L.elem[k]!Min L.elem[k]!Max
不能用或——||