Create, insert, and delete a single-chain table in C Language

Source: Internet
Author: User
# Include <stdio. h> # include <stdlib. h> typedef struct node {int data; // data field node * Next; // pointer field, pointing to the next node} node; node * Create (); // create a single-chain table int deletefromlist (node * linklist, int I); // If the node is deleted successfully, 0 is returned; otherwise,-1int insertintolist (node * linklist, int I, int value) is returned ); // If the node is successfully inserted, 0 is returned. Otherwise,-1 void display (node * linklist) is returned. // traverse and print the linked table void main () {node * linklist = create (); // display (linklist); insertintolist (linklist, 5, 30); display (Lin Klist); deletefromlist (linklist, 5); display (linklist);} node * Create () {int n = 20; node * plist, * pnew, * ptail; plist = (node *) malloc (sizeof (node); plist-> next = NULL; ptail = plist; // For (INT I = 1; I <= N; I ++) {pnew = (node *) malloc (sizeof (node); If (pnew = NULL) {printf ("error !! "); Exit (0);} pnew-> DATA = I; pnew-> next = NULL; ptail-> next = pnew; ptail = pnew ;} ptail-> next = NULL; return plist;} int insertintolist (node * linklist, int I, int value) {// first find whether the node * P, * q; int J = 1; // count P = linklist; while (P & J <I) // find the I-th node {P = p-> next; + + J;} If (! P | j> I) {return-1; // the I-th node does not exist. insertion Failed} // now P is in the 4th position, p-> next points to the fifth value q = (node *) malloc (sizeof (node); q-> DATA = value; q-> next = p-> next; // mount the fifth value and subsequent linked list to Q-> next pointer p-> next = Q; // link Q and the link above and below to Q-> next to P-> next return 0;} int deletefromlist (node * linklist, int I) {// check whether node * PTR exists on node I; Int J = 1; PTR = linklist; while (PTR-> next & J <I) {PTR = PTR-> next; ++ J;} If (! (PTR-> next) | j> I) {return-1; // node I does not exist} PTR-> next = PTR-> next; return 0;} void display (node * linklist) {While (linklist-> next) {printf ("% d \ n", linklist-> next-> data ); linklist = linklist-> next ;}}

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.