Algorithm course (second)

Source: Internet
Author: User

1. delete an element from the linked list with no header pointer.
First, keep the value of the following element, delete the following element, and assign its value to the previous one)
Assert (P! = NULL)

2. Given the head pointer, put the linked list upside down.
1) Check each value from the back to the top
2) You only need to exchange the values in each table, and do not understand the pointer.
3) use three pointers to ensure that the header pointer is not inverted.
# Include <iostream>
Using namespace STD;

Typedef struct tagnode {
Char data;
Struct tagnode * next;
} Node;

Typedef node * List;

List reverse (list l)
{
If (L = NULL)
Exit (0 );
If (L-> next = NULL)
Exit (0 );

Node * P = L-> next; // start from the next node of the original node.
Node * q = p-> next;
Node * r = NULL;
While (q ){
R = Q-> next;
Q-> next = P;
P = Q;
Q = R;
}
L-> next = NULL;
L-> next = P;

Return L;
}

Void printlist (list l)
{
If (L = NULL)
Exit (0 );

Node * P = L;
While (p ){
Cout <p-> data;
Cout <"----> ";
P = p-> next;
}
Cout <"null" <Endl;
}

List createlist (int n)
{
List L = NULL;
Node * P = NULL, * q = NULL;

// Create a header node so that the insertion of the first node does not require special processing.
L = (node *) malloc (sizeof (node ));
If (! L)
Exit (0 );
L-> DATA = 'H ';
L-> next = NULL;

P = L;
While (n --){
Q = (node *) malloc (sizeof (node ));
If (! Q)
Exit (0 );
Char C;
Cin> C;
Q-> DATA = C; // set the data domain.
Q-> next = p-> next; // set the link domain.
P-> next = Q; // link to the node.
P = Q; // This sentence indicates sequential insertion. If this sentence is left blank, it indicates reverse insertion.
}

Return L;
}

Int main ()
{
List L = createlist (3 );
Printlist (L );
L = reverse (L );
Printlist (L );
Return 0;
}

4. How to determine whether the two linked list headers are intersection given.
1) obtain: For each pointer in H1, check whether it is in H2.
2) Statistical Table query: Hash Sorting. query the hash table for each address.
3) connect the 2nd linked lists to the back of the first list and determine whether the form ring is used.
4) Determine whether the last node of the two tables is the same

5. Two values are known, and the lowest common ancestor of the ball
1) Use two linked lists to store all the ancestors and find the public minimum.
2) between two values, compare the size

6. N nodes and M-edge undirected connected graph. The number of deleted edges is still connected.
The smallest spanning tree has N-1 edges, so removing M-(N-1) entries is still

7. The number of n elements is K, and the number exceeds half. Calculate this number (complexity O (n ))
1) pairing, leaving the same, losing the difference, and asking for the rest
2) For (I = COUNT = 0; I <n; I ++ ){

If (I = COUNT = 0) S = A [I];

If (A [I] = s) {count ++ ;}
Else count --;

}
The last S is what we want

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.