C data structure of the single linked list Detailed example analysis _c language

Source: Internet
Author: User
Copy Code code as follows:

#include <stdio.h>
#include <stdlib.h>
typedef struct TYPE
{
int num;
struct type *next;
}type;
//=============================================================
Syntax format: TYPE *init_link_head (int n)
Implementation function: From beginning to end, the positive sequence creates a linked list with n nodes and initializes its values.
Parameters: N: The length of the list, that is, the number of nodes
Return value: The first address of the linked list created
//=============================================================
TYPE *init_link_head (int n)
{
int i;
TYPE *phead = null, *PF = NULL, *PI = NULL;
for (i=0; i<n; i++)
{
PI = (type *) malloc (sizeof (type));
printf ("Please inout num:\n");
scanf ("%d", &pi->num);
if (i = = 0)
PF = Phead = pi;
Else
{
Pf->next = PI;
PF = pi;
}
Pi->next = NULL;
}
return phead;
}
//=============================================================
Syntax format: TYPE *init_link_end (int n)
Implementation function: From the end of the head, in reverse, create a linked list with n nodes, and initialize its value
Parameters: N: The length of the list, that is, the number of nodes
Return value: The first address of the linked list created
//=============================================================
TYPE *init_link_end (int n)
{
TYPE *phead = null, *PI = NULL;
int i;
for (i=0; i<n; i++)
{
PI = (type *) malloc (sizeof (type));
printf ("Please inout num:\n");
scanf ("%d", &pi->num);
if (i = = 0)
Pi->next = NULL;
Else
Pi->next = Phead;
Phead = PI;
}
return phead;
}
//=============================================================
Syntax format: Insert_link (TYPE * phead,type * pi)
Implementation function: Add the newly requested node to the specified list
Parameters: *phead: List to insert
* Pi: With insert node
Return value: Inserts the first address of the new list after the specified node
//=============================================================
Type * Insert_link (type *phead, type *PI)
{
TYPE *PB, *PF;
PB = Phead;
if (Phead = NULL)
{
Phead = PI;
Phead->next = NULL;
}
Else
{
while ((Pi->num > Pb->num) && (Pb->next!= NULL))
{
PF = PB;
PB = pb->next;
}
if (Pi->num <= pb->num)
{
if (Pb = = Phead)
{
Pi->next = Phead;
Phead = PI;
}
Else
{
Pf->next = PI;
Pi->next = PB;
}
}
Else
{
Pi->next = NULL;
Pb->next = PI;
}
}
return phead;
}
//=============================================================
Syntax format: Delete_link (TYPE * phead,int num)
Implementation function: Deletes the node to which the given ordinal number points
Parameters: *phead: List to delete
Num: The node that you want to delete
Return value: Deletes the first address of the new list after the specified node
//=============================================================
Type * Delete_link (type *phead, int num)
{
TYPE *PF;
TYPE *PB;
if (Phead = NULL)
{
printf ("\nempty link\n");
return NULL;
}
PB = Phead;
while ((Pb->num!= num) && pb->next!= NULL)
{
PF = PB;
PB = Pb->next;
}
if (pb->num = num)
{
if (Pb = = Phead)
Phead = phead->next;
Else
Pf->next = pb->next;
Free (PB);
printf ("The node is deleted\n");
}
Else
printf ("The Node not found\n");
return phead;
}
//=============================================================
Syntax format: Print_link (TYPE * phead)
Implementation function: Print all node data in the specified list
Parameters: *phead: The first address of the list to be printed
return value: None
//=============================================================
void Print_link (TYPE *phead)
{
TYPE *temp = Phead;
while (temp!= NULL)
{
printf ("%d", temp->num);
temp = temp->next;
}
}
//=============================================================
Syntax format: search_num (TYPE * phead,int num)
Implementation function: In the specified list, find the specified element by name
Parameters: Phead: To find the first address of the chain, Num need to find the string
return value: None
//=============================================================
void Search_num (TYPE *phead, int num)
{
TYPE *temp = Phead;
while (temp!= NULL)
{
if (temp->num = num)
printf ("%d", num);
temp = temp->next;
}
if (temp = NULL)
printf ("Node not been found\n");
}
//=============================================================
Syntax format: Order_link (TYPE * phead)
Implementation function: Using bubble method to sort the specified list by ordinal (Exchange data field)
Parameters: Phead: The first address of the chain to be sorted
Return value: A sorted list of phead pointers
//=============================================================
Type *order_link (Type *phead)
{
TYPE *pb,*pf,temp;
PB = pf =phead;
if (Phead = NULL)
return NULL;
while (Pb->next!= NULL)
{
PF = pb->next;
while (PF!= NULL)
{
if (Pb->num > Pf->num)
{
temp = *PB;
*PB = *PF;
*PF = temp;
Temp.next = pb->next;
Pb->next = pf->next;
Pf->next = Temp.next;
}
PF = pf->next;
}
PB = pb->next;
}
return phead;
}
//=============================================================
Syntax format: Reverse_link (TYPE * phead)
Implementation function: Order sequence by Ordinal for a given list
Parameters: Phead: The first address of the chain to be sorted
Return value: A sorted list of phead pointers
//=============================================================
Type *reverse_link (Type *phead)
{
TYPE *PB, *PF, *temp;
PB = Phead;
PF = pb->next;
while (PF!= NULL)
{
temp = pf->next;
Pf->next = PB;
PB = PF;
PF = temp;
}
Phead->next = NULL;
Phead = PB;
return phead;
}
//=============================================================
Syntax format: Free_all (TYPE * phead)
Implementation function: Release all nodes in the linked list
Parameters: Phead: List of linked lists to be released
return value: None
//=============================================================
void Free_all (TYPE *phead)
{
TYPE *p;
while (Phead!=null)
{
p=phead->next;
Free (phead);
Phead=p;
}
}
//=============================================================
Syntax format: merge (TYPE *p1,type *p2)
Implementation function: Two linked lists are merged in ascending order
Parameters: P1,p2 Two-generation merged linked list
Return value: Merged linked list
//=============================================================
Type *merge_link (Type *P1, type *P2)
{
TYPE *p, *phead;
if (P1 = NULL)
return p2;
if (P2 = NULL)
return p1;
if (P1->num < P2->num)
{
Phead = P = p1;
P1 = p1->next;
}
Else
{
Phead = p = p2;
P2 = p2->next;
}
while (P1!= null && p2!= null)
{
if (P1->num < P2->num)
{
P->next = p1;
p = p1;
P1 = p1->next;
}
Else
{
P->next = p2;
p = p2;
P2 = p2->next;
}
}
if (P1!= NULL)
P->next = p1;
Else
P->next = p2;
return phead;
}
//=============================================================
Realization method: Using recursion
Syntax format: merge (TYPE *p1,type *p2)
Implementation function: Two linked lists are merged in ascending order
Parameters: P1,p2 Two-generation merged linked list
Return value: Merged linked list
//=============================================================
Type * MERGE_LINK_SELF (type *P1, type *P2)
{
TYPE *phead = NULL;
if (P1 = NULL)
return p2;
if (P2 = NULL)
return p1;
if (P1->num < P2->num)
{
Phead = p1;
Phead->next =merge_link (P1->next, p2);
}
Else
{
Phead = p2;
Phead->next = Merge_link (P1, P2->next);
}
return phead;
}
int main (void)
{
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.