Single-linked list of the head plug, tail plug, delete, merge and other operations implementation code as follows:
#include <iostream>
using namespace Std;
The storage structure of a single-linked list
typedef struct NODE
{
int data;
struct node* next;
}node,*linklist;//linklist is a struct pointer type
Initializing a single-linked list
void Initlist (linklist *l)
{
*l = (linklist) malloc (sizeof (Node));//Establish head node
(*l)->next = null;//Create empty single-linked list L
}
L is the leading node of the empty-linked table head pointer, through the keyboard input table element values, using the head interpolation method to build a single linked list L
void Createfromhead (linklist L)
{
Node *s;
char c;
int flag = 1;
while (flag)//flag Initial value is 1, when input ' $ ', set flag is 0, the table is finished
{
c = GetChar ();
if (c! = ' $ ')
{
s = (node*) malloc (sizeof (Node));//Create new node S
S->data = C;
S->next = l->next;//Inserts the S node into the table header
L->next = s;
}
Else
{
Flag = 0;
}
}
}
L is the leading node of the empty-linked table head pointer, through the keyboard input Table element value, using the tail interpolation method to build a single linked list L
void Createfromfail (linklist L)
{
Node *r,*s;
The r=l;//r pointer dynamically points to the current footer of the linked list for tail insertion, and its initial value points to the head node
int flag = 1;
char c;
while (flag)//flag Initial value is 1, when input ' $ ', set flag is 0, the table is finished
{
c = GetChar ();
if (c! = ' $ ')
{
s = (node*) malloc (sizeof (Node));//Create new node S
S->data = C;
R->next = s;
R = S;
}
Else
{
Flag = 0;
R->next = null;//the next link field of the last node is empty, indicating that the end of the list
}
}
}
Finds the first node in the single-linked list L of the lead node, if found (1<=i<=n), returns the storage location of the node, otherwise returns null
Node *get (linklist L, int i)
{
int j = 0;
Node *p;
if (i <= 0)
{
return NULL;
}
p = L;
while ((P->next! = NULL) && (J < i))
{
p = p->next;//Scan next node
J++;//Scan Node counter
}
if (i = = j)
{
Return p;//found the first node.
}
Else
{
return NULL;
}
}
In the single-linked list L of the lead node, find the 1th node whose node value equals key, and return the storage position p if found, otherwise null
Node *locate (linklist L, int key)
{
Node *p;
p = l->next;//starting from the first node in the table
while (p!= NULL)//The current table is not checked out
{
if (P->data!=key)
{
p = p->next;
}
Else
{
break;//exit loop when the node value equals key is found
}
}
return p;
}
The length of the single-linked table L for the lead node
int Listlength (linklist L)
{
Node *p;
p = l->next;
Int J = 0;//is used to hold the length of a single linked list
while (P! = NULL)
{
p = p->next;
j + +;
}
Return j;//j is the length of the single linked list obtained
}
Insert a new node with a value of E in the first I position of the single-linked table L in the lead node, and n elements have n+1 insertion position.
#define OK 1
#define ERROR 0
void Inslist (linklist L, int i, int e)
{
Node *pre, *s;
int k = 0;
if (i<=0)//Determine if the insertion position is valid
{
cout << "Insertion position I value is not legal! "<< Endl;
return (ERROR);
}
Pre = L;
while (Pre! = null&&k < (i-1))//table is not checked out and no i-1 element is found, if you find the pre to point to the first i-1
{
Pre = pre->next;
K = k + 1;
}
if (!pre)//If the current position pre is empty, I have not found the first, indicating that the insertion position is unreasonable
{
cout << "The location is not reasonable. "<< Endl;
return (ERROR);
}
s = (node*) malloc (sizeof (Node));//apply for a new node s
S->data = e;//Value e Place the data field in S
S->next = pre->next;//Modify the pointer to complete the insert operation
Pre->next = s;
return (OK);
}
Delete the element I in the single-linked table L of the lead node and save the deleted element to the variable *e
void Dellist (linklist L, int i, int *e)
{
Node *pre, *r;
int k = 0;
Pre = L;
while (Pre->next! = null&&k < (i-1))//Find the precursor node of the deleted node I i-1, so that p points to it
{
Pre = pre->next;
K = k + 1;
}
While loop is skipped because of pre->next=null or i<1, because pre->next=null, no legal precursor position is found, indicating deletion position I is illegal
if (! ( Pre->next))
{
cout << "Delete node location I unreasonable!" "<< Endl;
return (ERROR);
}
R= pre->next;
pre->next=r->next;//Modify pointer, delete node r
*e=r->data;
Free (R);//Release the memory space occupied by the deleted node
return (OK);
}
Merges an ascending ordered single-linked list of LA and lb into an ascending ordered single-linked list LC
Linklist mergelinklist (linklist LA, linklist LB)
{
Node *pa, *pb,*r;
Linklist lc;//The LC to an empty table, PA and PB point to the first node in the single-linked list LA and LB respectively, the R initial value is LC and r always points to the footer of the LC
PA = la->next;
PB = lb->next;
LC = LA;
Lc->next = NULL;
R = LC;
When none of the two tables have been processed, the comparison chooses to insert the smaller value nodes into the new table LC
while ((PA! = null) && (PB! = null))
{
if (Pa->data <= pb->data)
{
r->next= PA;
R = Pa;//pa becomes the new R node.
pa=pa->next;
}
Else
{
R->next = PB;
r = PB;
PB = pb->next;
}
}
if (PA)//When table LA has the remaining elements, chain the remaining elements of table LA to the new table LC Footer
{
R->next = PA;
}
else//Otherwise, chain the remaining elements of the table lb to the new Table LC Footer
{
R->next = PB;
}
Free (LB);
return (LC);
}
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1771244
Single-linked list of head interpolation, tail plug, delete, merge and other operations