【第三周项目2】 建设“顺序表”算法库

/*      
Copyright (c)2017,烟台大学计算机与控制工程学院      
All rights reserved.      
文件名称:5.cpp      
作    者:尚文哲 
完成日期:2017年9月17日      
 
      
问题描述: 领会“0207将算法变程序”部分建议的方法,建设自己的专业基础设施算法库。  
           这一周,建的是顺序表的算法库。算法库包括两个文件:   
         1.头文件:list.h,包含定义顺序表数据结构的代码、宏定义、要实现算法的函数的声明;  
           2.源文件:list.cpp,包含实现各种算法的函数的定义   
         请采用程序的多文件组织形式,在项目1的基础上,建立如上的两个文件,另外再建立一个源文件,  
           编制main函数,完成相关的测试工作。请完成后,发布博文,展示你的实践成果。   
输入描述: 若干数据 。    
程序输出: 1.线性表的长度。   
           2.第3个元素及其值的大小。     
           3.8在表中的第几位。  
           4.删除的元素。  
           5.删除后的线性表。  
           6.删除线性表。  
*/     

#include<stdio.h>  
#include<malloc.h>  
#define MaxSize 50  
typedef int ElemType;  
typedef struct  
{  
    ElemType data[MaxSize];  
    int length;  
}SqList;  
void CreateList(SqList *&L, ElemType a[], int n);  
void DispList(SqList *L);  
bool ListEmpty(SqList *L);  
int ListLength(SqList *L);  
bool GetElem(SqList*L,int i,ElemType &e);  
int LocateElem(SqList *L, ElemType e);  
  
int ListLength(SqList *L)  
{  
    return (L->length);  
}  
bool GetElem(SqList*L,int i,ElemType &e)  
{  
    if(i<1||i>L->length)  
        return false;  
    e=L->data[i-1];  
    return true;  
}  
int LocateElem(SqList *L, ElemType e)  
{  
    int i=0;  
    while (i<L->length && L->data[i]!=e) i++;  
    if (i>=L->length)  
        return 0;  
    else  
        return i+1;  
}  
void CreateList(SqList *&L,ElemType a[],int n)  
{  
    int i;  
  
    L=(SqList *)malloc(sizeof(SqList));  
  
  
    for(i=0;i<n;i++)  
        L->data[i]=a[i];  
    L->length=n;  
}  
void DispList(SqList *L)  
{  
    int i;  
    if (ListEmpty(L))  
        return;  
    for (i=0; i<L->length; i++)  
        printf("%d ",L->data[i]);  
    printf("\n");  
}  
  
  
bool ListEmpty(SqList *L)  
{  
    return (L->length==0);  
}  
  
   
  
void InitList(SqList *&L);  
void DestroyList(SqList *&L);  
bool ListInsert(SqList *&L,int i,ElemType e);  
bool ListDelete(SqList *&L,int i,ElemType &e);  
void InitList(SqList *&L)  
{  
L=(SqList *)malloc(sizeof(SqList));  
L->length=0;  
}  
void DestroyList(SqList *&L)  
{  
free(L);  
}  
bool ListInsert(SqList *&L,int i,ElemType e)  
{  
int j;  
if(i<1||i>L->length+1)  
    return false;  
i--;  
for(j=L->length;j>i;j--)  
    L->data[j]=L->data[j-1];  
L->data[i]=e;  
L->length++;  
return true;  
}  
bool ListDelete(SqList *&L,int i,ElemType e)  
{  
  int j;  
if(i<1||i>L->length+1)  
    return false;  
i--;  
e=L->data[i];  
for(j=i;j<L->length-1;j++)  
    L->data[j]=L->data[j+1];  
  
L->length--;  
return true;  
}  
  
int main()  
{  
    SqList *sq;  
    InitList(sq);  
    printf("构造的新的:\n");  
    DispList(sq);  
    ListInsert(sq, 1, 5);  
    ListInsert(sq, 2, 3);  
    ListInsert(sq, 1, 4);  
    printf("插入后的:\n");  
    DispList(sq);  
    ListDelete(sq, 1, 5);  
    ListDelete(sq, 2, 3);  
    printf("删除后的:\n");  
    DispList(sq);  
  
    DestroyList(sq);  
    printf("销毁后的:\n");  
    DispList(sq);  
    return 0;  
  
} 




顺序表是一种基于数组的数据结构,它按照元素在内存中的存储顺序来组织数据。为了创建一个顺序表算法,我们可以实现以下几个核心操作: 1. **初始化** (CreateList): 创建一个空的顺序表,给定表的大小。 ```c++ struct Node { int data; Node* next; // 指向下一个节点的指针 }; SequenceList* CreateList(int capacity) { SequenceList* list = new SequenceList(); list->size = 0; list->capacity = capacity; list->head = NULL; return list; } ``` 2. **插入** (Insert): 在指定位置插入新元素。如果需要动态扩容,需要处理边界情况。 ```c++ void Insert(SequenceList* list, int index, int value) { if (index < 0 || index > list->size) { printf("Error: Index out of bounds.\n"); return; } if (list->size == list->capacity) { ResizeList(list); } Node* newNode = new Node{value, list->head}; for (int i = 0; i < index - 1; ++i) { list->data[i] = list->data[i + 1]; } list->data[index] = newNode->data; list->size++; } ``` 3. **删除** (Delete): 删除指定索引处的元素。 ```c++ void Delete(SequenceList* list, int index) { if (index < 0 || index >= list->size) { printf("Error: Index out of bounds.\n"); return; } Node* temp = list->head; for (int i = 0; i < index; ++i) { temp = temp->next; } delete temp->next; list->data[index] = temp->data; list->size--; if (index == list->size && list->size == list->capacity / 4) { ShrinkList(list); } } ``` 4. **搜索** (Search): 查找指定值的元素。 ```c++ bool Search(SequenceList* list, int value) { Node* current = list->head; while (current != NULL) { if (current->data == value) { return true; } current = current->next; } return false; } ``` 5. **其他实用函数** (例如:获取长度、访问元素等) 注意:`ResizeList()` 和 `ShrinkList()` 函数用于调整列表容量以提高效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值