Data Structure linked list operation 1

Source: Internet
Author: User
/*************************************** * *********************************/* April 8, 2013 19:40:12 */ /*************************************** * ********************************/# include <stdio. h> # include <malloc. h> # include <stdlib. h> typedef struct node {int data; // data field struct node * pnext; // pointer field} node, * pnode; pnode create_list (void ); // create void traverse_list (pnode phead); // traverse bool is_empty (pnode phead); // judge whether it is null int Length_list (pnode); // calculate the length of bool insert_list (pnode, Int, INT); // insert a value bool delete_list (pnode, Int, int *); // delete a void sort_list (pnode); // sort int main (void) {pnode phead = NULL; phead = create_list (); // create a non-cyclic single-chain table traverse_list (phead); int Len = length_list (phead); printf ("the length of the linked list is % d \ n", Len ); insert_list (phead, 4,33); sort_list (phead); printf ("sorted linked list \ n"); traverse_list (phead); If (is_empty (phead )) printf ("the linked list is empty! \ N "); elseprintf (" the linked list is not empty! \ N "); Return 0;} pnode create_list (void) {int Len; // used to store the number of valid nodes int I; int val; // use Alibaba Cloud to temporarily store the value of the User-input node pnode phead = (pnode) malloc (sizeof (node); If (null = phead) {printf ("Allocation failed, program termination \ n "); exit (-1);} pnode ptail = phead; ptail-> pnext = NULL; printf ("Enter the number of nodes in the linked list to be generated; Len ="); scanf ("% d", & Len); for (I = 0; I <Len; I ++) {printf ("Enter the value of node % d:", I + 1); scanf ("% d", & Val ); pnode pnew = (pnode) malloc (sizeof (node); If (null = pnew) {print F ("Allocation failed, Program terminated \ n"); exit (-1) ;}pnew-> DATA = val; ptail-> pnext = pnew; pnew-> pnext = NULL; ptail = pnew;} return phead;} void traverse_list (pnode phead) {pnode P = phead-> pnext; while (null! = P) {printf ("% d", p-> data); P = p-> pnext;} printf ("\ n");} bool is_empty (pnode phead) {If (null = phead-> pnext) return true; elsereturn false;} int length_list (pnode phead) {pnode P = phead-> pnext; int Len = 0; while (null! = P) {++ Len; P = p-> pnext;} return Len;} void sort_list (pnode phead) {int I, j, T; int Len = length_list (phead); pnode p, q; 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 ;}}return ;}// Insert a new node in front of the POs node of the linked list pointed to by phead, the node value is valbool insert_list (pnode phead, int POs, int Val) {int I = 0; pnode P = phead; while (Nu Ll! = P & I <pos-1) {P = p-> pnext; + I;} if (I> pos-1 | null = P) return false; pnode pnew = (pnode) malloc (sizeof (node); If (null = pnew) {printf ("dynamic memory allocation failed! \ N "); exit (-1) ;}pnew-> DATA = val; pnode q = p-> pnext; P-> pnext = pnew; pnew-> pnext = Q; return true;} bool delete_list (pnode phead, int POs, int * pval) {int I = 0; pnode P = phead; while (null! = P-> pnext & I <pos-1) {P = p-> pnext; + + I;} if (I> pos-1 | null = p-> pnext) return false; pnode q = p-> pnext; * pval = Q-> data; P-> pnext = p-> pnext; free (P ); Q = NULL; return true ;}

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.