您的位置:首页 > 编程语言 > C语言/C++

C++顺序存储的线性表的代码

2020-02-16 17:57 204 查看

内容过程中中,把内容过程中较好的内容段做个珍藏,如下内容段是关于C++顺序存储的线性表的内容,希望对各位有所帮助。

#include <stdio.h>

#define OK 1
#define ERR 0

#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
#define ElemType char

typedef struct {
} SqList;

{
if(NULL == aList->elem)return ERR;
aList->listSize = LIST_INIT_SIZE;
aList->length = 0;
return OK;
}

{
int i;
if( index<0 || index>aList->length ){
printf("Error : Insert %c to wrong position %dn",e,index);
}
if( aList->listSize <= aList->length )
{
if(NULL == aList->elem) return ERR;
aList->listSize += LIST_INCREMENT;
}

printf("Insert %c to position %dn",e,index );
i = aList->length;
while(i>index)
{
aList->elem[i] = aList->elem[i-1];
i--;
}

aList->elem[index] = e;
aList->length++;

return OK;
}

{
int i = index;
if(i<0 || i>=aList->length)return ERR;

i = index;
while(i<aList->length-1)
{
aList->elem[i] = aList->elem[i+1];
i++;
}
aList->length--;

return OK;
}

int ListTraverse(SqList aList)
{
int i=0;
while(i < aList.length){
printf("%ct",aList.elem[i]);
i++;
}
printf("n");
return OK;
}

{
SqList aList ;
char element;
int position;

InitList( &aList );

while(1){
printf("Please input element and position to insert in (input ; to quit) :n");
scanf("%1s,%d",&element,&position);
if(';' == element) break;
ListInsert(&aList,element,position);
}

ListDelete(&aList,&element,0);

ListTraverse(aList);

printf("length :%d . n",aList.length);
return OK;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
chanxuzhai9442 发布了0 篇原创文章 · 获赞 0 · 访问量 136 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: