单链表增删改查(A single list of crud).docVIP

  • 3
  • 0
  • 约4.28千字
  • 约 12页
  • 2017-08-18 发布于河南
  • 举报

单链表增删改查(A single list of crud).doc

单链表增删改查(A single list of crud)

单链表增删改查(A single list of crud) The structure of a single linked list is the simplest of the data structures, each of which has only one pointer to the latter. Single linked list creation: Typedef struct node { Int data; / / node content Node *next; / / the next node }node; / / create a single list Node, *create () { Int i=0; / / the number of data in the list Node, *head, *p, *q; Int x = 0; Head = (node * malloc) (sizeof (node)); / / create a head node While (1) { Printf (Please input the data:); Scanf (%d, x); If (0 = = x) //data for the creation of the end of 0 { Break; } P = (node *) malloc (sizeof (node)); P-data = x; If (++i = = 1) / / list of only one element { Head-next = p; / / head is connected to the back } Else { Q-next = p; / / connected to the end of the list } Q = p; //q points to the last node } Q-next = NULL; / / the last pointer list for NULL Return head; } In the code above, the while cycle each time an integer data read from the terminal, and call malloc dynamic allocation list node memory the integer data, then in the end is inserted into the single list; finally when the data of 0 represents the insertion end of data the end node, next pointer to NULL. / / single chain length measurement Int length (node *head) { Int len = 0; Node *p = head-next; While (NULL = = P) { Len++; P = p-next; } Return len; } / / print list Void print (node *head) { Int POS = 0; Node *p = NULL; If (NULL = head-next) { Printf (Link, is, empty, \n); Return; } P = head-next; While (NULL! = P) / / traversing the list { Printf (The,%dth, node,%d\n, is:, ++pos, p-data); P = p-next; } } / / single list node search / / node search POS position, returns the node pointer //pos starts at 0, and 0 returns the head node Node *search_node (node, *head, int, POS) { Node *p = head-next; If (POS 0) //pos is not in the right position { Printf (incorrect, position, to, search, node, \n); Return NULL; } If (POS = 0) { Return head; } If (NULL = P) { Printf (Link is empty \n!); / / list is

文档评论(0)

1亿VIP精品文档

相关文档