首页 / 算法 / PTA数据结构与算法中文-顺序表操作集
PTA数据结构与算法中文-顺序表操作集
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PTA数据结构与算法中文-顺序表操作集,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1657字,纯文字阅读大概需要3分钟。
内容图文
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 6
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
if ( Insert(L, X, 0)==false )
printf(" Insertion Error: %d is not in.\n", X);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else
printf("%d is at position %d.\n", X, P);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &P);
if ( Delete(L, P)==false )
printf(" Deletion Error.\n");
if ( Insert(L, 0, P)==false )
printf(" Insertion Error: 0 is not in.\n");
}
return 0;
}
List MakeEmpty(){
List list;
list = (List)malloc(sizeof(struct LNode));
list->Last = -1;
return list;
}
Position Find( List L, ElementType X ){
Position i;
for ( i=0; i<=L->Last; i++ ) {
if ( L->Data[i] == X)
return i;
}
return ERROR;
}
bool Insert( List L, ElementType X, Position P ){
if ( L->Last == MAXSIZE-1 ) {
printf("FULL");
return false;
}
if ( P<0 || P>L->Last+1 ) {
printf("ILLEGAL POSITION");
return false;
}
int j;
for ( j=L->Last; j>= P; j--){
L->Data[j+1] = L->Data[j];
}
L->Data[P] = X;
L->Last++;
return true;
}
bool Delete( List L, Position P ){
if ( P<0 || P>L->Last ) {
printf("POSITION %d EMPTY", P);
return false;
}
int i;
for ( i=P; i<L->Last; i++ ) {
L->Data[i] = L->Data[i+1];
}
L->Last--;
return true;
}
内容总结
以上是互联网集市为您收集整理的PTA数据结构与算法中文-顺序表操作集全部内容,希望文章能够帮你解决PTA数据结构与算法中文-顺序表操作集所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。