算法介绍本篇代码中常见的
为什么有三种 请先阅读这一部分这个是重点 需看 代码实现
下面的只可以在 DevC win10 64位运行在这个可以在 VS 运行也可在 DevC 运行最近一次更改 我的代码 需要的 直接看这一个 仅供参考 有错误望指出
算法介绍
对线性表进行:
创建 initList 追加 appendList 遍历 traverseList 插入 insertList 删除 deleteList 排序 sortList 反转 inversionList
本篇代码中常见的 ???
为什么有三种? 请先阅读这一部分
最初是在 DevC++上写的成功后发现 不能在 VS2010上运行 所以做了改进 可以在 vs2010 运行, 后来发现我写的 代码规范 和 严太太所编书上的 不太相同 所以又做了改进, 也就是 最下边 那个 所以 请直接看 目录—>3.3 !!!
这个是重点, 需看
因为 习惯问题, 所有的函数 采用了 驼峰命名法 (严太太书上 的 都是所有单词的 首字母大写) 在 void 类型的函数中 同样也写了 return 语句
原因: 即使函数无返回值, 也要加上 return语句, 以表明, 此函数体的代码 到此结束结束
测试时 基本都是 用的 int 类型
其他的 请自行更改相应代码, 更改为 其他类型, 例如 char 类型, 请注意 要把 输出的格式 要更改
代码实现
下面的只可以在 DevC++ (win10 64位)运行
写的 不太规范, 但主要算法都已实现
#include <stdio.h>
#include <stdbool.h>
#include <malloc.h>
#define LISTLENGTH 6
#define LISTINCREMENT 6
typedef char ElemType;
typedef struct
{
int cnt;
int len;
ElemType *pBase;
} LIST;
bool initList(LIST *pList);
bool appendList(LIST *pList, ElemType val);
bool isFull(LIST *pList);
bool isEmpty(LIST *pList);
void showList(LIST *pList);
bool insertList(LIST *pList,
int pos, ElemType insert_val);
bool deleteList(LIST *pList,
int pos, ElemType *pDelete_val);
bool inverseList( LIST *pList);
int main()
{
LIST
list;
ElemType delete_val;
initList(&
list);
appendList(&
list,
'a');
appendList(&
list,
'b');
appendList(&
list,
'c');
appendList(&
list,
'd');
appendList(&
list,
'e');
appendList(&
list,
'f');
showList(&
list);
if( insertList(&
list,
6,
'w'))
{
printf(
"插入成功!!\n显示: ");
showList(&
list);
}
if( deleteList(&
list,
6, &delete_val))
{
printf(
"删除成功, 你删除的值为: %c\n删除后的线性表为: ", delete_val);
showList(&
list);
}
if(inverseList(&
list)){
printf(
"反转成功!!\n显示: ");
showList(&
list);
}
return 0;
}
bool inverseList( LIST *pList){
int i =
0;
int j = pList->cnt -
1;
ElemType temp;
if( isEmpty(pList) ){
printf(
"线性表为空,反转失败!!\n");
return false;
}
while(i < j){
temp = pList->pBase[i];
pList->pBase[i] = pList->pBase[j];
pList->pBase[j] = temp;
i++;
j--;
}
return true;
}
bool deleteList(LIST *pList,
int pos, ElemType *pDelete_val)
{
int i;
if(pos <
0 || pos > pList->cnt)
{
printf(
"输入的位置无效,删除失败!!\n");
return false;
}
if(isEmpty(pList))
{
printf(
"线性表为空,删除失败!!\n");
return false;
}
*pDelete_val = pList->pBase[pos-
1];
for(i = pos-
1; i < pList->cnt; i++)
{
pList->pBase[i] = pList->pBase[i+
1];
}
(pList->cnt)--;
return true;
}
bool insertList(LIST *pList,
int pos, ElemType insert_val)
{
int i;
if(pos <
0 || pos > pList->cnt+
1)
{
printf(
"输入的位置无效,插入失败!!\n");
return false;
}
if( pList->cnt >= pList->len)
{
ElemType * pNewBase = (ElemType *)realloc(pList->pBase,
sizeof(ElemType)*LISTINCREMENT);
if(pNewBase == NULL)
{
printf(
"内存分配失败!!--线性表自增长失败--插入失败\n");
return false;
}
pList->pBase = pNewBase;
pList->len += LISTINCREMENT;
}
for(i = pList->cnt; i >= pos; i--)
pList->pBase[i] = pList->pBase[i-
1];
pList->pBase[pos-
1] = insert_val;
(pList->cnt)++;
return true;
}
void showList(LIST *pList)
{
int i =
0;
while(i < pList->cnt)
{
printf(
"%c ", pList->pBase[i]);
i++;
}
printf(
"\n");
return;
}
bool isEmpty(LIST *pList)
{
if(pList->cnt ==
0)
return true;
else
return false;
}
bool isFull(LIST *pList){
if(pList->len == pList->cnt)
return true;
else
return false;
}
bool appendList(LIST *pList, ElemType val)
{
if(isFull(pList)){
printf(
"线性表已满!!\n");
return false;
}
else{
pList->pBase[pList->cnt] = val;
(pList->cnt)++;
printf(
"追加成功!!\n");
return true;
}
}
bool initList(LIST *pList)
{
pList->pBase = (ElemType *)
malloc(
sizeof(ElemType) * LISTLENGTH);
if(pList != NULL){
pList->cnt =
0;
pList->len = LISTLENGTH;
return true;
}
else{
printf(
"分配失败!\n");
return false;
}
}
在这个可以在 VS 运行(也可在 DevC++ 运行)
#include <stdio.h>
#include <malloc.h>
#define LISTLENGTH 6
#define LISTINCREMENT 6
#define true 1
#define false 0
typedef int boolean;
typedef char ElemType;
typedef struct
{
int cnt;
int len;
ElemType *pBase;
} LIST;
boolean initList(LIST *pList);
boolean appendList(LIST *pList, ElemType val);
boolean isFull(LIST *pList);
boolean isEmpty(LIST *pList);
void showList(LIST *pList);
boolean insertList(LIST *pList,
int pos, ElemType insert_val);
boolean deleteList(LIST *pList,
int pos, ElemType *pDelete_val);
boolean inverseList( LIST *pList);
int main()
{
LIST
list;
ElemType delete_val;
initList(&
list);
appendList(&
list,
'a');
appendList(&
list,
'b');
appendList(&
list,
'c');
appendList(&
list,
'd');
appendList(&
list,
'e');
appendList(&
list,
'f');
showList(&
list);
if( insertList(&
list,
6,
'w'))
{
printf(
"插入成功!!\n显示: ");
showList(&
list);
}
if( deleteList(&
list,
6, &delete_val))
{
printf(
"删除成功, 你删除的值为: %c\n删除后的线性表为: ", delete_val);
showList(&
list);
}
if(inverseList(&
list)){
printf(
"反转成功!!\n显示: ");
showList(&
list);
}
return 0;
}
boolean inverseList( LIST *pList){
int i =
0;
int j = pList->cnt -
1;
ElemType temp;
if( isEmpty(pList) ){
printf(
"线性表为空,反转失败!!\n");
return false;
}
while(i < j){
temp = pList->pBase[i];
pList->pBase[i] = pList->pBase[j];
pList->pBase[j] = temp;
i++;
j--;
}
return true;
}
boolean deleteList(LIST *pList,
int pos, ElemType *pDelete_val)
{
int i;
if(pos <
0 || pos > pList->cnt)
{
printf(
"输入的位置无效,删除失败!!\n");
return false;
}
if(isEmpty(pList))
{
printf(
"线性表为空,删除失败!!\n");
return false;
}
*pDelete_val = pList->pBase[pos-
1];
for(i = pos-
1; i < pList->cnt; i++)
{
pList->pBase[i] = pList->pBase[i+
1];
}
(pList->cnt)--;
return true;
}
boolean insertList(LIST *pList,
int pos, ElemType insert_val)
{
int i;
if(pos <
0 || pos > pList->cnt+
1)
{
printf(
"输入的位置无效,插入失败!!\n");
return false;
}
if( pList->cnt >= pList->len)
{
ElemType * pNewBase = (ElemType *)realloc(pList->pBase,
sizeof(ElemType)*LISTINCREMENT);
if(pNewBase == NULL)
{
printf(
"内存分配失败!!--线性表自增长失败--插入失败\n");
return false;
}
pList->pBase = pNewBase;
pList->len += LISTINCREMENT;
}
for(i = pList->cnt; i >= pos; i--)
pList->pBase[i] = pList->pBase[i-
1];
pList->pBase[pos-
1] = insert_val;
(pList->cnt)++;
return true;
}
void showList(LIST *pList)
{
int i =
0;
while(i < pList->cnt)
{
printf(
"%c ", pList->pBase[i]);
i++;
}
printf(
"\n");
return;
}
boolean isEmpty(LIST *pList)
{
if(pList->cnt ==
0)
return true;
else
return false;
}
boolean isFull(LIST *pList){
if(pList->len == pList->cnt)
return true;
else
return false;
}
boolean appendList(LIST *pList, ElemType val)
{
if(isFull(pList)){
printf(
"线性表已满!!\n");
return false;
}
else{
pList->pBase[pList->cnt] = val;
(pList->cnt)++;
printf(
"追加成功!!\n");
return true;
}
}
boolean initList(LIST *pList)
{
pList->pBase = (ElemType *)
malloc(
sizeof(ElemType) * LISTLENGTH);
if(pList != NULL){
pList->cnt =
0;
pList->len = LISTLENGTH;
return true;
}
else{
printf(
"分配失败!\n");
return false;
}
}
-------------------------2016年9月6日-------------------------
最近一次更改 我的代码, 需要的 直接看这一个
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LISTSIZE 6
#define LISTINCREMENT 6
#define false 0
#define true 1
typedef int Status;
typedef int ElemType;
typedef struct{
ElemType *pBase;
int cnt;
int len;
} *PLIST, LIST;
void initList(PLIST plist);
void appendList(PLIST plist, ElemType e);
Status listEmpty(PLIST plist);
void traverseList( PLIST pList);
Status insertList(PLIST pList,
int pos, ElemType e);
Status deleteList(PLIST pList,
int pos, ElemType *e);
void sortList(PLIST pList);
void inversionList(PLIST pList);
int main(){
LIST
list;
ElemType delete_e;
initList(&
list);
appendList(&
list,
12);
appendList(&
list,
11);
appendList(&
list,
31);
appendList(&
list,
4);
appendList(&
list,
53);
appendList(&
list,
612);
traverseList(&
list);
if( insertList(&
list,
6,
99) ){
printf(
"插入成功!!!\n");
traverseList(&
list);
}
else{
printf(
"插入失败!!!\n");
}
if( deleteList(&
list,
6, &delete_e) ){
printf(
"删除成功!!! 删除的元素的值为: %d\n删除元素后的表为:\n", delete_e);
traverseList(&
list);
}
else{
printf(
"删除失败!!!\n");
}
sortList(&
list);
traverseList(&
list);
inversionList(&
list);
traverseList(&
list);
return 0;
}
void inversionList(PLIST pList){
int i =
0;
int j = pList->cnt-
1;
ElemType temp;
if(pList->cnt <
2){
return;
}
while(i < j){
temp = pList->pBase[i];
pList->pBase[i] = pList->pBase[j];
pList->pBase[j] = temp;
++i;
--j;
}
return ;
}
void sortList(PLIST pList){
int i, j;
ElemType temp;
if( pList->cnt <
2){
return ;
}
for(i =
0; i < pList->cnt; i++){
for(j = i+
1; j <= pList->cnt -
1; ++j ){
if(pList->pBase[i] > pList->pBase[j]){
temp = pList->pBase[j];
pList->pBase[j] = pList->pBase[i];
pList->pBase[i] = temp;
}
}
}
return;
}
Status deleteList(PLIST pList,
int pos, ElemType *e){
if(pos <
1 && pos > pList->cnt){
printf(
"删除位置无效!!!\n");
return false;
}
*e = pList->pBase[pos-
1];
for(; pos < pList->cnt; pos++){
pList->pBase[pos-
1] = pList->pBase[pos];
}
(pList->cnt)--;
return true;
}
Status insertList(PLIST pList,
int pos, ElemType e){
int i;
if(pos <
1 && pos > pList->cnt +
1){
printf(
"插入位置无效!!!\n");
return false;
}
if(pList->cnt >= pList->len){
ElemType *newBase;
newBase = (ElemType *)realloc(pList->pBase,
sizeof(ElemType) * (pList->len + LISTINCREMENT));
if(newBase == NULL){
printf(
"线性表自动增长: 内存分配失败!!\n程序终止........");
exit(-
1);
}
pList->pBase = newBase;
pList->len += LISTINCREMENT;
}
for(i = pList->cnt; i >= pos ; --i){
pList->pBase[i] = pList->pBase[i-
1];
}
pList->pBase[pos-
1] = e;
++(pList->cnt);
return true;
}
void traverseList( PLIST pList){
int i;
if( listEmpty(pList) ){
return;
}
for(i =
0; i < pList->cnt; i++){
if(i != pList->cnt -
1)
printf(
"%d ", pList->pBase[i]);
else
printf(
"%d\n\n", pList->pBase[i]);
}
return;
}
Status listEmpty(PLIST pList){
if(pList->cnt ==
0){
printf(
"线性表: 为空!!!!!!\n");
return true;
}
return false;
}
void appendList(PLIST pList, ElemType e){
if(pList->cnt == pList->len){
ElemType *newBase;
newBase = (ElemType *)realloc(pList->pBase,
sizeof(ElemType)*(pList->len + LISTINCREMENT));
if(newBase == NULL){
printf(
"线性表自动增长: 内存分配失败!!\n程序终止........");
exit(-
1);
}
pList->pBase = newBase;
pList->len += LISTINCREMENT;
}
pList->pBase[pList->cnt] = e;
++(pList->cnt);
return;
}
void initList(PLIST plist){
plist->pBase = (ElemType *)
malloc(
sizeof(ElemType) * LISTSIZE);
if(plist->pBase == NULL){
printf(
"初始线性表: 内存分配失败!!\n程序终止........");
exit(-
1);
}
else{
plist->cnt =
0;
plist->len = LISTSIZE;
}
return ;
}
仅供参考 ,有错误望指出.
有大神路过请指点一下。 菜鸟求飞 !!! 有什么疑问 也可以在 下边 提问 ! (有点托大了)或者发邮件 E-Mail:ppbboddqq.qq@qq.com