Home > Back-end >  Data structure created for linked list, insert, delete, sort, inversion
Data structure created for linked list, insert, delete, sort, inversion

Time:09-17

# include & lt; Stdio. H>
# include & lt; The malloc. H>
# include & lt; Stdlib. H>
# include & lt; Stdbool. H>

Typedef struct node
{
int data;
Struct node * pNext;
} Node, * pNode;

PNode creat_list ();
Void travese_list ();
Bool utilities like is_empty that (pNode);
Int length_empty (pNode);
Void sort_list (pNode, int);
Bool insert_list (pNode);
Bool delete_list (pNode);
Bool inversion_list (pNode);

Int main ()
{
PNode head=NULL;
Int len.

The head=creat_list ();//create a acyclic singly linked list
Travese_list (head);//traverse the singly linked list

If (utilities like is_empty that (the head))
Printf (" list is empty! \n");
The else
Printf (" the list is not empty! \n");

Len=length_empty (head);//calculate the length of the list
Sort_list (head, len); Order of the elements in the list//
Travese_list (head);//output sorted list
Insert_list (head);//insert an element
Travese_list (head);//output after inserting new list
Delete_list (head);//delete some data in the list
Travese_list (head);//output to delete the new list
Inversion_list (head);
Travese_list (head);

return 0;
}
//create a linked list node (take the lead of tail interpolation)
PNode creat_list ()
{
Int len.//the node number
int i;//loop variable
Int val.//is used to store data domain data

PNode head=(pNode) malloc (sizeof (Node));

PNode Tail=head;//define a nodes to head tail
Tail - & gt; PNext=NULL;//end node pointer field empty

If (head==NULL)
{
Printf (" chain table creation failed!" );
The exit (1);
}
The else
{
Printf (" do you want to create a few node?" );
The scanf (" % d ", & amp; Len);

for (i=0; i{
Printf (" please enter the first % d node values: ", I + 1);
The scanf (" % d ", & amp; Val);

PNode pNew=(pNode) malloc (sizeof (Node));//create a new temporary node

PNew - & gt; Data=https://bbs.csdn.net/topics/val;//to the new node data fields assignment
Tail - & gt; PNext=pNew;//to the back of the nodes to hang in the previous letter
PNew - & gt; PNext=NULL;//the new node data fields empty
Tail=pNew;//end node point to the new node
}
}
return head;//return the head pointer
}
//traversing the list
Void travese_list (pNode head)
{
PNode p=head - & gt; PNext;//head node is assigned to a pointer to the domain p

While (p!//=NULL) to determine whether a pointer field is empty
{
Printf (" % d ", p - & gt; data);//output data in the domain
P=p - & gt; PNext;//pointer to move back a place
}
Printf (" \ n ");
return;
}
//whether the list is empty
Bool utilities like is_empty that (pNode head)
{
If (the head - & gt; PNext==NULL)
return true;
The else
return false;
}
//calculate the length of the list
Int length_empty (pNode head)
{
Int CNT=0;
PNode p=head - & gt; PNext;//p points to the first node

While (p!=NULL)//p points to judge whether the empty
{
Cnt++;
P=p - & gt; PNext;
}

Printf (" the length of the list is: % d \ n ", CNT);
Return CNT.
}
//to sort of element on the list
Void sort_list (pNode head, int len)
{
PNode p, q;
PNode temp.//int k
Int I, j, t;
Printf (" list ordering is as follows: \ n ");

For (I=0, p=head - & gt; PNext; i{
Temp=p;//will subscript preserved, equivalent k=I;
For (j=I + 1, q=p - & gt; PNext; jIf (temp - & gt; The data & gt; Q - & gt;//data) is equivalent to a [k] & gt; A [j]
Temp=q;//equivalent to k=j
If (temp!=p)//equivalent to k!=I;
{
T=temp - & gt; The data;//equivalent to t=a [k]
Temp - & gt; Data=https://bbs.csdn.net/topics/p-> data;//equivalent to a [k]=[I] a
P - & gt; Data=https://bbs.csdn.net/topics/t;//equivalent to a [I]=t
}
}
return;
}
//insert a data in the linked list
Bool insert_list (pNode head)
{
Int I=0, pos, val.
PNode p=head, q;

Printf (" do you want to insert data in which a position: ");
The scanf (" % d ", & amp; Pos);

Printf (" do you want to insert number is: ");
The scanf (" % d ", & amp; Val);

While (p!=NULL & amp; & i{
P=p - & gt; PNext;
i++;
}

If (i> Pos - 1 | | p==NULL)//determine whether to insert the location of the data more than the length of the chain table + 1
return false;

PNode pNew=(pNode) malloc (sizeof (Node));//create a new node

If (pNew==NULL)
{
Printf (" node creation failed!" );
The exit (1);
}

PNew - & gt; Data=https://bbs.csdn.net/topics/val;//will be inserted data is a new node data fields
Q=p - & gt; PNext;//will be inserted inside a node to save data domain to q
P - & gt; PNext=pNew;//p point to the new node
PNew - & gt; PNext=q;//point p at the back of the new node q

return true;
}
//delete some data in the list
Bool delete_list (pNode head)
{
Int I=0, pos.
PNode p=head, q;

Printf (" which position you want to delete the data: ");
The scanf (" % d ", & amp; Pos);

While (p - & gt; PNext!=NULL & amp; & i{
P=p - & gt; PNext;
i++;
}

If (i> Pos - 1 | | p - & gt; PNext==NULL)//determine whether to delete the location of the data more than the length of the chain table + 1
{
Printf (" delete failed!" );
return false;
}

Q=p - & gt; PNext;//will delete nodes stored
P - & gt; PNext=q - & gt; PNext;//points to a node after
Free (q);//release to remove node memory
Q=NULL;
return true;
}
//flip list
Bool inversion_list (pNode head)
{
PNode TMP=NULL;//define a temporary node
PNode p=NULL;//define p for head node

Printf (" the turn of the list is: \ n ");

If (head==NULL)//whether the list is empty
{
return false;
}

TMP=head - & gt; PNext; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull