| |

VerySource

 Forgot password?
 Register
Search
View: 942|Reply: 1

Data structure linear table problem, hope expert solution, thank you

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Iran

Post time: 2020-3-22 19:30:01
| Show all posts |Read mode
How to append elements to the end of a linear table in a data structure
Please experts to help answer
Thank you
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-8 07:30:02
| Show all posts
//Give you a ready-made reference
/*================================================ ==========
*********************Linear table and its implementation***********************
================================================== ========*/
#ifndef _LINEARLIST_H_
#define _LINEARLIST_H_
//------------------------------------------------ ---------
#include <exception>
#include <iostream>
using namespace std;

//Class definition
template<class T>
class LinearList
{
public:
LinearList(int MaxListSize = 10); //Constructor
~LinearList(){delete [] element;} //Destructor
bool IsEmpty() const {return length == 0;}
int Length() const {return length;}
bool Find(int k, T&x) const; //Return the kth element to x
int Search(const T&x) const; // returns the location of x
LinearList<T>&Delete(int k, T&x); // delete the kth element and return it to x
LinearList<T>&Insert(int k, const T&x); // Insert x after the kth element
void Output(ostream&out) const;
private:
int length;
int MaxSize;
T *element; // One-dimensional dynamic array


//Custom exception handling class
public:
class NoMem:public std::exception
{
public:
virtual const char* what() const throw()
{
return "there is no memory!";
}
};

class OutOfBounds:public std::exception
{
public:
virtual const char* what() const throw()
{
return "out of bound!";
}
};
};

template<class T>
LinearList<T>::LinearList(int MaxListSize)
{// Constructor of linear table based on formula
MaxSize = MaxListSize;
element = new T[MaxSize];
length = 0;
}
//------------------------------------------------ --------------

template<class T>
bool LinearList<T>::Find(int k, T&x) const
{//Fetch the kth element into x
//If the k-th element does not exist, return false, otherwise return true
if (k <1 || k> length)
return false; // There is no k-th element
x = element[k-1];
return true;
}
//------------------------------------------------ ----------------
template<class T>
int LinearList<T>::Search(const T&x) const
{// Find x, if found, return the position of x
// If x is not in the table, return 0
for (int i = 0; i <length; i++)
if (element[i] == x)
return ++i;
return 0;
}

//------------------------------------------------ ----------------
template<class T>
LinearList<T>&LinearList<T>::Delete(int k, T&x)
{// Put the kth element in x, then delete the kth element
// If the k-th element does not exist, an exception OutOfBounds is raised
if(Find(k, x))
{// Move element k+l, ... forward one position
for (int i = k; i <length; i++)
element[i-1] = element[i];
length--;
return *this;
}
else throw OutOfBounds();
}
//------------------------------------------------ ----------------
template<class T>
LinearList<T>&LinearList<T>::Insert(int k, const T&x)
{// Insert x after the kth element
// If there is no k-th element, an exception OutOfBoun d s is raised
// If the table is full, an exception NoMem is raised
if (k <0 || k> length) throw OutOfBounds();
if (length == MaxSize) throw NoMem();
//Move back one position
for (int i = length-1; i >= k; i--)
element[i+1] = element[i];
element[k] = x;
length++;
return *this;
}

//------------------------------------------------ ---------------
template<class T>
void LinearList<T>::Output(ostream&out) const
{//Transfer the table to the output stream
for (int i = 0; i <length; i++)
out<<element[i]<< "";
}
//------------------------------------------------ ---------------
// Overload <<

template <class T>
ostream&operator<<(ostream&out, const LinearList<T>&x)
{
x.Output(out);
return out;
}

//---------------------------end-------------------- ------------

#endif
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list