Previous article
< implement various operation of single linked list with C language (i) >is mainly a single linked list of some of the most basic operations, below, mainly some of the other typical algorithms and test procedures.
Copy Code code as follows:
/* To sort the single linked list
struct Lnode *sort (struct Lnode *head)
{
Linklist *p;
int n,i,j;
int temp;
n = listlength (head);
if (head = = NULL | | head->next = NULL)
return head;
p = head->next;
for (J =1;j<n;++j)
{
p = head->next;
for (i =0;i<n-j;++i)
{
if (P->data > P->next->data)
{
temp = p->data;
P->data = p->next->data;
P->next->data = temp;
}
p = p->next;
}
}
return head;
}
* * to the single linked list of reverse/
Linklist *reverse (linklist *head)
{
linklist *p1,*p2 = NULL,*P3 = NULL;
if (head = = NULL | | head->next = NULL)
return head;
P1 = head->next;
while (P1!=null)
{
P3 = p1->next;
P1->next = p2;
P2 = p1;
P1 = p3;
}
Head->next = p2;
head = p2;
return head;
}
Status equal (elemtype c1,elemtype C2)
{
if (c1== C2)
return TRUE;
Else
return FALSE;
}
/* Inserts into LA all data elements that are in linear table lb but not in LA * *
void Union (linklist *la,linklist *lb)
{
Elemtype *e;
int La_len,lb_len;
int i;
La_len = Listlength (La);
Lb_len = Listlength (Lb);
for (i=1;i<=lb_len;i++)
{
Getelem (lb,i,e); Take the first element of the LB and assign it to E
if (! Locateelem (la,*e,equal))//la does not exist in the same element as E, insert
Listinsert (la,++la_len,*e);
}
}
void print (Elemtype c)
{
printf ("%4d", c);
}
/* Merge two single linked list, the data in LA and LB is arranged in a non descending order, the LC is merged or the order is not descending.
void Mergelist (linklist *la,linklist *lb,linklist **LC)
{
int i =1,j=1,k=0;
int La_len,lb_len;
Elemtype *AI,*BJ;
AI = (elemtype *) malloc (sizeof (elemtype));
BJ = (Elemtype *) malloc (sizeof (elemtype));
Initlist (LC);
La_len = Listlength (La);
Lb_len = Listlength (Lb);
while (I<=la_len && J<=lb_len)
{
Getelem (La,i,ai);
Getelem (LB,J,BJ);
if (*AI<*BJ)
{
Listinsert (*lc,++k,*ai);
++i;
}
Else
{
Listinsert (*LC,++K,*BJ);
++j;
}
}
while (I<=la_len)
{
Getelem (La,i++,ai);
Listinsert (*lc,++k,*ai);
}
while (J<=lb_len)
{
Getelem (LB,J++,BJ);
Listinsert (*LC,++K,*BJ);
}
}
/* Traverse only once to find the middle node in the single linked list
1 defines two pointers, one pointer moves two steps at a time (fast pointer), and another pointer moves one data at a time (slow pointer)
2. When the quick pointer arrives at the end of the list, the slow pointer comes to the middle node of the list.
In the program can also determine whether a single linked list has a ring, if the quick pointer must be able to catch up on the slow pointer, otherwise it will end with null/
Linklist *searchmid (linklist * head)
{
if (NULL = = head)
return NULL;
if (Head->next = NULL)
return head;
if (Head->next->next = NULL)
return head;
Linklist *mid= Head;
linklist *p = mid->next;
while ((P!= null) && (null!=p->next))
{
MID = mid->next;
p = p->next->next;
}
return mid;
}
The following is mainly a single linked list of a test program.
Copy Code code as follows:
Status Comp (elemtype c1,elemtype C2)
{
if (C1==C2)
return TRUE;
Else
return FALSE;
}
void visit (Elemtype c)
{
printf ("%4d", c);
}
void Main ()
{
Linklist *l;
Linklist *mid;
Mid = (struct Lnode *) malloc (sizeof (struct lnode));
Elemtype *e,e0,*e1;
Status i;
int j,k;
E = (Elemtype *) malloc (sizeof (elemtype));
E1 = (Elemtype *) malloc (sizeof (elemtype));
i = Initlist (&l);
for (j=1;j<=6;j++)
{
i = Listinsert (l,1,j);
}
printf ("After the header of L is inserted 1~6: l=");
Listtraverse (L,visit);
printf ("L Intermediate node value is mid=:");
Mid = Searchmid (L);
printf ("%d\n", mid->data);
printf ("L Inverse output: l=");
Listtraverse (Reverse (L), visit);
printf ("L sort followed by: L=");
Listtraverse (sort (L), visit);
i = Listempty (L);
printf ("L" is null: i=%d (1: Yes, 0: NO) \ n ", i);
i = Clearlist (L);
printf ("Empty L: l=");
Listtraverse (L,visit);
i = Listempty (L);
printf ("L IS null: i=%d\n", i);
for (j=1;j<=10;j++)
{
Listinsert (L,J,J);
}
printf ("After the tail of the L is inserted 1~10: l=");
Listtraverse (L,visit);
Getelem (l,5,e);
printf ("The value of the 5th element is:%d\n", *e);
for (j=0;j<=1;j++)
{
K = Locateelem (L,j,comp);
if (k)
printf ("The value of the%d element is%d\n", k,j);
Else
printf ("element with no value%d \ n", j);
}
for (j=1;j<=2;j++)
{
Getelem (L,J,E1);
i = Priorelem (l,*e1,e);
if (i== infeasible)
printf ("element%d without precursor \ \ *e1");
Else
printf ("The precursor of element%d is:%d\n", *e1,*e);
}
For (J=listlength (L) -1;j<=listlength (L); j + +)
{
Getelem (L,J,E1);
i = Nextelem (l,*e1,e);
if (i==infeasible)
printf ("element%d no successor \", *E1);
Else
printf ("Successor of element%d:%d\n", *e1,*e);
}
K = Listlength (L);
for (j=k+1;j>=k;j--)
{
i = Listdelete (l,j,e);
if (i==error)
printf ("Delete%d data failed \ n", j);
Else
printf ("Deleted elements are:%d\n", *e);
}
printf ("Output the elements of L in turn:");
Listtraverse (L,visit);
Destroylist (L);
printf ("After destruction of L: l=%u\n", L);
printf ("*************************************************\n");
Linklist *la,*lb;
i = Initlist (&la);
if (i==1)
for (j=1;j<=5;j++)
i= Listinsert (LA,J,J);
printf ("la=");
Listtraverse (La,print);
Initlist (&LB);
for (j=1;j<=5;j++)
i = Listinsert (lb,j,2*j);
printf ("Lb =");
Listtraverse (Lb,print);
Union (LA,LB);
printf ("New la=");
Listtraverse (La,print);
printf ("*************************************************\n");
Linklist *la_1,*lb_1,*lc_1;
int a[4]={3,5,8,11},b[7]= {2,6,8,9,11,15,20};
Initlist (&la_1);
for (j=1;j<=4;j++)
Listinsert (La_1,j,a[j-1]);
printf ("la_1=");
Listtraverse (La_1,print);
Initlist (&lb_1);
for (j=1;j<=7;j++)
Listinsert (Lb_1,j,b[j-1]);
printf ("lb_1=");
Listtraverse (Lb_1,print);
Mergelist (la_1,lb_1,&lc_1);
printf ("lc_1=");
Listtraverse (Lc_1,print);
}
The following are some of the results of the run under Linux:
Copy Code code as follows:
After the header of L is inserted 1~6: l= 6 5 4 3 2 1
L The middle node value is Mid=:4
L Inverse output: l= 1 2 3 4 5 6
L sorted by: l= 1 2 3 4 5 6
L is null: i=0 (1: Yes, 0: NO)
After emptying L: l=
L IS null: I=1
After the tail of L is inserted 1~10: l= 1 2 3 4 5 6 7 8 9 10
The value of the 5th element is: 5
No element with a value of 0
The value of the 1th element is 1
Element 1 without precursor
The precursor of Element 2 is: 1
The successor of Element 9 is: 10
Element 10 No successor
Failed to delete 11th data
The deleted elements are: 10
Output the elements of L: 1 2 3 4 5 6 7 8 9
After destruction of L: l=7954544
*************************************************
La= 1 2 3 4 5
Lb = 2 4 6 8 10
New La= 1 2 3 4 5 6 8 10
*************************************************
La_1= 3 5 8 11
Lb_1= 2 6 8 9 11 15 20
Lc_1= 2 3 5 6 8 8 9 11 11 15 20