Home > Back-end >  C written data table structure order master function has a problem, I don't know how to change
C written data table structure order master function has a problem, I don't know how to change

Time:09-24

# include
using namespace std;
Const int defaultSize=100;
Template
The class Seqlist
{
Protected:
T * data;
Int maxSize;
Int last;
Public:
Seqlist (int sz=defaultSize);//the constructor
~ Seqlist ()
{
The delete [] data;
}
Void input ();
Void the output ();
Int the Locate (int I) const;
Bool getData (int I, T & amp; X) const
{
If (i> 0 & amp; & i<=last + 1)
{
X=data [I - 1);
return true;
}
The else
return false;
}
Int Search (T & amp; X) const;
Const int Size ()
{
Return maxSize;
}
Const int Length ()
{
Return the last + 1;
}
Boolean Isempty ()
{
Return (last==1)? True, false;
}
Bool Isfull ()
{
Return (last==maxSize - 1)? True, false;
}
Void serData (int I T& X)
{
If (i> 0 & amp; & i<=last + 1)
{
Data [I - 1);
}
}
Bool Insert (int I, T & amp; X);
Bool Remove (int I, T & amp; X);
};
Template
Void Seqlist : : input ()
{
Cout<" Please enter the number of elements: ";
While (1)
{
Cin> The last;
If (last> MaxSize - 1)
break;
Cout<" The input number is wrong ";
}
for(int i=0; i<=last; I++)
{
Cin> Data [I];
}
}
Template
Void Seqlist : : the output ()
{
for(int i=0; i<=last; I++)
Cout<" # "& lt; }
Template
Int Seqlist : : Search (T & amp; X) const
{
for(int i=0; i<=last; I++)
If (data [I]==x)
Return the I + 1;
The else
return 0;
}
Template
Int Seqlist : : Locate (int I) const
{
If (i>=1 & amp; & i<=last + 1)
return i;
The else
return 0;
}
Template
Bool Seqlist : : Insert (int I, T & amp; X)
{
If (last==maxSize - 1)
return false;
If (i<0 | | i> The last + 1)
return false;
For (int j=last; j>=I; J -)
{
The data=data [m + 1] [j];
}
Data [I]=x;
Last++;
return true;
}
Template
Bool Seqlist : : Remove (int I, T & amp; X)
{
If (last==maxSize - 1)
return false;
If (i<0 | | i> The last + 1)
return false;
X=data [I - 1);
For (int j=I; j<=last; J++)
{
Data [1]=data [j];
}
Last++;
return true;
}
Int main ()
{
Seqlist a;
Anderson nput ();
A.o utput ();
/* a. ocate (2);
A.S earch (3);
Cout<" Size="& lt; Cout<" Length="& lt; Cout<" Isempty? : "& lt; Cout<" Isfull? : "& lt; Cout<" Insert: "& lt; Cout<" Remove: "& lt; }

CodePudding user response:

The building Lord, I take you all bugs fixed basic

You at the beginning of the main error is the constructor for the definition, the simplest is the other way behind the constructor; To {}

But this, absolutely complains because you don't initialize data pointer, no initialization, how do you allocate memory?

Major repair points as follows:

1. Since you use a pointer, and delete, as necessary to the new, out of simple, I help you in your input function a new
2. The boundary of the circle should not be to & lt;=last, should be to & lt; Last, so your input and the output function of the I changed for you, other functions, you can be amended ,
3. In addition, the constructor form of initialization should put all the elements are initialized, I also help you to change,
4. The destructor, you'd better empty,

 # include 
using namespace std;
Const int defaultSize=100;
Template
The class Seqlist
{
Protected:
T * data;
Int maxSize;
Int last;
Public:
Seqlist (int sz=defaultSize) : data (nullptr), maxSize (sz), the last (0) {}
~ Seqlist ()
{
if (data ! Nullptr)={
The delete [] data;
Data=https://bbs.csdn.net/topics/nullptr;
}
}
Void input ();
Void the output ();
Int the Locate (int I) const;
Bool getData (int I T& X) const
{
If (I & gt; 0 & amp; & I & lt;=last + 1)
{
X=data [I - 1);
return true;
}
The else
return false;
}
Int Search (T& X) const;
Const int Size ()
{
Return maxSize;
}
Const int Length ()
{
Return the last + 1;
}
Boolean Isempty ()
{
Return (last==1)? True, false;
}
Bool Isfull ()
{
Return (last==maxSize - 1)? True, false;
}
Void serData (int I T& X)
{
If (I & gt; 0 & amp; & I & lt;=last + 1)
{
Data [I - 1);
}
}
Bool Insert (int I T& X);
Bool Remove (int I T& X);
};
Template
Void Seqlist : : input ()
{
Cout & lt; <"Please enter the number of elements:";
While (1)
{
cin> The last;
If (last & gt; MaxSize - 1)
break;
cout <"Input number is wrong";
}
This - & gt; Data=https://bbs.csdn.net/topics/new T [this -> last];
for (int i=0; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related