Complete operations on a Linear Linked List (1)

Source: Internet
Author: User

# 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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.