大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.5 KiB

  1. #include<stdio.h>
  2. // 定义链表
  3. struct Node
  4. {
  5. int data;
  6. struct Node* next;
  7. };
  8. struct Node* head;
  9. // 在链表头节点插入一个值为x的节点
  10. void Insert(int x){
  11. struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
  12. temp->data = x;
  13. temp->next = head;
  14. // if(head != NULL)temp->next = head;
  15. head = temp;
  16. }
  17. // 迭代法实现反转列表
  18. struct Node* Reverse1(struct Node* head){
  19. struct Node *Current,*Prev,*next;
  20. Current = head;
  21. Prev = NULL;
  22. while(Current != NULL){
  23. next = Current->next;
  24. Current->next = Prev;
  25. Prev = Current;
  26. Current = next;
  27. }
  28. head = Prev;
  29. return head;
  30. }
  31. // 递归法实现反转列表
  32. void Reverse2(struct Node* p){
  33. if(p->next ==NULL){
  34. head = p;
  35. return;
  36. }
  37. Reverse2(p->next);
  38. struct Node* q = p->next;
  39. q->next = p;
  40. p->next = NULL;
  41. }
  42. // 链表内指定区间反转 调试好了
  43. struct Node* reverseBetween(struct Node* head, int m, int n ) {
  44. // write code here
  45. if(head == NULL)return head;
  46. if(head->next == NULL)return head;
  47. if(m == n)return head;
  48. struct Node *Current,*Prev,*next,*start,*start_last;
  49. int i;
  50. Current = head;
  51. Prev = NULL;
  52. next = NULL;
  53. // 先找到开始位置
  54. for (i=1; i<m; i++) {
  55. next = Current->next;
  56. // Current->next = Prev;
  57. Prev = Current;
  58. Current = next;
  59. }
  60. // 标记
  61. start_last = Prev;
  62. start = Current;
  63. // 反转
  64. for (i=0; i<(n-m+1); i++) {
  65. next = Current->next;
  66. Current->next = Prev;
  67. Prev = Current;
  68. Current = next;
  69. }
  70. // 头尾节点重指向
  71. if(start != head){
  72. start->next = next;
  73. start_last->next = Prev;//start!=head的情况下,需要保留start上一个指针
  74. }
  75. else {
  76. start->next = next;
  77. head = Prev;//start==head的情况下,直接将head指向待反转的最后一个
  78. }
  79. return head;
  80. }
  81. //打印链表的所有值
  82. void Print(){
  83. struct Node* temp = head;
  84. printf("List is:");
  85. while(temp){
  86. printf("%d",temp->data);
  87. temp = temp->next;
  88. }
  89. printf("\n");
  90. }
  91. void Print2(struct Node*p){
  92. if(p ==NULL){
  93. printf("\n");
  94. return;
  95. }
  96. // 正序打印
  97. // printf("%d",p->data);
  98. // Print2(p->next);
  99. // 反转打印
  100. printf("%d",p->data);
  101. Print2(p->next);
  102. }
  103. //计算数组的大小,传递数组其实传的地址会改变原来的值
  104. int RetureLen(int array[], int size){
  105. array[0] = 0;
  106. return size;
  107. }
  108. int main(){
  109. int a;
  110. char b;
  111. char str[10] = "zzwxxy";
  112. char str2[10];
  113. int array[] = {1,2,3,4,5,6,7,8,9};
  114. int len = RetureLen(array, sizeof(array) / sizeof(array[0]));
  115. printf("%d", array[0]);
  116. // printf("please input string\n");
  117. // gets(str2);
  118. // printf("input string is:\n%s", str2);
  119. }
  120. /*
  121. *
  122. char string[] = "zhang";
  123. char string[] = {'z','h','a','n','g'};
  124. char str[] = {zhang};
  125. */
  126. /*
  127. scanf和gets的区别
  128. 使scanf("%s", str2); gets(str2);
  129. scanf tab键会自动在字符串后面添加\0tab键仍会留在输入的缓冲区中
  130. gets\0 \n.
  131. printf()puts():
  132. printf("input string is:\n%s", str2); puts(str2);
  133. */
  134. /*
  135. int length = sizeof(array) / sizeof(array[0]);
  136. #include<string.h> int length = strlen(strArray);
  137. */