Linked List C itself

Source: Internet
Author: User

Linked List:

 # Include  <  String  . H  >  
# Include < Stdlib. h >
# Include < Stdio. h >

Typedef Struct Stu_listme stu_list;
/* Define struct */

Struct Stu_listme
{
Char * P_ch_data;
Stu_list * P_stu_next;
};

Char * Getform_list (stu_list * Phead, Int Nlim)
/* Obtain the K-Bit Data */
{
Char * P_ch_lim = NULL;
Int N_count = 1 ;
Stu_list * P_stu_point = Phead;
While (N_count <= Nlim)
{
If (P_stu_point = Null)
Return NULL;
P_stu_point = P_stu_point -> P_stu_next;
N_count ++ ;
}
If (P_stu_point ! = Null)
{
P_ch_lim = P_stu_point -> P_ch_data;
}
Return P_ch_lim;
}

Int Calu_list (stu_list * Phead)
/* Calculate the length of a linked list */
{
Int N_back = 0 ;
Stu_list * P_stu_point = Phead;
While (P_stu_point -> P_stu_next ! = Null)
{
P_stu_point = P_stu_point -> P_stu_next;
N_back ++ ;
}
Return N_back;
}

Void Insert_node ( Char * Pnow, stu_list ** Phead)
/* Insert linked list */
{
Stu_list * P_stu_me = (Stu_list * ) Malloc ( Sizeof (Stu_list ));
P_stu_me -> P_ch_data = ( Char * ) Malloc (strlen (pnow) + 1 );
Memcpy (p_stu_me -> P_ch_data, pnow, strlen (pnow ));
P_stu_me -> P_ch_data [strlen (pnow)] = ' \ 0 ' ;
If ( * Phead = Null)
{
P_stu_me -> P_stu_next = NULL;
}
Else
{
P_stu_me -> P_stu_next = * Phead;
}
* Phead = P_stu_me;
}

Void List_free (stu_list * Phead)
/* Release linked list */
{
Stu_list * P_stu_me = Phead;
While (P_stu_me ! = Null)
{
Stu_list * P_stu_point = P_stu_me -> P_stu_next;
Free (p_stu_me -> P_ch_data );
Free (p_stu_me );
P_stu_me = P_stu_point;
}
}

Int Main ()
{
Stu_list * P_stu_head = NULL;
Insert_node ( " Fuck " , & P_stu_head );
Insert_node ( " Ffsf " , & P_stu_head );
Printf ( " % S \ n " , Getform_list (p_stu_head, 0 ));
Printf ( " % D \ n " , Calu_list (p_stu_head ));
List_free (p_stu_head );
Return 0 ;
}

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.