Home > Back-end >  Ask everybody a great god help look at the list of the written in C language, mainly is the definiti
Ask everybody a great god help look at the list of the written in C language, mainly is the definiti

Time:10-16

# include & lt; Iostream>
using namespace std;

Typedef int DataType.

Struct the Head//the Node to Node type name
{
DataType data;//data representative data element
Struct Head * next;//next to a pointer to the next node
};


//initialize the singly linked list
Int InitList (Head * H)
{//H for the pointer to the head of the singly linked list
H=new Head;//allocate space, as a head node
if(! H)
{
Cout<& lt;" Initialization error "& lt; return 0;
}
H - & gt; Next=NULL;
return 1;
}



//to table an empty
Int ListEmpty (Head * H) {

If (H - & gt; Next==NULL) return true;
The else return false;

}
//the length of the linear table
Int Listlength (Head * H) {
Int n=0;
The Head * p=H;
While (p - & gt; Next!=NULL)
{
n++;
P=p - & gt; next;

}
Return (n);


}


//return the first position of the matched elements with a specified value
Int Find_item (Head * H, DataType item)
{
The Head * p=H - & gt; next;
Int pos=0;
While (p)
{
Pos++;
If (p - & gt; data=https://bbs.csdn.net/topics/=item) break;
P=p - & gt; next;
}
If (p) return pos.
The else return 0;
}


//get singly linked lists on the specified position in the data element
Int Find_pos (Head * H, int pos, DataType item)
{
The Head * p=H - & gt; next;
Int I=0;
While (p & amp; & i!=pos)
{
P=p - & gt; next; i++;
}
If (p==NULL)
{
Cout<& lt;" Location is invalid "& lt; return 0;
}
The item=p - & gt; The data;
return 1;
}


//to insert a new element linear table specified location

Int ListInsert (Head * H, int pos, DataType item)
{
The Head * p=H; Int I=0;
While (p) {//find pos precursor
If (I + 1==pos) break;
P=p - & gt; next; i++;
}
If (p==NULL) {//search is not successful, quit running
Cout<& lt;" Insert location is invalid "& lt; return 0;
}
The Head * t=new Head; T - & gt; data=https://bbs.csdn.net/topics/item;
T - & gt; Next=p - & gt; next;
P - & gt; Next=t;
return 1;
}

//traverse singly linked list
Void TraverseList (Head * H)
{//H for the pointer to the head of the singly linked list
The Head * p=H - & gt; next;
While (p)
{
Cout

P=p - & gt; next;
}
Cout}


//delete the first from a linear table of the matched elements with a specified value
Int ListDelete (Head * H, DataType item)
{
The Head * p=H, * t; Int I=0;
While (p - & gt; Next) {//find pos precursor
If (p - & gt; Next - & gt; data=https://bbs.csdn.net/topics/=item) break;
P=p - & gt; next;
}
If (p - & gt; Next==NULL) {//search is not successful, quit running
Cout<& lt;" Remove elements do not exist "& lt; return 0;
}
T=p - & gt; next;//(1) t to be deleted node
P - & gt; Next=t - & gt; next;//(2) delete t link relations
The delete t;//(3) to release the delete node
return 1;
}


//cancel the singly linked list
Void DestroyList (Head * & amp; H)
{
The Head * p;
While (H)
{
P=H;
H=H - & gt; next;
}
}


Int main () {
The Head STR, * a;
A=& amp; STR;
DataType f, g;
Int n, x;
InitList (a);//initialization list
N=Listlength (a);//the length of the list
Cin> x;
Cout<& lt;" Please enter the list of elements, ends with a 1 ";//input elements in the list, and ends with a 1
While (x!=1) {
ListInsert (a, n + 1, x);
n++;
}
Cin> g;
Find_item (a, g);
N=ListEmpty (a);//determine whether the singly linked list is empty tables: empty return 1, otherwise it returns 0
Cout<& lt;" The current list long for: "& lt; Cin> f;
ListInsert (a, 3, f);
Cout<& lt;" After insert at 3 f, elements in the table is: ";
TraverseList (& amp; STR);


}


Help everybody see initialization list to do this right, and whether the list is empty the function parameters of the pointer variable should be about?

  • Related