C-language single-link list implementation

Source: Internet
Author: User

/************************************************************************* > File name:singlelinetable.c > Au thor:zshh0604 > Mail: [email protected] > Created time:2014 October 15 Wednesday 11:34 08 seconds *************************** /#include <stdio.h> #include <stdlib.h> #include < string.h>/*** * Single linked list.     * Student Structure: * ID: Student number * Name: Student name * Math: Score * Next: point to next student struct */typedef struct student {    int id;    Char name[20];    int math; struct student * NEXT;} stu;typedef int Cmp_stu (const void *, const void *);/**** * Function Function: * Create a head node. * Function Parameters: * void. * Return value of function: * Returns the head node pointer.    */stu * Create (void) {Stu *head = NULL;    Stu *p = NULL;    Stu *new = NULL;    int tmpid = 0;    Char tmpname[20];            int Tmpmath;    Head = (stu*) malloc (sizeof (STU)); if (head = = NULL) {printf ("Allocating Stu address space failed!")

!!

\ n "); return NULL; } head->id = Tmpid; printf ("name ="); scanf ("%s", tmpname); strncpy (head->name,tmpname,20); printf ("math ="); scanf ("%d", &tmpmath); Head->math = Tmpmath; Head->next = NULL; p = head; When the head is created, you should point the pointer to the header.

while (1) {new = (stu*) malloc (sizeof (STU)); if (new==null) {printf ("malloc new error\n"); return NULL; } tmpid++; if (tmpid = = 3) {break; } new->id = Tmpid; printf ("name="); scanf ("%s", tmpname); strncpy (new->name,tmpname,20); printf ("math="); scanf ("%d", &tmpmath); New->math = Tmpmath; P->next = new; p = new; New->next = NULL; } return head; /*** * Function: * Print out the data in a single linked list.

* Function Parameters: * Head is the header of the linked list.

* Return Value: * No return value */void printf_list (Stu *head) {stu *tmp = NULL; int i = 0; if (head== NULL) {return; } tmp = head; while (tmp!=null) {i++; printf ("name =%s\n", tmp->name); printf ("math =%d\n", Tmp->math); TMP = tmp->next; } printf ("Len =%d\n", i);} /***** * Function Function: * comparison function.

* Function Parameters: * * Function return value: * Returns 0 indicates success.

* Return 1 indicates failure? * */int CMP (const void * data, const void * key) {Stu * head = NULL; int * Tmpkey =null; Head = (stu*) data; tmpkey= (int*) key; printf ("Head->id =%d, Tmpkey =%d", ((stu*) data)->id, *tmpkey); if (Head->id = = *tmpkey) {return 0; } return 1;} /**** * Function Function: * Find data in one node. * Function Parameters: * * Function return value: * Return 0 Lookup succeeded, return 1 lookup failed. */void * FIND_STU (stu* head,cmp_stu* cmps, const void *key) {stu * tmp = NULL; TMP = head; if (key = = null) {return null; } while (tmp! = NULL) {if (CMPS (const void *) TMP, (const void *) key) ==0) {printf ("name = %s\n ", tmp->name); printf ("math =%d\n", Tmp->math); return TMP; } TMP = tmp->next; } return NULL; /*** * Function Function: * Insert node.

* Function: * Head: A node in a linked list.

* NEW: The node that needs to be inserted. * Return value of function: * Returns 0 indicates successful insertion. * Return 1 indicates an insert failure. */int Insert_tool (stu* Head, stu* new) {if (head==null| | New = NULL) {return 1; } if (Head->next = = NULL) {head->next = new; New->next = NULL; } New->next = head->next; Head->next = new; return 0;} /*** * Function Function: * Compare by name. * Function Parameters: * Data, key is the value to look up in the data. * Return value of function: * Returns 0 success. 1 failed to return. */int cmp_name (const void *data, const void *key) {stu *tmp = NULL; char *tmpname = NULL; if (data== null | | key = = NULL) {return 1; } TMP = (STU *) data; Tmpname = (char *) key; if (strncmp (tmp->name,tmpname) ==0) {return 0; } return 1;} /*** * * Function function: * Insert a node into the linked list. * Function Parameters: * Head: The head node of the linked list. * Name: The names of the nodes to view. * Function return Value: * Returns 0 insert succeeded.

* Return 1 Insert failed. */int Insert_stu (stu* Head,char *name) {stu * tmp = NULL; Stu * new = NULL; Char tmpname[20]; int Tmpmath; TMP = (stu *) Find_stu (head,cmp_name,name); if (tmp = = NULL) {printf ("did not find the classmate \ n"); return 1; } new = (stu*) malloc (sizeof (STU)); printf ("name="); scanf ("%s", tmpname); strncpy (new->name,tmpname,20); printf ("math="); scanf ("%d", &tmpmath); New->math = Tmpmath; New->id = 10; Insert_tool (tmp,new); return 0;} /** * Function Function: * Delete the established node. * Number of participants: * Head: The head of the list * Name: To delete the student's name.

* return value. * 0 returns success. 1 failed to return.

*/int Delete_stu (stu * Head,char *name) {stu * back = NULL; Stu * p = NULL; p = head; while (p!=null) {back = P; p = p->next; if (strcmp (p->name,name) = = 0) {Back->next = p->next; P->next = NULL; Free (p); return 0; }} return 1;} /*** * Function function: * Destroy linked list. * Function: * The head of the linked list.

* The return value of the function * 0 indicates success.

1 indicates a failure to return. */int destory_list (stu* head) {stu *tmp; Stu *p; p = head; while (p!=null) {tmp = P; p = p->next; Tmp->next = NULL; printf ("name =%s", tmp->name); Free (TMP); }}int Main (void) {int i = 2; Stu * head = NULL; Head = Create (); Printf_list (head); Find_stu (Head,cmp,&i); Insert_stu (head, "BB"); Printf_list (head); printf ("----------------------\ n"); Destory_list (head); head = NULL; Printf_list (head); printf ("---------------------\ n");}



C-language single-link list implementation

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.