#include <stdio.h>
#include <stdlib.h>
typedef struct type
{
int num;
struct type *next;
}TYPE;
//=============================================================
//Syntax: TYPE *init_link_head(int n)
//From beginning to end, the positive order creates a linked list with n nodes and initializes its value
//Parameters: & have spent N: the length of the list, that is, the number of nodes
//Return value: & NBSP; The first address of the 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: TYPE *init_link_end(int n)
//Function: from the end of the end, the reverse order to create a list of n nodes, and the value of the initialization
//Parameters: & have spent N: the length of the list, that is, the number of nodes
//Return value: & NBSP; The first address of the 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;
}
//=============================================================
//Insert_link (TYPE * phead,TYPE * PI)
//Function: add the newly applied node to the specified linked list
//Parameters: & have spent *phead: to be inserted into a linked list
//& have spent & have spent & have spent * PI: with insert node
//Return value: & NBSP; The first address of the new list after insertion of 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: delete_link(TYPE * phead,int num)
//Function: delete the node pointed to by the given sequence number
//Parameters: & have spent *phead: list to be deleted
//& have spent & have spent & have spent Num: the node that needs to be deleted
//Return value: & NBSP; 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 linkn");
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 deletedn");
}
else
printf("the node not foundn");
return phead;
}
//=============================================================
//Syntax: print_link(TYPE * phead)
//Print all node data in the specified linked list
//Parameters: & have spent *phead: the first address of the list to be printed
//Return value: & NBSP; There is no
//=============================================================
void print_link(TYPE *phead)
{
TYPE *temp = phead;
while( temp != NULL)
{
printf(" %d ",temp->num);
temp = temp->next;
}
}
//=============================================================
//Syntax: search_num(TYPE * phead, int num)
//Function: find the specified element by name in the specified linked list
//Parameters: & have spent Phead: the first address of the chain to be found, the string that num needs to find
//Return value: & NBSP; There is no
//=============================================================
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 foundn");
}
//=============================================================
//Syntax: order_link(TYPE * phead)
//Function: using bubble method to sort the specified linked list by order number (exchange data domain)
//Parameters: & have spent Phead: the first address of a chain to be sorted
//Return value: & NBSP; A sorted linked list phead pointer
//=============================================================
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;
}
//=============================================================
//Reverse_link (TYPE * phead)
//Function: to the given list in reverse order by the order number
//Parameters: & have spent Phead: the first address of a chain to be sorted
//Return value: & NBSP; A sorted linked list phead pointer
//=============================================================
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: free_all(TYPE * phead)
//Function: release all nodes in the linked list
//Parameters: & have spent Phead: the first address of a list to be released
//Return value: & NBSP; There is no
//=============================================================
void free_all(TYPE *phead)
{
TYPE *p;
while(phead!=NULL)
{
p=phead->next;
free(phead);
phead=p;
}
}
//=============================================================
//Merge (TYPE *p1,TYPE *p2)
//Function: merge the two linked lists in ascending order
//Parameters: & have spent A linked list of two generations of p1 and p2
//Return value: & NBSP; The combined 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;
}
//=============================================================
//Implementation method: & NBSP; & have spent Using the recursive
//Merge (TYPE *p1,TYPE *p2)
//Function: merge the two linked lists in ascending order
//Parameters: & have spent A linked list of two generations of p1 and p2
//Return value: & NBSP; The combined 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;
}