设计一个静态链表(或称为数组链表)

2024-03-20 21:58

本文主要是介绍设计一个静态链表(或称为数组链表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

设计一个静态链表(或称为数组链表),该链表储存的数据类型为string,请实现以下的功能。
public:
List(); //构造函数
List(int size); //含参构造函数-创建最大长度为size的静态链表
int Size() const; //返回链表的长度
bool IsFull() const; //返回链表是否已满
bool IsEmpty()const; //返回链表是否已空
void Clear(); //清空链表
int Retrieve( int position, string &x ) const; //获取链表第position位置的元素到x,成功返回0,否则返回-1
int Replace( int position, const string &x ); //将链表第position位置的元素换为x,成功返回0,否则返回-1
int Remove( int position, string &x ); //获取并删除链表第position位置的元素,成功返回0,否则返回-1
int Insert(int position, const string &x ); //将元素x插入到链表第position位置,成功返回0,否则返回-1

要求:
在实现要求的结构的基础上,需在main函数中编写下面所述的简单测试程序,请使用屏幕输入输出。
(1)按输入序列:Jan、Feb、Mar、Apr、May 建立初始链表。
(2)在Feb 之前,May 之后,先后插入Jun、Oct。
(3)先后删除Mar 和 Jan 。
(4)在Apr之前插入Dec。
在每一步执行后,输出链表状态。链表的状态请按照position:content:index输出。并输出当前链表。
注释:1. 第一个元素的Position的index为0;
2.插入元素操作Insert为在指定位置之前插入;

代码如下:

#include<iostream>
#include<string>
using namespace std;
typedef int index;
const int max_list=11;
class Node
{
public:
 string entry;
 index next;
};
class List
{
public:
    List(); //构造函数
 List(int size);//含参构造-创建最大长度为size的静态链表
    int Size() ; //返回链表的长度
    bool IsFull() const; //返回链表是否已满
    bool IsEmpty() const; //返回链表是否已空
    void Clear(); //清空链表
    int Retrieve( int position, string &x ) ; //获取链表第position位置的元素到x,成功返回0,否则返回-1
    int Replace( int position, const string &x ); //将链表第position位置的元素换为x,成功返回0,否则返回-1
    int Remove( int position, string &x ); //获取并删除链表第position位置的元素,成功返回0,否则返回-1
    int Insert( int position, const string &x ); //将元素x插入到链表第position位置,成功返回0,否则返回-1
 
 void Print();
    void PrintElement();
protected:
    Node workspace[max_list]; 
 index avaliable,last_used,head;
 int count;
 index new_node();
 void delete_node(index n);
 int current_position(index n)const;
 index set_position(int position)const;
};

index List::new_node()
{
  index new_index;
  if(avaliable!=-1)
  {
    new_index=avaliable;
    avaliable=workspace[avaliable].next;
  }
  else if(last_used<max_list-1)
  {
   new_index=++last_used;
  }
  else return -1;
  workspace[new_index].next=-1;
  return new_index;
}

void List::delete_node(index old_index)
{
   index previous;
   if (old_index == head) head = workspace[old_index].next;
   else
   {
      previous = set_position(current_position(old_index) - 1);
      workspace[previous].next = workspace[old_index].next;
   }
   workspace[old_index].next = avaliable;
   avaliable = old_index;
}

int List::current_position(index n) const
{  
 index newindex=head;
 int position=0;
 for(;newindex!=-1;newindex=workspace[newindex].next)
 {
     if(newindex==n)
  {
  return position;
  }
  position++;
 }  
 return -1;
}

index List::set_position(int position) const
{
 index current=head;
 if(position<0||position>count)
 {
  return -1;
 }
  for(int ix=0;ix!=position;ix++)
  {
    current=workspace[current].next;
  }
   return current;
}

 

List::List()
{
head=-1;
avaliable=-1;
last_used=-1;
count=0;
}

int List::Size()
{
   index newindex=head;
   index current;
   while(workspace[newindex].next!=-1)
   {
    current=workspace[newindex].next;
    workspace[newindex]=workspace[current];
       count++;
   }
   return count;
}
bool List::IsFull()const
{
   if(workspace[avaliable].next==-1&&last_used==max_list-1)
 {
 return true;
 }
 else
 {
   return false;
 }
}

bool List::IsEmpty() const
{
 if(workspace[head].next==-1)
 {
 return true;
 }
 else
 {
 return false;
 }
}
void List::Clear()
{
  workspace[0].next=-1;
  int i = 1;
   for (; i < max_list-1; i++)
   {
    workspace[i].entry="/0";
    workspace[i].next=i+1;
   }
   workspace[i+1].entry="/0";
   workspace[i+1].next=-1;
}
int List::Retrieve( int position, string &x )
{
      if(position<0||position>count-1)
        return -1;
    else
     {
   index newindex=set_position(position);
         x=workspace[newindex].entry;
      return 0;
      }
}

int List::Replace( int position, const string &x )
{
   if(position<0||position>count)
   return -1;
   else
    {
    index newindex=set_position(position);
   workspace[newindex].entry=x;
   return 0;
    }
}

int List::Remove( int position, string &x )
{     index following;index previous;
      if(position<0||position>count-1)
  return -1;
   if(position>0)
   {
    previous=set_position(position-1);   ///?????
    following=workspace[workspace[previous].next].next;
   }
 
   x=workspace[previous].entry;
   workspace[previous].entry="/0";
   delete_node(previous);
  
   workspace[previous].next=following;
   count--;
   return 0;
}

int List::Insert(int position,const string &x)
{
  index new_index,previous,following;
  if(position<0||position>count)
   return -1;
  if(position>0)
  {
  previous=set_position(position-1);
  following=workspace[previous].next;
  }
  else following=head;
  if((new_index=new_node())==-1)
   return -1;
  workspace[new_index].entry=x;
  workspace[new_index].next=following;
  if(position==0)
   head=new_index;
  else
   workspace[previous].next=new_index;
      count++;
   return 0;
}

void List::Print()
{
   for (int i = 0; i <count; i++)
    {   cout<<i<<":" <<(workspace[i].entry =="/0" ?"N/A": workspace[i].entry )<<":"<<workspace[i].next<<"    ";
  if(i%3==2||i==count-1)
  cout<<endl;
 }
}

void List::PrintElement()
{
  index newindex=head;
  cout<<"当前链表为: ";
  while(workspace[newindex].next!=-1)
   {
   cout<<workspace[newindex].entry<<" ";
 newindex=workspace[newindex].next; 
   }
  cout<<workspace[newindex].entry<<endl;
}
int main()
{
    List lis;
 cout<<"1)按输入序列:Jan、Feb、Mar、Apr、May 建立初始链表。"<<endl;
 lis.Insert(0,"/0");
    lis.Insert(1,"/0");
 lis.Insert(2,"Jan");
    lis.Insert(3,"Feb");
 lis.Insert(4,"Mar");
 lis.Insert(5,"Apr");
 lis.Insert(6,"May");
 cout<<endl;
 lis.Print();
 cout<<endl;
 lis.PrintElement();
     cout<<endl;


 cout<<"2)在Feb之前,May之后先后插入Jun,Oct"<<endl;
 lis.Insert(7,"Jun");
 lis.Insert(8,"Oct");
 cout<<endl;
 lis.Print();
 cout<<endl;
 lis.PrintElement();
 cout<<endl;

 cout<<"3)先后删除Mar和Jan"<<endl;
 string a,b;
 lis.Remove(5,a);
 lis.Remove(3,b);
 //cout<<a<<endl;     //输出要删的单词
 cout<<endl;
 lis.Print();
 cout<<endl;
 lis.PrintElement();
 cout<<endl;

 cout<<"4)在Apr之前插入Dec"<<endl;
 lis.Insert(3,"Dec");
 cout<<endl;
 lis.Print();
 cout<<endl;
 lis.PrintElement();
 cout<<endl;
  return 0;
}
 

这篇关于设计一个静态链表(或称为数组链表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/830853

相关文章

MySQL JSON 查询中的对象与数组技巧及查询示例

《MySQLJSON查询中的对象与数组技巧及查询示例》MySQL中JSON对象和JSON数组查询的详细介绍及带有WHERE条件的查询示例,本文给大家介绍的非常详细,mysqljson查询示例相关知... 目录jsON 对象查询1. JSON_CONTAINS2. JSON_EXTRACT3. JSON_TA

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Java数组初始化的五种方式

《Java数组初始化的五种方式》数组是Java中最基础且常用的数据结构之一,其初始化方式多样且各具特点,本文详细讲解Java数组初始化的五种方式,分析其适用场景、优劣势对比及注意事项,帮助避免常见陷阱... 目录1. 静态初始化:简洁但固定代码示例核心特点适用场景注意事项2. 动态初始化:灵活但需手动管理代

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

Linux系统中配置静态IP地址的详细步骤

《Linux系统中配置静态IP地址的详细步骤》本文详细介绍了在Linux系统中配置静态IP地址的五个步骤,包括打开终端、编辑网络配置文件、配置IP地址、保存并重启网络服务,这对于系统管理员和新手都极具... 目录步骤一:打开终端步骤二:编辑网络配置文件步骤三:配置静态IP地址步骤四:保存并关闭文件步骤五:重

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入