# Include <stdio. h>
# Include <stdlib. h>
# Define maxsize 50
Typedef int elemtype;
//////////////////////////////////////// ///
Typedef struct lnode
{
Elemtype data;
Struct lnode * next;
} Linklist; // node type of a single-chain table
/******************** Create a linked list (create a table by using the header Insertion Method) **************************/
Void createlistf (linklist * & L, elemtype A [], int N)
{
Linklist * s;
Int I;
L = (linklist *) malloc (sizeof (linklist ));
L-> next = NULL;
For (I = 0; I <n; I ++)
{
S = (linklist *) malloc (sizeof (linklist ));
S-> DATA = A [I];
S-> next = L-> next;
L-> next = s;
}
}
/********************* Create a linked list (create a table by means of tail insertion) get the ordered result **************************/
Void createlistr (linklist * & L, elemtype A [], int N)
{
Linklist * s, * R;
Int I;
L = (linklist *) malloc (sizeof (linklist ));
R = L;
For (I = 0; I <n; I ++)
{
S = (linklist *) malloc (sizeof (linklist ));
S-> DATA = A [I];
R-> next = s;
R = s;
}
R-> next = NULL;
}
/***************** C initialize the Linear Linked List ******************/
Void initlist (linklist * & L)
{
L = (linklist *) malloc (sizeof (linklist ));
L-> next = NULL;
}
******************/
Void destroylist (linklist * & L)
{
Linklist * P = L, * q = p-> next;
While (Q! = NULL)
{
Free (P );
P = Q;
Q = p-> next;
}
Free (p); // at this time, Q is null. P points to the end node and releases it.
}
/**************** Determine whether the Linear Linked List is empty **************** **/
Int listempty (linklist * l)
{
Return (L-> next = NULL );
}
***************** */
Int listlength (linklist * l)
{
Linklist * P = L;
Int n = 0;
While (p-> next! = NULL)
{
N ++;
P = p-> next;
}
Return N;
}
/***************** Output Linear Linked List ******************/
Void displist (linklist * l)
{
Linklist * P = L-> next;
While (P! = NULL)
{
Printf ("% d", p-> data );
P = p-> next;
}
Printf ("\ n ");
}
/**************** Calculate the I-th element value in a linear linked list ************* *****/
Int getelem (linklist * l, int I, elemtype & E)
{
Linklist * P = L;
Int J = 0;
While (j <I & P! = NULL)
{
J ++;
P = p-> next;
}
If (P = NULL)
{
Return 0;
}
Else
{
E = p-> data;
Return 1;
}
}
**************** **/
Int locateelem (linklist * l, elemtype E)
{
Linklist * P = L-> next;
Int I = 1;
While (P! = NULL & P-> data! = E)
{
P = p-> next;
I ++;
}
If (P = NULL)
{
Return 0;
}
Else
{
Return I;
}
}
/**************** Insert an element value in the linear linked list **************** **/
Int listinsert (linklist * & L, int I, elemtype E)
{
Linklist * P = L, * s;
Int J = 0;
While (j <I-1 & P! = NULL)
{
J ++;
P = p-> next;
}
If (P = NULL)
{
Return 0;
}
Else
{
S = (linklist *) malloc (sizeof (linklist ));
S-> DATA = E;
S-> next = p-> next;
P-> next = s;
Return 1;
}
}
Void main ()
{
Int A [8] = {6, 9 };
Linklist * l;
Printf ("% s", "Create and output head insertion method to create a Linear Linked List: \ n ");
Createlistf (L, A, 8 );
// Printf ("% s", "Create and output the end plug method to create a Linear Linked List: \ n ");
// Createlistr (L, A, 8 );
Displist (L );
Printf ("% s", "position where the output element value is 7 :");
Int e = locateelem (L, 7 );
Printf ("% d \ n", e );
Printf ("% s", "insert element value 67 and Output Linear Linked List: \ n ");
Listinsert (l, 2, 67 );
Displist (L );
}
The running result is: