数据结构题集(c语言版)第2章:线性表-创新互联
2.10 从顺序表中a中删除第i个元素起的k个元素
文章标题:数据结构题集(c语言版)第2章:线性表-创新互联
URL地址:http://lswzjz.com/article/cscjjp.html
#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章:线性表-创新互联
URL地址:http://lswzjz.com/article/cscjjp.html