Home > Back-end >  Where is wrong?
Where is wrong?

Time:10-03

# include & lt; Iostream>//introduction of input and output flow
using namespace std;

The template & lt; Typename DataType>
Struct Node
{
DataType data;//data domain
Node * next;//pointer field
};

The template & lt; Typename DataType>
The class LinkList
{
Public:
LinkList ();//a no-parameter constructor, create an empty list with only head node
LinkList (DataType a [], int n);//a constructor, establishing the singly linked list of n elements
~ LinkList ();//destructors
Int Length ();//for the length of a singly linked list
Int Empety ();
DataType Get (int I);//by a search, search for the ith a node element value
Int the Locate DataType (x);//by value lookup, find the value of x number of elements
Void Insert (int I DataType x);//insert operation, the place I inserted value for the node of x
DataType Delete (int I);//delete, delete the ith node
Void PrintList ();//traversal operations, according to the serial number, in turn, each element output
Private:
Node * first;//singly linked lists head pointer
};

The template & lt; Typename DataType>
LinkList : : LinkList ()
{
The first=new Node;//generated head node
First - & gt; Next=nullptr;//head node pointer field empty
}

The template & lt; The class DataType>
LinkList : : ~ LinkList ()
{
Node * q=NULL;
While (first!=NULL)//release singly linked lists of each node storage space
{
Q=first;//temporary release node
The first=first - & gt; Next;//first nodes of the next point to be released
The delete q;
}
}

The template & lt; Typename DataType>
Int LinkList : : Empety ()
{
If (first - & gt; Next==nullptr)
return 1;
The else
return 0;
}

The template & lt; Typename DataType>
Void LinkList : : PrintList ()
{
Node * p=first - & gt; Next;//pointer initialization p
While (p!=nullptr)
{
Cout & lt;

P=p - & gt; Next;//work pointer p (one-way, attention can't writing p++
}
}

The template & lt; Typename DataType>
Int LinkList : : Length ()
{
Node * p=first - & gt; Next; Work//a pointer initialized to begin to contact p
int count=0;//accumulator count initialization
While (p!=nullptr)
{
P=p - & gt; Next;
count++;
}
Return the count.//note that the count of initialization and the relationship between the return value
}

The template & lt; Typename DataType>
DataType LinkList : : Get (int I)
{
Node * p=first - & gt; Next;//pointer initialization p
Int count=1;//accumulator count initialization
While (p!=nullptr & amp; & The count & lt; I)
{
P=p - & gt; Next;//work pointer p (one-way
count++;
}
If (p==nullptr) throw "location";
The else return p - & gt; The data;
}

The template & lt; Typename DataType>
Int LinkList : : Locate DataType (x)
{
Node * p=first - & gt; Next;//pointer initialization p
Int count=1;//accumulator count initialization
While (p!=nullptr)
{
If (p - & gt; Data=https://bbs.csdn.net/topics/=x) return the count;//to find success, end and returns the serial number
P=p - & gt; Next;
count++;
}
return 0;//exit cycle show lookup failure
}

The template & lt; Typename DataType>
Void LinkList : : Insert (int I, DataType x)
{
Node * p=first, * s=nullptr;//pointer initialization p
int count=0;
While (p!=nullptr & amp; & The count & lt; I - 1)//find the I - 1 node
{
P=p - & gt; Next;//work pointer p (one-way
count++;
}
If (p==nullptr) throw "location";//not found the I - 1 node
The else {
S=new Node; S - & gt; Data=https://bbs.csdn.net/topics/x;//application node s, data fields for x
S - & gt; Next=p - & gt; Next; P - & gt; Next=s;//insert node s to node p
}
}

//head interpolation structure
//the template & lt; Typename DataType>
//LinkList : : LinkList (DataType a [], int n)
//{
//first=new Node; First - & gt; Next=nullptr;//initialize a empty list
//for (int I=0; I & lt; n; I++)
//{
//Node * s;
//s=new Node; S - & gt; Data=https://bbs.csdn.net/topics/a [I];
//s - & gt; Next=first - & gt; Next; First - & gt; Next=s;//insert node s end node after
//}
//}

The template & lt; Typename DataType>
LinkList : : LinkList (DataType a [], int n)
{
The first=new Node;//generated head node
Node * r=first, * s=nullptr;//the tail pointer initialization
For (int I=0; I & lt; n; I++)
{
S=new Node; S - & gt; Data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s; R=s;//insert node s to the terminal nodes after
}
R - & gt; Next=nullptr;//singly linked lists set up, the terminal node pointer field empty
}

The template & lt; Typename DataType>
DataType LinkList : : Delete (int I)
{
DataType x;
Node * p=first, * q=nullptr;//p to the head node
int count=0;
While (p!=nullptr & amp; & The count & lt; The first I - I - 1)//find a node
{
P=p - & gt; Next;
count++;
}
If (p==nullptr | | p - & gt; Next==nullptr)/nodes p does not exist or subsequent p/there is no
"Throw" position;
The else {
Q=p - & gt; Next; X=q - & gt; The data;//the staging was delete node
P - & gt; Next=q - & gt; Next;//removable chain
The delete q;
Return the x;
}
}

Int main ()
{
Int r [5]={1, 2, 3, 4, 5}, I, x;
LinkList L (r, 5);
Cout & lt; <"The current linear table data for:";
L.P rintList ();//output current chain table 1 2 3 4 5
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull

  • Related