链表任意交换两个元素(Linked lists exchange two elements at will).docVIP

  • 4
  • 0
  • 约3.82千字
  • 约 10页
  • 2017-08-18 发布于河南
  • 举报

链表任意交换两个元素(Linked lists exchange two elements at will).doc

链表任意交换两个元素(Linked lists exchange two elements at will)

链表任意交换两个元素(Linked lists exchange two elements at will) Algorithm function: the establishment of single linked list, exchange single chain table of the two elements. 3 * algorithm single chain table is the leading point of the node. 4 * function description: NOP 5 * @author:xiaoq-ohmygirl 6 * @time: 2012-06-20 7 **/ 08.#include stdio.h 09.#include malloc.h 10.#include stdlib.h 11.#define MAXNODE 10 Twelve 13.typedef struct node{ 14. int data; 15. node* next; 16.}*linkNode, *linkList; Seventeen Eighteen 19.// creates a single linked list with header nodes. 20.int createLinkList (linkList L, int, n) { 21. L = (linkList) malloc (sizeof (linkNode)); 22. if ({L) { 23. return 0; 24.} 25. L-next = NULL; 26. linkList p; 27. for (int i = n; I 0; i--) { 28. P = (linkList) malloc (sizeof (linkNode)); 29. scanf (%d, p-data); 30. p-next = L-next; 31. L-next = p; 32.} 33. return 1; 34.} Thirty-five 36.void visit (linkNode, node) { 37. printf (%d-, node-data); 38.} Thirty-nine 40.void, getNewLine () { 41. printf (\n); 42.} 43.// single linked list of leading nodes 44.void traverseLink (linkList, L) { 45. linkNode cur = L-next; 46. getNewLine (); 47. while (cur = = NULL) { 48. visit (cur); 49. cur = cur-next; 50.} 51. getNewLine (); 52.} Fifty-three Fifty-four 55.// looks for the first K node in the list. 56.linkNode findNode (const, linkList, L, int, K) { 57. linkNode node = L; 58. for (int = I = 1; i=k; i++) { 59. node = node-next; 60.} 61. return node; 62.} Sixty-three 64.linkNode findPre (const, linkList, head, const, linkNode, node) { 65. linkNode TMP = head; 66. while (tmp-next = = node) { 67. TMP = tmp-next; 68.} 69. return tmp; 70.} Seventy-one 72./* 73. * two nodes are exchanged according to the precursor of two nodes 74. * there are situations to consider: 75. * 1., if node 1 is the same as node 2, no interchange is required 76. * 2. if one of the nodes is the head node, it is not exchanged in principle 77. * 3. if two nodes are adjacent, you need to do some special proc

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档