C language has a head loop single linked list

Source: Internet
Author: User
Tags strcmp

/************************************************************************* > 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>/*** * Circular 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;s Truct student * NEXT;} stu;typedef int Cmp_stu (const void *, const void *);/**** * Function Function: * Create a head node.

* Function Parameters: *void. * Function return Value: * 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 ("Failed to allocate Stu address space! ... \ n "); return NULL;} Head->id = 0;strncpy (Head->name, "n"); Head->math = 0;//head->next = NULL; Single-linked list head->next = head; A single linked list with head loops 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 ("\nname="), scanf ("%s", Tmpname), strncpy (new->name,tmpname,20);p rintf ("math="); scanf ( "%d", &tmpmath); new->math = Tmpmath;p->next = new; p = new;//new->next = NULL; Single-linked list new->next = head; A single-linked list with head loops}return head;} /*** * Function: * Print out the data in a single linked list. * Function Parameters: *head is the head of the list.

* Return Value: * No return value */void printf_list (Stu *head) {stu *tmp = Null;int i = 0;if (head== NULL) {return;} TMP = head->next; #if1//With Head loop single-linked list while (Tmp!=head) {i++;p rintf ("name =%s\n", tmp->name);p rintf ("math =%d\n", tmp- >math); tmp = Tmp->next;} #elsewhile (Tmp!=null)//single-linked list {i++;p rintf ("name =%s\n", tmp->name);p rintf ("math =%d\n", tmp->math); tmp = tmp-> Next; #endifprintf ("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->next;if (key = = NULL) {return NU LL;} #if 1//Circular single-linked list if (CMPS (const void *) head, (const void *) key) = = 0) {printf ("name =%s\n", tmp->name);p rintf ("math =%d\n" , Tmp->math); return tmp;} while (tmp! = head) {if (CMPS (const void *) TMP, (const void *) key) (==0) {printf ("name =%s\n", tmp->name);p rintf ("math =% D\n ", Tmp->math); return tmp;} TMP = Tmp->next;} #else//single-linked list while (tmp! = NULL) {if (CMPS (const void *) TMP, (const void *) key) ==0) {printf ("name =%s\n", tmp->name);p RI NTF ("math =%d\n", tmp->math); return tmp;} TMP = tmp->next; #endifreturn 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 1//Cycle single-link list if (Head->next = = head) {head->next = New;new->next = head;} #else//single-linked list if (Head->next = = NULL) {Head->next = new; new->next = NULL;} #endifnew->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) {Retu RN 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 list. *name: The name of the node 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_s Tu (head,cmp_name,name), if (tmp = = NULL) {printf ("The classmate was not found \"); return 1;} New = (stu*) malloc (sizeof (STU));p rintf ("Name="), scanf ("%s", Tmpname), strncpy (new->name,tmpname,20);p rintf (" 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->next; #if 1//Circular single-link list if (strcmp (p-&gt ; name,name) ==0) {Head->next = P->next;p->next = Null;free (p); return 0;} while (P! = head) {back = P; p = p->next; if (strcmp (p->name,name) = = 0) {Back->next = P->next;p->next = Null;f REE (p); return 0;}} #else//single-linked list while (p!=null) {back = P; p = p->next; if (strcmp (p->name,name) = = 0) {Back->next = P->next;p->nex t = Null;free (p); return 0;} #endifreturn 1;} /*** * function function: * Destroy linked list. * Function: * The head of the linked list. * The return value of the function *0 indicates a successful return.

1 indicates a failure to return. */int destory_list (stu* head) {stu *tmp;stu *p;p = Head->next;while (p!=head) {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 ();p rintf_list (head); Find_stu (head,cmp,&i); Insert_stu (Head, " BB ");p rintf_list (head);p rintf ("----------------------\ n ");d estory_list (head); head = Null;printf_list (head); printf ("---------------------\ n");}


C language has a head loop single linked list

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.