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

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

相关文章

使用C++实现链表元素的反转

《使用C++实现链表元素的反转》反转链表是链表操作中一个经典的问题,也是面试中常见的考题,本文将从思路到实现一步步地讲解如何实现链表的反转,帮助初学者理解这一操作,我们将使用C++代码演示具体实现,同... 目录问题定义思路分析代码实现带头节点的链表代码讲解其他实现方式时间和空间复杂度分析总结问题定义给定

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

Python中的可视化设计与UI界面实现

《Python中的可视化设计与UI界面实现》本文介绍了如何使用Python创建用户界面(UI),包括使用Tkinter、PyQt、Kivy等库进行基本窗口、动态图表和动画效果的实现,通过示例代码,展示... 目录从像素到界面:python带你玩转UI设计示例:使用Tkinter创建一个简单的窗口绘图魔法:用

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

vue如何监听对象或者数组某个属性的变化详解

《vue如何监听对象或者数组某个属性的变化详解》这篇文章主要给大家介绍了关于vue如何监听对象或者数组某个属性的变化,在Vue.js中可以通过watch监听属性变化并动态修改其他属性的值,watch通... 目录前言用watch监听深度监听使用计算属性watch和计算属性的区别在vue 3中使用watchE

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que