All kinds of operation test interviews for a single-chain table, Single-Chain Test
I. Differences between linked lists and Arrays
1. The logical structure array must have a fixed length defined in advance and cannot be dynamically increased or decreased. The linked list is dynamically allocated memory.
2. The memory structure array allocates space from the stack to the linked list in the heap.
3. Sequential storage of arrays in memory linked list Random storage array access efficiency high linked list insertion and deletion convenience
4. array out-of-bounds problems
Define the table header. To facilitate the operation of the table header, the data field is empty.
Ii. Single-chain table insertion and Deletion
Bool insertList (List head, Type x, int index ){
Node * p;
P = head;
Int j = 1;
While (p-> next & j <index ){
P = p-> next;
++ J;
}
If (p = NULL) return ERROR;
Node * tmp = (Node *) malloc (sizeof (Node ));
Tmp-> data = x;
Tmp-> next = p-> next; // the successor of tmp points to the original index Node
P-next = tmp; // p is followed by a new node.
Return OK;
}
Bool deleteList (List head, int index ){
Node * p = head;
Int j = 1;
While (p-> next! = NULL & j <I ){
P = p-> next;
++ J;
} // P-> next indicates the index number.
If (p = NULL | p-> next = NULL | j> I) return ERROR; // I may be negative
Node * tmp = p-> next; // tmp points to the nth index
P-> next = tmp-> next; // replace index with index
Free (tmp); // release space
Return OK;
}
3. Find the last k elements in a single-chain table
Solutions
1. traverse and calculate the length, and then find the last k
2. The dual-path Qi Xia p1 starts from the first node, p2 starts from the k to traverse until p2 goes to the header
Idea 2 code:
Node * find (Node * head, int index ){
Node * p1, * p2;
P1 = head;
P2 = head;
For (int I = 0; I <index; I ++ ){
P2 = p2-> next; // p2 moves k bits to the back
}
While (p2! = NULL ){
P1 = p1-> next;
P2 = p2-> next;
}
Return p1;
}
Iv. Single-chain table reversal
Solution: Mark the front node and the back node of each operation to operate the node.
Node * reverse (Node * head ){
Node * prev = NULL;
Node * pCur = head;
Node * rHead = NULL;
While (pCur! = NULL ){
Node * pNext = head-> next;
If (pNext = NULL)
RHead = pCur;
PCur-> next = prev; // change the order. The current node points to its previous node.
Prev = pCur; // the current node changes to the previous node (for the next node)
PCur = pNext; // move the node back
}
}
5. Output A Single-chain table from the end to the end
1. Recursive output
2. Non-recursive output
Non-recursive operation:
1). You can use the "4" code to reverse and then output
2) traverse nodes into the stack and then output
6. Search for the intermediate node of a single-chain table
1. traverse and find the length and then traverse the output.
2. Dual-path Qi Xia p1 take one step p2 take two steps p2 go to the end point when p1 just to the midpoint
Node * findMiddle (Node * head ){
Node * p1 = head;
Node * p2 = head;
While (p2-> next! = NULL ){
P2 = p2-> next;
P1 = p1-> next;
}
Return p1;
}
VII. Sorting of Single-Chain tables
It is not much different from array sorting. When an element is exchanged, only the content of the data domain can be exchanged. The Sequence Structure of the linked list does not need to be changed.
8. exchange any two elements in a single-link table (note the header)
When switching two nodes, pay attention to the next pointer exchange problem.
// Switch the p q Node
Node * swap (Node * head, Node * p, Node * q ){
If (head = NULL | p = NULL | q = NULL ){
Print (ERROR );
Return head;
}
If (p = q ){
Print (OK );
Return head;
}
Else if (p-> next = q) {// two adjacent vertices
Node * prev = getPrev (p );
P-> next = q-> next;
Q-> next = p;
Prev-> next = q;
}
Else if (q-> next = p ){
Node * prev = getPrev (q );
Q-> next = p-> next;
P-> next = q;
Prev-> next = p;
}
Else if (p! = Q) {// two nodes are not adjacent and pointer fields before and after different exchanges
Node * prev1 = getPrev (p );
Node * prev2 = getPrev (q );
Node * next1 = p-> next;
Node * next2 = q-> next;
P-> next = next2;
Prev2-> next = p;
Q-> next = next1;
Prev1-> next = q;
}
Return head;
}
9. check whether a large single-chain table has a ring
Dual-channel integration, one step, One step, two steps to see if they will meet
10. Determine whether two (no-ring) Single-Chain tables overlap
Determine whether the last node is the same
11. Delete duplicate nodes in a single-chain table
Hash. We recommend that you use a hash_map. If the nodes of the traversal link are repeated, delete it.
12. merge two ordered linked lists (non-crossover)
1. Recursion
Node * Merge (Node * head1, Node * head2 ){
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
If (head1-> data Head = head1;
Head-> next = Merge (head1, head2 );
} Else {
Head = head2;
Head-> next = Merge (head1, head2 );
}
Return head;
}
2. Non-recursion
Node * Merge (Node * head, Node * head1, Node * head2 ){
Node * tmp = head;
While (head1! = NULL & head2! = NULL ){
If (head1-> data Tmp-> next = head1; // point to the current node of head1
Tmp = head1; // move the tmp location
Head1 = head1-> next; // move head1 back
} Else {
Tmp-> next = head2;
Tmp = head2;
Head2 = head2-> next;
}
}
If (head1 = NULL)
Tmp-> next = head2;
If (head2 = NULL)
Tmp-> next = head1;
Return head;
}
Operations on a single-chain table?
Single Linked List Implementation
The single-item linked list template independent of MFC, I will not talk about it if it is redundant. It provides a function to demonstrate its usage. I hope it can help readers who have the same requirements.
// Queue. h: interface for the CQueue class.
# If! Defined (afx_queue_h000057c46373_d485_43f2_998c_d3ea4984e59a0000included _)
# Define afx_queue_h000057c46373_d485_43f2_998c_d3ea4984e59a00000000ded _
# If _ MSC_VER> 1000
# Pragma once
# Endif // _ MSC_VER> 1000
Template <class T>
Class CQueue
{
Public:
CQueue ();
Virtual ~ CQueue ();
Int AddHead (T * pT );
Int AddTail (T * pT );
Int Insert (int nIndex, T * pElement); // Insert after the nIndex Element
Int GetSize ();
BOOL GetAt (int nIndex, T & Element );
BOOL RemoveAt (int nIndex );
Void RemoveAll ();
BOOL RemoveHead ();
BOOL RemoveTail ();
Int Find (T Element );
BOOL GetFirstNode (T & Element );
BOOL GetNextNode (T & Element );
Protected:
Struct Node {
Node * pNext;
T Data;
};
Int m_nSize; // queue size
Node * m_pHead; // queue header pointer
Node * m_pTail; // The tail pointer of the queue.
Node * m_pCurrentNode; // pointer of the current Node
};
// Because it is a template class, the implementation code of all functions must be put together in the header file.
Template <class T>
CQueue <T>: CQueue ()
{
M_nSize = 0;
M_pHead = NULL;
M_pTail = NULL;
M_pCurrentNode = NULL;
}
Template <class T>
CQueue <T> ::~ CQueue ()
{
RemoveAll ();
}
Template <class T>
Int CQueue <T >:: AddHead (T * pT)
{
ASSERT (pT );
Node * pNewNode = new Node;
PNewNode-> Data = * pT;
PNewNode-> pNext = m_pHead;
M_pHead = pNewNode;
M_nSize ++;
If (m_nSize = 1)
{
M_pTail = m_pHead;
}
Return m_nSize;
}
Template <class T>
Int CQueue <T & g ...... remaining full text>
I want to be a C/C ++ programmer, but I have never been able to perform interviews after the written examination. What knowledge points should I add? Network, database, operating system, data structure Algorithm
It is reasonable to say that you have thought about the interview and written examination of some large companies, so you should really take a look at the data structure and algorithms. As for the other aspects, you should select the focus based on your application direction. However, algorithms and data structures usually need to pass through.