C language of One-way link list detailed and instance code _c language

Source: Internet
Author: User

1, one-way chain concise.

One-way linked list (single linked list) is a kind of linked list, it is characterized by the link direction of the linked list is one-way, the access to the linked list from the head to read sequentially; The list is constructed using pointers, or node lists, because the linked lists are assembled by a single node. ; Each node has a pointer member variable that refers to the next node in the list; The list is made up of nodes, and the head pointer points to the first node that becomes the header and terminates with the last pointer to null;

2, example requirements:

Based on the example in the example code, complete the insertion, deletion and lookup of the linked list with string data in the one-way list (single linked list), and support the inversion of one-way list;

3, code implementation.

#include <stdio.h> #include <math.h> #include <cstring> #include <memory.h> #include <malloc .h>//Node definition typedef struct Node {void *data;//data domain//link domain struct node *next;


Nodestruct, *pnode;

 


Pnode head = NULL;
typedef char (*PCOMPAREFUNC) (void *a, void *b);


typedef void* (*pchar) (void *p);
 string to judge int str_compare (void *a, void *b) {char *pa = (char*) A;
 Char *PB = (char*) b;
Return strcmp (PA, Pb);
 }//Assign a node Pnode allocate_node (void *data, Pchar char_func) {pnode node = allocate ();
 Node->data = Char_func (data);
return node;
 }//Create node Pnode allocate () {void *p = malloc (sizeof (nodestruct));
 Pnode node = (pnode) p;
 Node->next = NULL;
 Node->data = NULL;
return node;
  }//Add a node void Insertnode (Pnode node) {if (head = = null) {Head=node;
  else {node->next = head;
  Head = node;
 } void* Char_char (void *p) {char* pa = (char*) malloc (sizeof (char));
 memcpy (PA, p, sizeof (char));
Return PA;
}//Initialize nodePnode allocate_node (void *data, Pchar char_func) {pnode node = allocate ();
 Node->data = Char_func (data);
return node;


 }//release resource void Free_list (Pnode node) {Pnode next = node;
  while (next!= null) {if (next->data!= null) free (next->data);
  Pnode temp = next;
  Next = next->next;
 Free (temp);
 }//1.1 Add a node void Insert (Pnode node) {if (head = = NULL) head = node;
  else {node->next = head;
 Head = node;
 //1.2 Find int str_search (void* data,pcomparefunc Compare) {pnode next = head;
 Pnode prev = NULL;
   while (next!= NULL) {if (compare (data, next->data) = = 0) {//if found, Exit returns 1 return 1;
  Break
  } prev = next;
 Next = next->next;
//If you have not been found, return 0 returns 0;
 }//1.3 delete node void remove (void* data,pcomparefunc Compare) {pnode next = head;
 Pnode prev = NULL;
    while (next!= null) {if (compare (data, next->data) = = 0) {if (prev = = null) {head = next->next;
 Next->next = NULL;   Free_list (next);
    else {Prev->next = next->next;
    Next->next = NULL;
   Free_list (next);
  } break;
  } prev = next;
 Next = next->next;
  }//1.4 reverse void Invert_order () {node *this,*prev;
  P=head.next;
  This=null;
    while (p) {prev=this;
    This=p;
    p=p->next;


  this->next=prev;
} head.next=this;
 } void Main () {//1 one-way list char a1[] = ' AAA1 ';
 Char a2[] = ' AAA2 ';
 Char a3[] = ' Aaa3 ';
 1.1 Add Insertnode (Allocate_node (A1, Init_char));
 Insertnode (Allocate_node (A2, Init_char));
 Insertnode (Allocate_node (A3, Init_char));
 1.2 Find int flag = 0;
 Flag = Str_search (&a2,str_compare);
 1.3 Delete Remove (&a2,str_compare);


1.4 Reverse Invert_order ();


 

 }

  above is the  c language one-way linked table data collation, follow-up to continue to supplement the relevant information, thank you for your support of this site!

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.