Home > Back-end >  Singly linked list implementation: why doesn't my code is compiled by running the always pop-up
Singly linked list implementation: why doesn't my code is compiled by running the always pop-up

Time:09-18


# include & lt; Iostream>

using namespace std;
The template & lt; The class DataType>
Struct Node
{
DataType data;
Node * next;
};
The template & lt; The class DataType>
The class LinkList
{
Public:
LinkList ();
LinkList (DataType a [], int n);
~ LinkList ();
Int Length ();
DataType Get (int I);
Int the Locate DataType (x);
Void Insert (int I DataType x);
DataType Delete (int I);
Void PrintList ();

Private:
Node * first;
};
The template & lt; The class DataType>
LinkList : : LinkList ()
{
The first=new Node;
First - & gt; Next=NULL;
}
/* template
LinkList : : LinkList (DataType a [], int n)//head interpolation
{
First - & gt; Next=NULL;
for(int i=0; I{
Node * s=new Node;
S - & gt; data=https://bbs.csdn.net/topics/a [I];
S - & gt; Next=first - & gt; Next;
First - & gt; Next=s;
}
} */
Template
LinkList : : LinkList (DataType a [], int n)//stern interpolation
{

First - & gt; Next=NULL;
Node * r=first;
for(int i=0; I{
Node * s=new Node;
S - & gt; data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s;
R=s;
}
R - & gt; Next=NULL;
}
Template
Int LinkList : : Length ()
{Node * p=first - & gt; Next;
Int Count=0;
while(p!=NULL)
{p=p - & gt; Next;
Count++;
}
Return the Count.
}
Template
DataType LinkList : : Get (int I)
{Node * p=first - & gt; Next;
Int Count=1;
while(p!=NULL& & Count{
P=p - & gt; Next;
Count++;
}
If (p==NULL) throw "location";
The else
The return p - & gt; The data;
}
Template
Int LinkList : : Locate DataType (x)
{int Count=1;
Node P=first - & gt; Next;
while(p!=NULL)
{
If (p - & gt; data=https://bbs.csdn.net/topics/=x) return the Count;
P=p - & gt; Next;
Count++;
}
Return 0;
}
Template
Void LinkList : : Insert (int I, DataType x)
{
Node * p=first;
Int Count=0;
while(p!=NULL& & Count{
P=p - & gt; Next;
Count++;
}
If (p==NULL)
Abnormal "throw" insert position;
The else
{
Node * s=new Node;
S - & gt; data=https://bbs.csdn.net/topics/x;
S - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
}
}
Template
DataType LinkList : : Delete (int I)
{
Node * p=first;
Int Count=1;
while(p!=NULL& & Count{
P=p - & gt; Next;
Count++;
}
If (p==NULL | | p.n ext==NULL)
Cout<" Position error ";
The else
{Node * q=p - & gt; Next;
Int x=q - & gt; The data;
P - & gt; Next=q - & gt; Next;
The delete q;
Return the x;
}
}
Template
Void LinkList : : PrintList ()
{
Node * p=first;
while(p!=NULL)
{
P=p - & gt; Next;
Cout

}

}
Template
LinkList : : ~ LinkList ()
{Node * q;
While (first!=NULL)
{
Q=first;
The first=first - & gt; Next;
The delete q;
}
}


Int main ()
{
,2,5,8,5 int a [5]={1}, b [5]=,5,9,7,6 {2};
LinkList List_1=LinkList (a, 5);
LinkList List_2=LinkList (b, 5);
List_1. PrintList ();
Return 0;
}

CodePudding user response:

LinkList : : LinkList (DataType a [], int n)//head interpolation
There is no to the first assignment in the function
 template 
LinkList : : LinkList (DataType a [], int n)//head interpolation
{first=new Node;
First - & gt; data=https://bbs.csdn.net/topics/a [0].
Node * r=first;
for(int i=1; I{Node * s=new Node;
S - & gt; data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s; R=s;
}
R - & gt; Next=NULL;
}
  • Related