Chain table struct

Source: Internet
Author: User
# Include <stdio. h> # define type struct Stu # define Len sizeof (struct Stu) struct Stu {int num; int age; struct Stu * Next;}; type * creatlink (int n ); type * deletelink (type * head, int num); Type * insertlink (type * head, type * PI); void printlink (type * head ); void destroylink (type * head); void main (void) {type * head = NULL, * pnum = NULL; int n = 3, num; /* Create a linked list containing N nodes */printf ("input number of node:"); Head = Creatlink (n); printlink (head);/* Delete the node with the value of num in the linked list */printf ("input the deleted number:"); scanf ("% d ", & num); Head = deletelink (Head, num); printlink (head);/* Insert a node into the linked list */printf ("insert a record \ n "); pnum = (type *) malloc (LEN); If (pnum = NULL) printf ("pointer is null -- Memory alloc fail! "); Printf (" input the inserted number: "); scanf (" % d ", & pnum-> num); printf (" input the inserted age :"); scanf ("% d", & pnum-> age); Head = insertlink (Head, pnum); printlink (head);/* destroy the linked list, release dynamically allocated memory */destroylink (head);}/* Create a linked list containing N nodes */type * creatlink (int n) {type * head = NULL, * PF, * pb; int I; for (I = 0; I <n; I ++) {Pb = (type *) malloc (LEN ); printf ("record % d \ n", I); printf ("input number:"); scanf ("% d", & Pb-> Num); printf ("input age:"); scanf ("% d", & Pb-> age); if (I = 0) pF = head = Pb; else PF-> next = Pb; Pb-> next = NULL; pF = Pb;} return (head );} /* Delete the node with the value of num in the linked list */type * deletelink (type * head, int num) {type * PF, * pb; If (Head = NULL) {printf ("\ nempty list! \ N "); return NULL;} Pb = head; while (Pb-> num! = Num & Pb-> next! = NULL) {pF = Pb; Pb = Pb-> next;} If (Pb-> num = num) {If (PB = head) head = Pb-> next; else PF-> next = Pb-> next; free (PB); printf ("the node is deleted \ n ");} else printf ("the node not been found! \ N "); return head;}/* Insert a node in the linked list */type * insertlink (type * head, type * PI) {type * pb, * PF; PB = head; If (Head = NULL) {head = PI; Pi-> next = NULL;} else {While (Pi-> num> Pb-> num) & (Pb-> next! = NULL) {pF = Pb; Pb = Pb-> next;} If (Pi-> num <= Pb-> num) {If (Head = Pb) head = PI; else PF-> next = PI; Pi-> next = Pb;} else {Pb-> next = PI; Pi-> next = NULL ;}} return head;}/* print the node information in the linked list */void printlink (type * head) {printf ("number \ t \ Tage \ n"); While (Head! = NULL) {printf ("% d \ t % d \ n", head-> num, head-> age); Head = head-> next ;}} /* destroy the linked list and release the dynamically allocated memory */void destroylink (type * head) {type * P, * q; P = head; while (P! = NULL) {q = p-> next; free (p); P = Q ;}}

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.