#include#include#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef struct {
int integer;
}ElemType;
typedef struct {
ElemType* elem;
int length;
int listsize;
}SqList;
Status DeletK(SqList* a, int i, int k)
{
//本过程从顺序存储结构的线性表a中删除第i个元素起的k个元素
if (i< 1 || k<0 || i + k>a->length) {
printf("parameters infeasible\n");
return INFEASIBLE;
}
else {
for (int j = i + k; j<= a->length; j++, i++)
{
a->elem[i - 1] = a->elem[i + k - 1];
}
a->length -= k;
}
return OK;
}
int main()
{
SqList a = {
(ElemType*)malloc(LIST_INIT_SIZE * sizeof(SqList)),
LIST_INIT_SIZE,
LIST_INIT_SIZE
};
for (int i = 0; i< a.length; i++)
{
a.elem[i].integer = i + 1;
printf("a.elem[%d]=%d ", i, a.elem[i].integer);
if ((i + 1) % 10 == 0)putchar('\n');
};
putchar('\n');
DeletK(&a, 22, 79);
for (int i = 0; i< a.length; i++)
{
printf("a.elem[%d]=%d ", i, a.elem[i].integer);
if ((i + 1) % 10 == 0)putchar('\n');
}
return 0;
}
2.12 比较顺序表A,B大小(不破坏原表,不一定先求得A撇B撇)typedef struct {
char integer;
}ElemType;
typedef struct {
ElemType* elem;
int length;
int listsize;
}SqList;
typedef enum{bigger=1,equal,smaller}comp_result;
comp_result SqListComp(SqList a, SqList b)
{
//返回表a与表b的大小关系,a在前
ElemType* pSqListA, * pSqListB;
pSqListA = a.elem; pSqListB = b.elem;
int count_common_elem=0;
for (; pSqListA->integer != pSqListB->integer; pSqListA++, pSqListB++)count_common_elem++;
if (a.length - count_common_elem == 0 && b.length - count_common_elem == 0)return equal;
else if (a.length - count_common_elem == 0 && b.length - count_common_elem != 0)return smaller;
else if (a.length - count_common_elem != 0 && b.length - count_common_elem != 0)
{
if (a.elem[count_common_elem].integer< b.elem[count_common_elem].integer)
return smaller;
}
else return bigger;
return 0;
}
2.13 试写一算法在带头节点的带链表结构上实现线性表操作LOCATE(L,X)。typedef struct {
int integer;
}ElementType;
typedef struct LNode{
ElementType elem;
struct LNode* next;
}LNode,*LinkList;
Status compare(ElementType List_elem, ElementType x);//比较两元素,相同返回OK,不同返回ERROR
int LOCATE(LinkList L, ElementType x, Status (* compare)(ElementType ,ElementType ))
{
//在带头结点的链表L中查找数据元素x,如有,返回结点位序,如无,返回0
if (!L)return INFEASIBLE;
LinkList pLNode = L; pLNode++;
for (int i = 1; pLNode != NULL; pLNode++)
{
if (compare(pLNode->elem, x) == OK)return i;
}
return 0;
}
2.14 试写一算法在带头节点的单链表结构上实现线性表操作LENGTH(L)。typedef struct {
int integer;
}ElementType;
typedef struct LNode{
ElementType data;
struct LNode* next;
}LNode,*LinkList;
int LENGTH(LinkList L)
{
if (!L)return INFEASIBLE;
LinkList p = L;
int i = 0;
while (p->next)
{
p = p->next;
i++;
}
return i;
}
int main()
{
LinkList l = (LinkList)malloc(sizeof(LNode));
LinkList l1 = (LinkList)malloc(sizeof(LNode));
l1->next = NULL;
LinkList p = l;
int i = 0;
for (i = 0; i< 10; i ++ )
{
p->next = (LinkList)malloc(sizeof(LNode));
p = p->next;
if (!p)exit(0);
p->data.integer = i+1;
printf("node%d=%d\n", i+1, p->data.integer);
}
p->next = NULL;
printf("length of l built is %d\n", i);
int len_l, len_l1;
printf("length of l is %d, length of l1 is %d", len_l=LENGTH(l), len_l1=LENGTH(l1));
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前名称:数据结构题集(c语言版)第2章:线性表-创新互联
转载源于:https://www.cdcxhl.com/article28/cscjjp.html
成都网站建设公司_创新互联,为您提供面包屑导航、服务器托管、App设计、品牌网站制作、动态网站、关键词优化
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联