Two different insertion methods for the linked list

Source: Internet
Author: User

# Include <stdio. h>
# Include <stdlib. h>

Typedef struct Node
{
Int data;
Struct node * pnext;
} Node, * pnode; // node is equivalent to struct node pnode and is equivalent to struct node *

Pnode create_list (); // create a non-circular linked list
Int traverse_list (pnode phead); // traverse
Void sort_list (pnode phead, int Len); // sort
Int delete_list (pnode, INT); // Delete
Int insert_list (pnode); // insert
Int is_empty (pnode );

Int main ()
{
Pnode phead = NULL;
Int Len = 0;

Phead = create_list ();
Len = traverse_list (phead );
// Sort_list (phead, Len );
// Delete_list (phead, Len );
Insert_list (phead );

Free (phead );
Return 0;
}

Pnode create_list ()
{
Pnode pnew; // create a node
Pnode ptail; // table tail
Pnode phead; // Header
Int Len;
Int I;

Phead = (pnode) malloc (sizeof (node ));
If (phead = NULL)
{
Printf ("out of memory, program exited! ");
Exit (-1 );
}
Phead-> DATA = 0; // initialize the header Node
Phead-> pnext = NULL;
Ptail = phead;
Printf ("Enter the number of linked list nodes to be generated: Len = ");
Scanf ("% d", & Len );
For (I = 0; I <Len; I ++)
{

Pnew = (pnode) malloc (sizeof (node ));
If (pnew = NULL)
{
Printf ("out of memory, program exited! ");
Exit (-1 );
}
Printf ("Enter the % d Data:", I + 1 );
Scanf ("% d", & pnew-> data );
Pnew-> pnext = NULL;
Ptail-> pnext = pnew;
Ptail = pnew;

}
// Free (pnew );
Return phead;

}

Int traverse_list (pnode phead)
{
Pnode P;
Int Len = 0;
P = phead-> pnext;
While (P! = NULL)
{
Printf ("% d,", p-> data );
P = p-> pnext;
Len ++;
}
If (P! = Phead-> pnext)
{
Printf ("\ B. \ n ");
Printf ("number of nodes: % d \ n", Len );
Return Len;
}
Else
Return is_empty (phead );
}

Void sort_list (pnode phead, int Len)
{
Pnode p, q;
Int I, j, T;
If (phead-> pnext = NULL) Exit (-1 );
For (I = 0, P = phead-> pnext; I <len-1; ++ I, P = p-> pnext)
For (j = I + 1, q = p-> pnext; j <Len; ++ J, q = Q-> pnext)
{
If (p-> DATA> q-> data)
{
T = p-> data;
P-> DATA = Q-> data;
Q-> DATA = T;
}
}
Printf ("sorted linked list: \ n ");
For (P = phead-> pnext; P! = NULL; P = p-> pnext)
Printf ("% d", p-> data );
}

Int is_empty (pnode phead)
{
If (phead-> pnext = NULL)
{
Printf ("the linked list is empty! ");
Return 1;
}
Else
Return 0;
}

Int delete_list (pnode phead, int Len)
{
Int Val, N, I;
Pnode P = phead-> pnext, q = phead;
If (P = NULL) return 0;
Printf ("\ n enter the element to be deleted and its location :");
Scanf ("% d", & Val, & N );
For (I = 1; I! = N & I <= Len; ++ I)
{
P = p-> pnext; q = Q-> pnext;
}
// Printf ("\ n % d, % d, I = % d \ n", p-> data, Q-> data, I );
If (I = N & P-> DATA = Val)
{
Q-> pnext = p-> pnext; // P points to the current node to be deleted, and Q points to its precursor Node
Free (P );
}
Else
{
Printf ("incorrect input. The corresponding node or location cannot be found! ");
Return 0;
}
Printf ("You have deleted the % d node % d! \ N ", N, Val );
Traverse_list (phead );
Return 1;

}

Int insert_list (pnode phead)
{
Pnode P, pnew;
Int I = 1, Val, N;
Printf ("the Val will be inserted before the nth node :");
Scanf ("% d", & Val, & N );
P = phead;

While (P! = NULL & I <n)
{
P = p-> pnext;
I ++;
}
If (I> N | P = NULL)
Return 0;
Pnew = (pnode) malloc (sizeof (node ));
If (pnew = NULL)
{
Printf ("memory full, insertion failed! ");
Return 0;
}
Pnew-> DATA = val;
Pnew-> pnext = NULL;
Pnew-> pnext = p-> pnext;
P-> pnext = pnew;
Traverse_list (phead );
Return 1;
}

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.