C ++ day15 Study Notes

Source: Internet
Author: User

1. In the header file

 
# Ifndef _ account _//Pre-compile option, indicating that if this macro is not defined# Define_ Account _//Create a macro named _ account _And declare the class# Endif 

2. Linked List
(1) solve the problem that arrays must be stored continuously
The linked list can be discontinuous and connected through the pointer of each node.
(2) A part of the space in a node is used to store data, and the other part is a pointer to the next node.
(3) Each node is a structure

StructNode {IntData;//Store DataNode * next;//The pointer to the next node. It is the type of its own structure.}

(4) tail node --- the last node in the linked list --- pointer pointing to null
Header node-to access elements in the linked list, you must know the position of the header node.
Place the address in a pointer. the header Pointer Points to the header node. It is just a pointer. It is a required element.
(5) common operations on the linked list-add, delete, modify, and query
(6) differences between linked lists and Arrays
Array: the space must be continuous, and the array is fixed. inserting and deleting an array requires traversing the entire array, which is inefficient.
You can directly use subscript to retrieve elements for convenient access.
Linked list: the space does not need to be consecutive in the memory. It is connected by a pointer.
The linked list is not long. You can add new nodes at any time and associate them with pointers.
Insert or delete a linked list. You only need to operate on pointers without moving the node location.
Access element, which must be traversed from the beginning

When data needs to be inserted and deleted frequently, you need to use a linked list.
When the changes are not large and the query frequency is frequent, the array is used.
Potential Rule: linked lists are not required when arrays are used.

View code

========================================================== ==================================== Link. h ========================================================== ==================================== # Ifndef _ link _  # Define _ Link _ Using   Namespace  STD;  Class Node { //  Node          Public :  Int Val; //  Save data Node * next; //  Save the address of the next node Node (){ //  The constructor initializes the pointer to null. Next = Null ;}};  Class  Link {  Protected  : Node * Head;//  Header pointer          Public  : Link (); ~ Link ();  Void Inserttail ( Int  );  Void Inserthead ( Int  );  Void Del ( Int  );  Int Indexof ( Int ); //  Queries the subscript of an element.               Void Update ( Int , Int  );  Void  Disp ();};  # Endif 
View code

========================================================== ==================================== Link. CC ========================================================== ==================================== # Include  "  Link. h  "  # Include <Iostream> Using   Namespace  STD; Link: Link () {head = NULL;} link :: ~ Link (){ //  Release space from the beginning to the end              If (Head! =Null) {Node * P = Head; head = Head-> next; //  Move the head node backward Delete P; //  Discard the original header Node Cout < "  Delete one...  " < Endl ;}}  //  End insert          Void Link: inserttail ( Int  V) {Node * P = New  Node; P -> Val = V;  If (Head = Null) {head = P; //  Point the pointer of the new node to the new node, that is, save the address of the new node in the header pointer.                     Return  ;} Node * Temp = head; // Use a temporary pointer to locate the end of the node                While (Temp-> next! = NULL ){ //  Indicates that temp is not the last node. Temp = temp-> next; //  Assign a value to yourself using a pointer next to temp, that is, point to the next node.  } Temp -> Next = P; //  Insert at the end. The pointer of the last node saves the address of the new node.  }  //  Insert Header           Void Link: inserthead (Int  V) {Node * P = New Node; //  Create a node P-> val = V; //  Save data P-> next = head; //  Let the pointer of the new node point to the first node like the header pointer. Head = P; //  Point the header node to the new node  }  Void Link: del (Int V ){ //  Find the deleted node,                 If (Head = Null ){  Return  ;}  If (Head-> val = V) {Node * P = Head; head = Head-> Next; Delete head;} Node * P1 = head-> next;//  Find one with the same value Node * P2 = head; //  Following p1                 While (P1! = Null ){  If (P1-> val = V) {p2 -> Next = p1-> Next; Delete P1;  Break  ;} P1 = P1->Next; p2 = P2-> Next ;}}  Int Link: indexof ( Int V ){ //  Queries the subscript of an element. Node * P = Head;  Int Counter = 0  ;  While (P! = Null ){ If (P-> val = V ){  Return  Counter;} p = P-> Next; Counter ++ ;}  Return - 1  ;}  Void Link: Update ( Int V1, Int V2) {Node * P = Head;  While (P! = Null ){  If (P-> val = V1) {P -> Val = V2;} p = P-> Next ;}}  Void  Link: disp () {Node * P =Head;  While (P! = Null) {cout <P-> Val < "   "  ; P = P-> Next;} cout < Endl ;} 

3. Binary Tree
Each node has a tree with a maximum of two branches. It has a root pointer pointing to the root node (the top node) of the tree ).
The value of the Left subtree is smaller than the value of its parent node, and the value of the right subtree is greater than the value of its parent node. --- Sort Binary Tree
(1) traveling (traversal): First Order --- center left and right
Middle Order --- left middle right
Backward --- left and right
(2) Easy to find

Common Operations on the binary search tree:
1) insert. ExampleCodeAs follows:

View code

Node * tree: _ insert ( Int V, node * r ){ //  Truly implement the insert operation and return the inserted Root     If (R = NULL ){ //  Is an empty tree) Node * P = New Node (v ); //  Create a node R = P; //  Make the new node A root or subnode          Return  R ;}  If (V <r-> Val ){ //  Insert to left subtree R-> left = _ insert (v, R->Left );  Return  R ;}  Else { //  Insert to right subtree R-> right = _ insert (v, R-> Right );  Return  R ;}} 

2) search. The sample code is as follows:

View code

Node * & find (bnode * & root, Const Data & CD ){  If (Root = NULL) //  If the root node is empty, the tree is empty.              Return Root; //  Returns the address indicated by root, that is, null.          Else   If (Root-> DATA = CD) //  If the root node is the value to be searched              Return Root; //  Returns the address pointed to by the root user. For clarity, it is written separately from the preceding address.          Else   If (CD <root-> data) //  If the root node points to a value greater than the value to be searched              Return Find (root-> left, Cd ); //  Returns the address returned by the Left subtree of the root user.          Else              Return Find (root-> right, Cd ); //  Otherwise, the system returns the address returned by the right subtree of the root user. }

3) delete. The sample code is as follows:
If the tree root is deleted (1), the tree root of the right subtree is selected as the new tree root. The left subtree can be hung on a left node on the far left of the right subtree.
The leftmost node in the right subtree is the root of the Left subtree.
(2) create the largest node in the left subtree as the new root.

View code

Node * _ del ( Int Value, node * R ){  If (R = NULL ){ //  Delete empty tree              Return  R ;} If (R-> value = value ){ //  Delete Root               If (R-> left = r-> right ){ //  When both left and right subtree are null  Delete R;  Return  NULL ;}  Else   If (R-> right = NULL ){ //  Only the right subtree. Node * P =R; R = R-> Left; Delete P;  Return  R ;}  Else   If (R-> left = NULL ){ //  Only right subtree, no left subtree Node * P = R; R = R-> Right; Delete P;  Return  R ;} Else { //  Left and right subtree Node * P1 = r-> Right; Node * P2 = r-> Right;  While (P2-> left! = Null) {p2 = P2-> Left;} p2 -> Left = r-> Left; Delete R;  Return P1 ;}}  If (Value <= r-> Value) {r -> Left = _ del (value, R-> Left );  Return  R ;}  Else  {R -> Right = _ del (value, R-> Right );  Return  R ;}  Return  R ;} 

job: Modify the linked list Program to delete all identical elements. insert data at the specified position.

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.