顺序链表

本文介绍了一个使用C++实现的顺序表类SqList的基本操作,包括初始化、插入元素等,并展示了如何通过这些操作来构建并操作一个顺序表实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

> #include "StdAfx.h"
> #include <stdio.h>
> #include <string>
> #include <iostream>
> 
> using namespace std; //InitList() 
> typedef string Elem;
> #define Maxlenght 10
>class SqList{ 
> public:   
>   bool initSqlist();      
>   bool insertSqlist(int position, Elem p);    
>   Elem GetElem(int position);     
>   int GetLength();    
>   int  GetmaxLength();    
>   int  GetLocalElem(const Elem value);    
>   Elem  GetPreviousElem(Elem value);  
>   Elem GetNextElem(Elem value);   
>   bool EnLargeList();     
>   bool ClearList();
> private:  
     unsigned int length; 
>    unsigned int maxLength; 
> }; 
> bool SqList::initSqlist() 
> {    
>   p=new Elem[Maxlenght];    
>   length=0;
>   maxLength=Maxlenght;
>   if(p!=nullptr) return true;    else return false;    
> }; 
> bool SqList::insertSqlist(int position,Elem value) 
> {
>   if(length+1>maxLength)  
>   { EnLargeList();    }   
>   if (position<0 )return false; 
>   else 
>   {       
>      for(int j=length-1;j>=position;--j)      
>      {
>        p[j+1]=p[j];           
>        cout<<"["<<j<<"]"<<p[j]<<endl;         
>       }   
>   }
>   p[position]=value;
>   ++length;       
>   return true; 
> }; 
> Elem SqList::GetElem(int position) 
> {     if(position <maxLength)     return p[position]; 
> }; 
> int SqList::GetLength() {     return length; }; 
> int SqList::GetmaxLength() 
> {return maxLength; } 
> int  SqList::GetLocalElem(const Elem value) 
> {
>   for(int i=0;i<=length-1;i++) 
>   {   if (p[i]==value)return i;
>       else
>          return -1;   
>   } 
> } 
> Elem SqList::GetPreviousElem(Elem value) 
> {     int i=GetLocalElem(value);  
>    if (i!=0 &&i !=-1) {   return p[i-1]; } 
> }
> Elem SqList::GetNextElem(Elem value) 
> {     int i=GetLocalElem(value);
>   if(i!=-1 &&i!=length-1)     
>   {     return p[i+1];    }
> } 
> bool SqList::EnLargeList() 
> {     
>  Elem *newp=new Elem[maxLength+newSize];
>   for(int i=0;i<=length-1;++i){       newp[i]=p[i];   }
>   if(!DestoryList())  
>   { return false;     } else{         
>      p=newp;
>     maxLength+=newSize;       
>      return true;     
>    }  
>    return false; 
>} 
> bool SqList::DestoryList() 
> { 
>    delete []p;    
>    p=NULL;
 }
int main() {
> 
>   SqList sql_a,sql_b,sql_c;   
>   sql_a.initSqlist();
>   sql_a.insertSqlist(0,"10");     
>   sql_a.insertSqlist(1,"40");
>   sql_a.insertSqlist(2,"20");     
>   sql_a.insertSqlist(2,"20");
>   sql_a.insertSqlist(3,"30");         
>   sql_a.insertSqlist(1,"50");
>   sql_a.insertSqlist(1,"60");     
>   sql_a.insertSqlist(1,"70");
>   sql_a.insertSqlist(1,"80");     
>   sql_a.insertSqlist(1,"90");
>   sql_a.insertSqlist(1,"100");    
>   sql_a.insertSqlist(1,"102");
> 
>   //cout<<value<<endl;
> 
>   cout<<"总长度为:"<<sql_a.GetLength()<<endl;     
>   for(int i=0;i<sql_a.GetLength();i++)    
>   { cout<<sql_a.GetElem(i)<<endl;     }
>   system("pause"); 
>   return 1; 
> }
在编程中,顺序链表通常指的是线性表的顺序存储结构,即使用数组来模拟链表的行为。虽然名称中包含“链表”,但其实现方式与真正的链表(如单链表、双向链表)不同。顺序链表本质上是通过数组实现的一种线性结构,具备动态扩容的能力[^3]。 ### 顺序链表的基本操作 顺序链表的核心操作包括: - 创建链表 - 插入元素 - 删除元素 - 获取元素 - 清空链表 - 获取长度等 ### 数据结构定义 为了实现顺序链表,首先需要定义一个结构体来保存链表的基本信息: ```c typedef struct { void** data; // 指向数据数组的指针 int capacity; // 当前最大容量 int length; // 当前实际长度 } SeqList; ``` ### 创建顺序链表 创建顺序链表的关键在于分配内存并初始化其基本属性: ```c SeqList* SeqList_Create(int capacity) { if (capacity <= 0) { return NULL; } SeqList* list = (SeqList*)malloc(sizeof(SeqList)); if (list == NULL) { return NULL; } list->data = (void**)malloc(capacity * sizeof(void*)); if (list->data == NULL) { free(list); return NULL; } list->capacity = capacity; list->length = 0; return list; } ``` ### 销毁顺序链表 销毁顺序链表时需要释放所有分配的内存: ```c void SeqList_Destroy(SeqList* list) { if (list != NULL) { free(list->data); free(list); } } ``` ### 插入元素 插入元素时需要注意检查是否需要扩容: ```c int SeqList_Insert(SeqList* list, void* data, int pos) { if (list == NULL || data == NULL || pos < 0 || pos > list->length) { return -1; } if (list->length >= list->capacity) { // 扩容逻辑,例如扩容为原来的两倍 int new_capacity = list->capacity * 2; void** new_data = (void**)realloc(list->data, new_capacity * sizeof(void*)); if (new_data == NULL) { return -1; } list->data = new_data; list->capacity = new_capacity; } for (int i = list->length; i > pos; i--) { list->data[i] = list->data[i - 1]; } list->data[pos] = data; list->length++; return 0; } ``` ### 获取元素 获取指定位置的元素: ```c void* SeqList_Get(SeqList* list, int pos) { if (list == NULL || pos < 0 || pos >= list->length) { return NULL; } return list->data[pos]; } ``` ### 删除元素 删除指定位置的元素: ```c int SeqList_Delete(SeqList* list, int pos) { if (list == NULL || pos < 0 || pos >= list->length) { return -1; } for (int i = pos; i < list->length - 1; i++) { list->data[i] = list->data[i + 1]; } list->length--; return 0; } ``` ### 获取长度 返回当前顺序链表的长度: ```c int SeqList_Length(SeqList* list) { if (list == NULL) { return -1; } return list->length; } ``` ### 清空链表链表清空: ```c void SeqList_Clear(SeqList* list) { if (list != NULL) { list->length = 0; } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值