牛客网 链表结构 算法相关内容

链表结构

  • 单链表的节点结构  由以下结构的节点依次连接起来所形成的链叫单链表结构
Clas Node<V>{ V value; Node next; 
} 
  • 双链表的节点结构 由以下结构的节点依次连接起来所形成的链叫双链表结构
Clas Node<V>{ V value; Node next; Node last; 
} 
  •  单链表和双链表结构只需要给定一个头部节点head,就可以找到剩下的所有的节点

反转单向和双向链表

  • [题目] 分别实现反转单向链表和反转双向链表的函数
  • [要求] 如果链表长度为N,时间复杂度要求为O(N),额外空间复杂度要求为 O(1)
package com.example.algorithm.demo.class04;public class Code02_ReverseList {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}public static Node reverseList(Node head) {Node pre = null;Node next = null;while (head != null) {next = head.next;head.next = pre;pre = head;head = next;}return pre;}public static class DoubleNode {public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data) {this.value = data;}}public static DoubleNode reverseList(DoubleNode head) {DoubleNode pre = null;DoubleNode next = null;while (head != null) {next = head.next;head.next = pre;head.last = next;pre = head;head = next;}return pre;}public static void printLinkedList(Node head) {System.out.print("Linked List: ");while (head != null) {System.out.print(head.value + " ");head = head.next;}System.out.println();}public static void printDoubleLinkedList(DoubleNode head) {System.out.print("Double Linked List: ");DoubleNode end = null;while (head != null) {System.out.print(head.value + " ");end = head;head = head.next;}System.out.print("| ");while (end != null) {System.out.print(end.value + " ");end = end.last;}System.out.println();}public static void main(String[] args) {Node head1 = new Node(1);head1.next = new Node(2);head1.next.next = new Node(3);printLinkedList(head1);head1 = reverseList(head1);printLinkedList(head1);DoubleNode head2 = new DoubleNode(1);head2.next = new DoubleNode(2);head2.next.last = head2;head2.next.next = new DoubleNode(3);head2.next.next.last = head2.next;head2.next.next.next = new DoubleNode(4);head2.next.next.next.last = head2.next.next;printDoubleLinkedList(head2);printDoubleLinkedList(reverseList(head2));}}

打印两个有序链表的公共部分

  • [题目] 给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。
  • [要求] 如果两个链表的长度之和为N,时间复杂度要求为O(N),额外空间复杂度要求为O(1)
  • [思路] head1指向第一个有序链表,head2指向第二个有序链表,比较元素的大小,如果相等打印元素的数值。否则,指针指向的元素的数值小的率先移动,比较大小,元素数值小的移动
package com.example.algorithm.demo.class04;public class Code03_PrintCommonPart {public static class Node{public int value;public Node next;public Node(int data){this.value = data;}}public static void printCommonPart(Node head1,Node head2){System.out.println("Common Part: ");while (head1 != null && head2 != null){if (head1.value < head2.value){head1 = head1.next;} else if(head2.value < head1.value){head2 = head2.next;} else{System.out.print(head1.value+" ");head1 = head1.next;head2 = head2.next;}}System.out.println();}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}public static void main(String[] args) {Node node1 = new Node(2);node1.next = new Node(3);node1.next.next = new Node(5);node1.next.next.next = new Node(6);Node node2 = new Node(1);node2.next = new Node(2);node2.next.next = new Node(5);node2.next.next.next = new Node(7);node2.next.next.next.next = new Node(8);printLinkedList(node1);printLinkedList(node2);printCommonPart(node1, node2);}
}

面试时链表解题的方法论

  • 1)对于笔试,不用太在乎空间复杂度,一切为了时间复杂度
  • 2)对于面试,时间复杂度依然放在第一位,但是一定要找到空间最省的方法

重要技巧:

  • 1)额外数据结构记录(哈希表等)
  • 2)快慢指针

快速找到链表的中点

  • 快慢指针的方式,慢指针每次移动一个长度,快指针每次移动二个长度。当快指针到达链表的末尾,慢指针到达链表的中点。
  • 问题:奇数偶数判定不一样的,需要细节定制

回文结构

  • 将链表的后半部分放到栈里边,存储的是后半部分数据的逆序
  • 面试考虑内存空间,进一步优化。找到链表的中点之后,将中点的下一个指针指向nullptr,然后将中点之后的节点的下一个指针进行逆向排序,如下图所示,主要目的是为了节省空间。然后两个指针一个从头 一个从尾进行依次读取数据进行比较,如果一致满足回文结构。但是最后需要将链表进行还原,恢复成 1 -> 2 -> 3 -> 2 ->1 的结构
  • 1 -> 2 -> 3 <- 2 <- 1
  • 第二种方式 初始化的时候,将right初始化为head->next;将cur初始化为head节点;主要目的是为了将慢指针移动到中心节点的右边,然后将中心节点开始的位置到null这段区间的节点都拷贝到栈空间
package class04;import java.util.Stack;public class Code04_IsPalindromeList {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}// need n extra spacepublic static boolean isPalindrome1(Node head) {Stack<Node> stack = new Stack<Node>();Node cur = head;while (cur != null) {stack.push(cur);cur = cur.next;}while (head != null) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;}// need n/2 extra spacepublic static boolean isPalindrome2(Node head) {if (head == null || head.next == null) {return true;}Node right = head.next;Node cur = head;while (cur.next != null && cur.next.next != null) {right = right.next;cur = cur.next.next;}Stack<Node> stack = new Stack<Node>();while (right != null) {stack.push(right);right = right.next;}while (!stack.isEmpty()) {if (head.value != stack.pop().value) {return false;}head = head.next;}return true;}// need O(1) extra spacepublic static boolean isPalindrome3(Node head) {if (head == null || head.next == null) {return true;}Node n1 = head;Node n2 = head;while (n2.next != null && n2.next.next != null) { // find mid noden1 = n1.next; // n1 -> midn2 = n2.next.next; // n2 -> end}n2 = n1.next; // n2 -> right part first noden1.next = null; // mid.next -> nullNode n3 = null;while (n2 != null) { // right part convertn3 = n2.next; // n3 -> save next noden2.next = n1; // next of right node convertn1 = n2; // n1 moven2 = n3; // n2 move}n3 = n1; // n3 -> save last noden2 = head;// n2 -> left first nodeboolean res = true;while (n1 != null && n2 != null) { // check palindromeif (n1.value != n2.value) {res = false;break;}n1 = n1.next; // left to midn2 = n2.next; // right to mid}n1 = n3.next;n3.next = null;while (n1 != null) { // recover listn2 = n1.next;n1.next = n3;n3 = n1;n1 = n2;}return res;}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}public static void main(String[] args) {Node head = null;printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(2);head.next.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(2);head.next.next.next.next = new Node(1);printLinkedList(head);System.out.print(isPalindrome1(head) + " | ");System.out.print(isPalindrome2(head) + " | ");System.out.println(isPalindrome3(head) + " | ");printLinkedList(head);System.out.println("=========================");}}

将单链表按照给定的数值,划分成左边小、中间相等、右边大的形式 (荷兰国旗)

思路 方法一

  • 1,申请一个链表长度大小一致的数组,然后遍历链表的过程中,将每一个点放到数组容器中,在数组里面进行荷兰国旗问题,再把数组容器中的元素都串联起来,形成一个链表返回
  • 第一种方法使用的排序 方式思路以及代码如下:
  • 设置 small 从左边开始移动,只要small对应位置的元素的数值小于pivot(用户输入的数值),small就进行移动,同时移动index,使small和index同时向后移动;注意++small index-- ;和初始赋值有关系
  • big从右边开始移动;只有index对应位置上的元素的数值 大于 用户输入的数值,就需要交换 index上的数值  和  big指向的数值;将大的数移动到右边,--big,index不要动,接下来检查index的数值
  • 如果index 指向的数值  和  用户输入的数值相等,只需要移动index
    public static void arrPartition(Node[] nodeArr,int pivot){int small = -1;int big = nodeArr.length;int index = 0;while (index != big){if (nodeArr[index].value < pivot){swap(nodeArr,++small,index++);} else if (nodeArr[index].value == pivot){++index;} else{swap(nodeArr,--big,index);}}}

思路 方法二 

  • 2,保证稳定性:根据题目要求设置三个区,小于区、等于区和大于区,每个区间包含两个变量,开始节点和结尾节点,一共设置6个变量,6个变量都指向null;当遇到第一个元素3的时候,它是小于指定元素5的,将小于区域的开始和结束指针都指向3;第二个元素是5,属于等于区,让等于区的start和end都指向第二个元素5;第3个元素也是5,让等于区的end只想第三个元素。以此类推,得到上面的结果。最后将小于区域的end指针链接等于区域的start指针,等于区域的end指针链接大于区域的start指针。
  • 需要注意的是,有可能上面6个变量有不存在的情形,需要仔细判别。
  • 链表天然具有稳定性  保持元素的前后位置的一致性;相较于方法一,避免了数据的交换

package com.example.algorithm.demo.class04;import org.w3c.dom.Node;public class Code05_SmallerEqualBigger {public static class Node {public int value;public Node next;public Node(int data){this.value = data;}}public static void swap(Node[] nodeArr,int a,int b){Node tmp = nodeArr[a];nodeArr[a] = nodeArr[b];nodeArr[b] = tmp;}public static void arrPartition(Node[] nodeArr,int pivot){int small = -1;int big = nodeArr.length;int index = 0;while (index != big){if (nodeArr[index].value < pivot){swap(nodeArr,++small,index++);} else if (nodeArr[index].value == pivot){++index;} else{swap(nodeArr,--big,index);}}}public static Node listPartition1(Node head,int pivot){if (head == null){return head;}Node cur = head;int i = 0;while (cur != null){i++;cur = cur.next;}Node[] nodeArr = new Node[i];i = 0;cur = head;for (i = 0; i != nodeArr.length;i++){nodeArr[i] = cur;cur = cur.next;}arrPartition(nodeArr,pivot);for (i = 1;i != nodeArr.length;i++){nodeArr[i-1].next = nodeArr[i];}nodeArr[i-1].next = null;return nodeArr[0];}public static Node listPartition2(Node head,int pivot){Node sH = null;Node sT = null;Node eH = null;Node eT = null;Node bH = null;Node bT = null;Node next = null;while (head != null){next = head.next;head.next = null;if (head.value < pivot){if (sH == null){sH = head;sT = head;} else {sT.next = head;sT = head;}} else if(head.value == pivot){if (eH == null){eH = head;eT = head;} else {eT.next = head;eT = head;}} else {if (bH == null){bH = head;bT = head;} else {bT.next = head;bT = head;}}}head = next;if (sT != null){sT.next = eH;eT = eT == null ? sT : eT;}if (eT != null){eT.next = bH;}return sH != null ? sH : eH != null ? eH : bH;}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}public static void main(String[] args) {Node head1 = new Node(7);head1.next = new Node(9);head1.next.next = new Node(1);head1.next.next.next = new Node(8);head1.next.next.next.next = new Node(5);head1.next.next.next.next.next = new Node(2);head1.next.next.next.next.next.next = new Node(5);printLinkedList(head1);head1 = listPartition1(head1, 4);
//        head1 = listPartition2(head1, 5);printLinkedList(head1);}
}

复制含有随机指针节点的链表

  • 【题目】一种特殊的单链表节点类描述如下

    public static class Node{public int value;public Node next;public Node rand;public Node(int data){this.value = data;}}
  • rand指针是单链表节点结构中新增的指针,rand可能指向链表中的任意一个节点,也可能指向null。
  • 给定一个由Node节点类型组成的无环单链表的头节点 head,请实现一个函数完成这个链表的复制,并返回复制的新链表的头节点。
  • 【要求】时间复杂度O(N),额外空间复杂度O(1)
  • 方法1 利用map,key和value都是Node类型;使用新建的类型为Node的Value节点存储节点的下一个和随机指针,然后抛弃先前的链表,使用map存储的记录实现链表的拼接;map.get(cur).next = map.get(cur.next);
    map.get(cur).rand = map.get(cur.rand);分别指定节点的下一个和随机指针
  • 方法2 遍历链表,在每个节点的后面创建对应的副本;然后将每一个新建的节点进行串联,形成拷贝;
        //创建节点 填入元素Node cur = head;Node next = null;while (cur != null){next = cur.next;  //指向新创建的节点的下一个cur.next = new Node(cur.value);  //新建节点cur.next.next = next;  //衔接新创建的节点和先前节点,相当于在指定位置插入节点cur = next;  //指向旧的节点}//分离节点Node res = head.next;cur = head;while (cur != null){next = cur.next.next;curCopy = cur.next;cur.next = next;curCopy.next = next != null ? next.next : null;cur = next;}
Codepackage class04;import java.util.HashMap;public class Code06_CopyListWithRandom {public static class Node {public int value;public Node next;public Node rand;public Node(int data) {this.value = data;}}public static Node copyListWithRand1(Node head) {HashMap<Node, Node> map = new HashMap<Node, Node>();Node cur = head;while (cur != null) {map.put(cur, new Node(cur.value));cur = cur.next;}cur = head;while (cur != null) {map.get(cur).next = map.get(cur.next);map.get(cur).rand = map.get(cur.rand);cur = cur.next;}return map.get(head);}public static Node copyListWithRand2(Node head) {if (head == null) {return null;}Node cur = head;Node next = null;// copy node and link to every nodewhile (cur != null) {next = cur.next;cur.next = new Node(cur.value);cur.next.next = next;cur = next;}cur = head;Node curCopy = null;// set copy node randwhile (cur != null) {next = cur.next.next;curCopy = cur.next;curCopy.rand = cur.rand != null ? cur.rand.next : null;cur = next;}Node res = head.next;cur = head;// splitwhile (cur != null) {next = cur.next.next;curCopy = cur.next;cur.next = next;curCopy.next = next != null ? next.next : null;cur = next;}return res;}public static void printRandLinkedList(Node head) {Node cur = head;System.out.print("order: ");while (cur != null) {System.out.print(cur.value + " ");cur = cur.next;}System.out.println();cur = head;System.out.print("rand:  ");while (cur != null) {System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");cur = cur.next;}System.out.println();}public static void main(String[] args) {Node head = null;Node res1 = null;Node res2 = null;printRandLinkedList(head);res1 = copyListWithRand1(head);printRandLinkedList(res1);res2 = copyListWithRand2(head);printRandLinkedList(res2);printRandLinkedList(head);System.out.println("=========================");head = new Node(1);head.next = new Node(2);head.next.next = new Node(3);head.next.next.next = new Node(4);head.next.next.next.next = new Node(5);head.next.next.next.next.next = new Node(6);head.rand = head.next.next.next.next.next; // 1 -> 6head.next.rand = head.next.next.next.next.next; // 2 -> 6head.next.next.rand = head.next.next.next.next; // 3 -> 5head.next.next.next.rand = head.next.next; // 4 -> 3head.next.next.next.next.rand = null; // 5 -> nullhead.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4printRandLinkedList(head);res1 = copyListWithRand1(head);printRandLinkedList(res1);res2 = copyListWithRand2(head);printRandLinkedList(res2);printRandLinkedList(head);System.out.println("=========================");}}

两个单链表相交的一系列问题

  • 【题目】给定两个可能有环也可能无环的单链表,头节点head1和head2。请实现一个函数,如果两个链表相交,请返回相交的 第一个节点。如果不相交,返 回null
  • 【要求】如果两个链表长度之和为N,时间复杂度请达到O(N),额外空间复杂度 请达到O(1)。
  • 两个单链表相交的一系列问题
  • 相交:共用内存地址  
  • 注意事项:每个节点只有next指针,不可以有两个指出的指针
  • 有环/无环:写一个函数,输入的类型是链表的头节点,判断是否有环,以及返回第一个入环的节点
  • 方法一:使用集合来做,遍历链表,每遍历一个元素,将其放入到集合中,判定是否存在此元素,如果将一个元素放入集合中,发现他已经存在,则是有环的,并且他就是第一个入环节点。
  • 方法二:使用快慢指针来做,快指针每次移动俩个位置,慢指针每次移动一个位置当快慢指针第一次相交之后,快指针回到链表的起始点,慢指针呆在原地不动。然后快指针和慢指针都每次移动一个位置,当他们再次相遇之后,相遇的元素就是入环的第一个元素,且能证明这个链表是有环的结构。上图所示的是环外4个点,环内4个点,可以按照这个流程走一遍。
  • 使用函数(返回第一个入环的节点)如果返回的是null 说明链表无环;如果两个链表都无环,找到两个链表最后的位置,如果最后的位置相等 ,说明链表相交;否则链表不想交;两个无环链表相交问题,哪个链表长,先走比短链表多余的部分,然后一起走,只要有一点的内存地址相稳合,则代表两者相交。

  • 如果一个是有环的,一个是无环的,那么他俩一定不相交  不可以出现两个指出的指针

  • 两个都有环的 链表 ,可以出现三种情况
  • 第一种情形,使用其中一个入环节点绕着环走一圈,如果可以找到另外的入环节点就是第三种情形;否则就是第一种情形
  • 第二种情形,将第一个入环节点作为判断终止的条件

package class04;public class Code07_FindFirstIntersectNode {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}public static Node getIntersectNode(Node head1, Node head2) {if (head1 == null || head2 == null) {return null;}Node loop1 = getLoopNode(head1);Node loop2 = getLoopNode(head2);if (loop1 == null && loop2 == null) {return noLoop(head1, head2);}if (loop1 != null && loop2 != null) {return bothLoop(head1, loop1, head2, loop2);}return null;}public static Node getLoopNode(Node head) {if (head == null || head.next == null || head.next.next == null) {return null;}Node n1 = head.next; // n1 -> slowNode n2 = head.next.next; // n2 -> fastwhile (n1 != n2) {if (n2.next == null || n2.next.next == null) {return null;}n2 = n2.next.next;n1 = n1.next;}n2 = head; // n2 -> walk again from headwhile (n1 != n2) {n1 = n1.next;n2 = n2.next;}return n1;}public static Node noLoop(Node head1, Node head2) {if (head1 == null || head2 == null) {return null;}Node cur1 = head1;Node cur2 = head2;int n = 0;while (cur1.next != null) {n++;cur1 = cur1.next;}while (cur2.next != null) {n--;cur2 = cur2.next;}if (cur1 != cur2) {return null;}cur1 = n > 0 ? head1 : head2;cur2 = cur1 == head1 ? head2 : head1;n = Math.abs(n);while (n != 0) {n--;cur1 = cur1.next;}while (cur1 != cur2) {cur1 = cur1.next;cur2 = cur2.next;}return cur1;}public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {Node cur1 = null;Node cur2 = null;if (loop1 == loop2) {cur1 = head1;cur2 = head2;int n = 0;while (cur1 != loop1) {n++;cur1 = cur1.next;}while (cur2 != loop2) {n--;cur2 = cur2.next;}cur1 = n > 0 ? head1 : head2;cur2 = cur1 == head1 ? head2 : head1;n = Math.abs(n);while (n != 0) {n--;cur1 = cur1.next;}while (cur1 != cur2) {cur1 = cur1.next;cur2 = cur2.next;}return cur1;} else {cur1 = loop1.next;while (cur1 != loop1) {if (cur1 == loop2) {return loop1;}cur1 = cur1.next;}return null;}}public static void main(String[] args) {// 1->2->3->4->5->6->7->nullNode head1 = new Node(1);head1.next = new Node(2);head1.next.next = new Node(3);head1.next.next.next = new Node(4);head1.next.next.next.next = new Node(5);head1.next.next.next.next.next = new Node(6);head1.next.next.next.next.next.next = new Node(7);// 0->9->8->6->7->nullNode head2 = new Node(0);head2.next = new Node(9);head2.next.next = new Node(8);head2.next.next.next = head1.next.next.next.next.next; // 8->6System.out.println(getIntersectNode(head1, head2).value);// 1->2->3->4->5->6->7->4...head1 = new Node(1);head1.next = new Node(2);head1.next.next = new Node(3);head1.next.next.next = new Node(4);head1.next.next.next.next = new Node(5);head1.next.next.next.next.next = new Node(6);head1.next.next.next.next.next.next = new Node(7);head1.next.next.next.next.next.next = head1.next.next.next; // 7->4// 0->9->8->2...head2 = new Node(0);head2.next = new Node(9);head2.next.next = new Node(8);head2.next.next.next = head1.next; // 8->2System.out.println(getIntersectNode(head1, head2).value);// 0->9->8->6->4->5->6..head2 = new Node(0);head2.next = new Node(9);head2.next.next = new Node(8);head2.next.next.next = head1.next.next.next.next.next; // 8->6System.out.println(getIntersectNode(head1, head2).value);}}

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446565.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ primer 第8章 IO库

文章目录IO库类型和头文件IO对象无拷贝或赋值IO流的条件状态文件输入输出ifstream 示例ofstream 示例文件模式以out模式打开文件会丢弃已有数据每次调用open时都会确定文件模式ofstream 保留源文件 追加数据 示例string流istringstream示例ostringstream示例IO库类型和头文件 …

C++面试宝典 基本语言(三)

如果同时定义了两个函数&#xff0c;一个带const&#xff0c;一个不带&#xff0c;会有问题吗&#xff1f; 不会&#xff0c;这相当于函数的重载 #include<iostream> class A{ public:void print()const{std::cout << "Hello" << std::endl;}void…

C++ primer 第9章 顺序容器

文章目录顺序容器类型确定使用哪种顺序容器容器库概览容器操作迭代器迭代器支持的所有操作迭代器支持的所有运算迭代器范围对构成范围的迭代器的要求标准库迭代器范围左闭右开的三种性质容器定义和初始化将一个新容器创建为另一个容器的拷贝将array拷贝到vector中的代码与顺序容…

牛客网C++面经 容器和算法

原文网址 参考网址 C语言中文网 请你来说一下map和set有什么区别&#xff0c;分别又是怎么实现的&#xff1f; map和set都是C的关联容器&#xff0c;其底层实现都是红黑树&#xff08;RB-Tree&#xff09;。由于 map 和set所开放的各种操作接口&#xff0c;RB-tree 也都提供…

C++ primer 第10章 泛型算法

文章目录概述findcount初识泛型算法只读算法只读算法accumulate只读算法equal写容器元素的算法算法fill算法fill_nback_inserter算法copy算法replace replace_copy重排容器元素的算法sortuniqueunique_copy定制操作向算法传递函数谓词算法stable_sort算法partitionlambda表达式…

C语言常用字符串函数

概括 代码 #include<stdlib.h> #include<stdio.h> #include<string.h> int main() {//常用字符串函数char a[]"abcSDFbnm";char b[]"SD";printf("a的字符串长度:%d\n",strlen(a));printf("b的字符串长度:%d\n",str…

C++ primer 第11章 关联容器

文章目录使用关联容器map示例关联容器概述定义关联容器关联容器值初始化multimap和multiset关键字类型的要求pair类型pair上的操作关联容器操作关联容器额外的类型别名关联容器迭代器map迭代器set迭代器关联容器和算法添加元素向map添加元素检测insert的返回值使用insert代替下…

C++ primer 第12章 动态内存

文章目录前言动态内存与智能指针shared_ptr类shared_ptr和unique_ptr都支持的操作shared_ptr独有的操作make_shared 函数shared_ptr的拷贝和赋值shared_ptr自动销毁所管理的对象shared_ptr还会自动释放相关联的内存程序使用动态内存出于以下原因直接管理内存使用new动态分配和初…

C语言顺序查找二分查找

介绍 顺序查找 按照顺序一个个查找 #include<stdio.h> //顺序查找 int search(int arr[],int len,int aim) {int i;for(i0;i<len;i){if(arr[i]aim){return i;//返回下标 }}return -1;//表示未查询到} int main() {int arr[]{13,355,256,65,234,-1,35,-6,-3,-4,0};…

C++ primer 第12章 12.3 使用标准库:文本查询程序

文章目录使用标准库&#xff1a;文本查询程序文本查询程序设计数据结构在类之间共享数据自己的文本查询程序书中的文本查询程序使用标准库&#xff1a;文本查询程序 我们将实现一个简单的文本查询程序&#xff0c;作为标准库相关内容学习的总结。 我们的程序允许用户在一个给…

C语言二维数组 int arr[2][3]

基础使用 先遍历行再遍历列 #include<stdio.h> //二维数组的基本使用 int main() {//二维数组的初始化int arr1[2][2]{{2,2},{0,0}};int arr2[2][3]{2,2,2,8,8,8};int arr3[6][9];int i,j;for(i0;i<6;i){for(j0;j<9;j){arr3[i][j]1;}}arr3[2][5]0;//打印printf(&…

C++ primer 第13章 拷贝控制

文章目录前言拷贝、赋值与销毁拷贝构造函数合成拷贝构造函数拷贝初始化和直接初始化拷贝初始化的发生&#xff1a;参数和返回值拷贝初始化的限制拷贝赋值运算符重载赋值运算符合成拷贝赋值运算符析构函数析构函数完成的工作什么时候会调用析构函数合成析构函数代码片段调用几次…

C语言 指针自增自减加减运算 p++ p+i

介绍 自增自减代码 #include<stdio.h> #include<string.h> //指针自增--short void increase(short *arr,int len) {int i;arr&arr[0];for(i0;i<len;i){printf("arr[%d]%d,address%p\n",i,*arr,arr);arr;} }//指针自减--char void decrease(char…

C++ 编译与底层

原文链接 编译与底层请你来说一下一个C源文件从文本到可执行文件经历的过程&#xff1f; 对于C源文件&#xff0c;从文本到可执行文件一般需要四个过程&#xff1a;预处理阶段&#xff1a;对源代码文件中文件包含关系&#xff08;头文件&#xff09;、预编译语句&#xff08;…

C语言 指针数组-字符指针数组整型指针数组 char*s[3] int*a[5] 数组指针int(*p)[4]

基本介绍 1.指针数组:由n个指向整型元素的指针而组成,里面存放指针 Int *ptr[3]; 2.地址: ptr[i]:元素地址 &ptr[i]:指针地址 图示 代码: 内存布局: 代码 #include<stdio.h> #include<string.h> //指针数组--int void pointer(int *arr,int len) {int …

C语言 多重指针--整型字符字符串 int**pp

介绍 多重指针:一个指针指向另一个指针 离值越近的指针级别越大:一级 内存布局 代码 图示: 多重指针–整型 #include<stdio.h> #include<string.h> //多重指针--整型//二级指针 void two() {printf("二级指针:\n");int a896;int *p&a,**pp&…

C++ primer 第13章 拷贝控制

文章目录前言拷贝、赋值与销毁拷贝构造函数合成拷贝构造函数拷贝初始化和直接初始化拷贝初始化的发生&#xff1a;参数和返回值拷贝初始化的限制拷贝赋值运算符重载赋值运算符合成拷贝赋值运算符析构函数析构函数完成的工作什么时候会调用析构函数合成析构函数代码片段调用几次…

牛客网C++面经 C++11

请问C11有哪些新特性&#xff1f; auto关键字&#xff1a;编译器可以根据初始值自动推导出类型。但是不能用于函数传参以及数组类型的推导nullptr关键字&#xff1a;nullptr是一种特殊类型的字面值&#xff0c;它可以被转换成任意其它的指针类型&#xff1b;而NULL一般被宏定义…

C语言 返回指针的函数--指针函数 int* max(int a)

定义 strlong示例代码 代码1: #include<stdio.h> #include<string.h> //返回指针的函数//比较两个字符串,返回更长的字符串 char *strlong(char* a,char* b) {char *p1&a[0];char *p2&b[0];while(true){if(*p1\0){return b;}else if(*p2\0){return a;}p1…

第2、3讲 图像的存储格式

本图像处理系列笔记是基于B站杨淑莹老师的课程进行学习整理的。 文章目录黑白图像8位灰度索引图像8位伪彩色索引图像24位真彩色图像图像文件格式BMP文件存储格式BMP文件头位图信息头颜色表位图信息——BITMAPINFO结构BMP位图文件汇总按照颜色深度分类&#xff0c;常用图像文件&…