Sharing of classic linked list questions in C language and sharing of questions in C Language

Source: Internet
Author: User

Sharing of classic linked list questions in C language and sharing of questions in C Language

There are many interview questions in C language. Here we have frequently-used classical interview questions. There are a variety of algorithms used, such as replacement methods and fast and slow pointers. Note: This article does not contain header files that reference the insertion and deletion of a single-chain table in the previous blog.

Interview Question 1: print a single-chain table from the end to the end.

/// // 1. print a single-chain table from the end to the end /////void SLitsPrintTailToHead (SListNode * pHead) // non-recursive algorithm (use two pointers to define one to the end of p1, another definition begins to loop p2 at the beginning. Whenever p2 loops to the end, the p2 value is output, so that p1 at the end points to p2. The loop starts again to reciprocating .) {SListNode * cur = NULL; while (cur! = PHead) {SListNode * tail = pHead; while (tail-> next! = Cur) {tail = tail-> next;} printf ("% d", tail-> data); cur = tail ;}} void SListPrintTailToHeadR (SListNode * pHead) // recursive algorithm {if (pHead = NULL) {return;} SListPrintTailToHeadR (pHead-> next); printf ("% d", pHead-> data );} //////////////////////////////////////// ///////
Interview Question 2: delete a non-End Node of a single-chain table without a header (you cannot repeat the table)
Void SListDelNonTailNode (SListNode * pos) // apply the forward replacement method, assign the value of the last value to the pos to replace the original value, and then point the pos to the next one of the pos. {SListNode * cur = NULL; cur = pos-> next; pos-> data = cur-> data; pos-> next = cur-> next; free (cur );}

Interview question 3: Insert a node in front of a node in a single-chain table without a header (the table cannot be traversed)

void SListInsertFrontNode(SListNode* pos, DataType x){SListNode *cur=BuySListNode(pos->data);cur->next=pos->next;pos->data=x;pos->next=cur;}

Interview question 4: single-chain table Implementation of Joseph circle)

//// 4. the single-chain table implements the Joseph ring (Joseph circle) //////// Joseph ring. For example, a group of people is circled in a circle and the number is reported from one person, for example, the person who reports to 3 will quit, and the next person will continue from 1 until only one person is left. SListNode * slistjoseph phcircle (SListNode * pHead, int k) // phead is a circular linked list {SListNode * cur = pHead; SListNode * nx = NULL; while (cur-> next! = Cur) {int Coun = k; while (-- Coun) {cur = cur-> next;} nx = cur-> next; // The replacement method does not need to delete the node cur-> data = nx-> data; cur-> next = nx-> next; free (nx );} return cur ;}

Interview question 5: reverse/reverse single-chain table

SListNode * SListReverse (SListNode * list) // reverse/reverse single-chain table (important to see more) {SListNode * cur = list; SListNode * newlist = NULL; SListNode * _ next = NULL; while (cur) {_ next = cur-> next; cur-> next = newlist; newlist = cur; cur = _ next;} return newlist ;}

Interview question 6: single-chain table sorting (Bubble Sorting & quick sorting)

Void SListBubbleSort (SListNode * list) // compare two types of Bubble Sorting (Bubble Sorting & quick sorting. {SListNode * tail = NULL; while (list! = Tail) {int change = 0; SListNode * cur = list; SListNode * _ next = list-> next; while (_ next! = Tail) {if (cur-> data> _ next-> data) {DataType tmp = cur-> data; cur-> data = _ next-> data; _ next-> data = tmp; change = 1;} _ next = _ next-> next; cur = cur-> next;} if (change = 0) {break;} tail = cur ;}}

Interview question 7: merge two sorted linked lists, which are still sorted

SListNode * SListMerge (SListNode * list1, SListNode * list2) // merge two ordered linked lists. The merged list is still ordered {SListNode * newlist = NULL; // SListNode * list = NULL; if (list2 = NULL) {return list1;} if (list1 = NULL) {return list2;} if (list1-> data <list2-> data) {newlist = list = list1; // one is used to locate the header, and the other is used to traverse. Only the pointer of the returned header can traverse all the linked lists list1 = list1-> next ;} else {newlist = list = list2; list2 = list2-> next;} while (list1 & list2) {if (list1-> data <list2-> data) {newlist-> next = list1; list1 = list1-> next;} else {newlist-> next = list2; list2 = list2-> next ;} newlist = newlist-> next;} if (list1) {newlist-> next = list1;} if (list2) {newlist-> next = list2;} return list ;}

Interview question 8: To find the intermediate node of a single-chain table, you must traverse the linked list only once.

SListNode * SListFindMidNode (SListNode * list) // 8. to find the intermediate node of a single-chain table, you must traverse only one linked list {SListNode * cur = NULL; // The speed pointer is applied. The speed of the fast pointer is twice that of the slow pointer, when the fast pointer goes to NULL, the slow pointer refers to the intermediate node. SListNode * fast = NULL; cur = fast = list; while (fast & fast-> next) {cur = cur-> next; fast = fast-> next;} return cur ;}

Interview Question 9: Find the last k nodes of a single-chain table, and you must traverse the linked list only once.

SListNode * SListFindTailKNode (SListNode * list, size_t k) // 9. to find the last k nodes of a single-chain table, you must traverse only one linked list {SListNode * cur, * fast; // the same speed pointer cur = fast = list; while (k --) {if (fast = NULL) {return 0;} fast = fast-> next;} while (fast) {fast = fast-> next; cur = cur-> next;} return cur ;}

Interview question 10: Delete the last K node of the linked list

Void SListFindPop (SListNode * list, size_t k) // 10. delete the last K node of the linked list {SListNode * cur = NULL; SListNode * tail = list; cur = SListFindTailKNode (list, k); while (list-> next! = Cur) {list = list-> next;} list-> next = cur-> next; free (cur );}

Interview question 11: Determine whether to bring a ring

SListNode * SListIsCycle (SListNode * list) // 11. determine whether or not to carry the ring {SListNode * cur, * fast; cur = fast = list; while (fast & fast-> next) {cur = cur-> next; fast = fast-> next; if (fast = cur) {return cur ;}} return NULL ;}

Question 12: length of the ring

Int slistcancelen (SListNode * meetNode) // 12. Evaluate the ring length {int n = 1; SListNode * cur = meetNode; while (cur-> next! = MeetNode) {++ n; cur = cur-> next;} return n ;}

Interview question 13: Calculate the entry point of the ring (the entry point of the ring is one starting from the linked list and the other starting from the encounter point, when the intersection point is the entry point)

SListNode * SListCrossEntreNode (SListNode * list, SListNode * meetNode) // 13. calculate the entry point of the ring (the entry point of the ring is one starting from the linked list and the other starting from the encounter point, when their intersection point is the entry point) {while (list! = MeetNode) {list = list-> next; meetNode = meetNode-> next;} return list ;}

Question 14: Determine whether two linked lists are intersecting. (Assume that the linked list does not contain loops)

Int SListIsCrossNode (SListNode * list1, SListNode * list2) // 14. Determine whether two linked lists are intersecting. (Assuming the linked list does not contain loops) {while (list1 & list1-> next) {list1 = list1-> next;} while (list2 & list2-> next) {list2 = list2-> next;} if (list2 = list1 & list1! = NULL) {return 1;} return 0 ;}

Question 14 (2): intersection of two linked lists.

SListNode * SListEnterNode (SListNode * list1, SListNode * list2) // intersection of two linked lists to obtain the intersection point. {SListNode * cur1 = list1; SListNode * cur2 = list2; int n1 = 0; int n2 = 0; while (cur1-> next) {n1 ++; cur1 = cur1-> next;} while (cur2-> next) {n2 ++; cur2 = cur2-> next;} cur1 = list1; cur2 = list2; if (n1-n2> = 0) {while (n1-n2! = 0) {cur1 = cur1-> next; n1 --;} while (cur1! = Cur2) {cur1 = cur1-> next; cur2 = cur2-> next ;}} else {while (n2-n1! = 0) {cur2 = cur2-> next; n2 --;} while (cur1! = Cur2) {cur1 = cur1-> next; cur2 = cur2-> next;} return cur1 ;}
Test code:
Void Test1 () // 1. print a single-chain table from the end to the header {SListNode * a = NULL; SListPushBack (& a, 1); SListPushBack (& a, 2); SListPushBack (& a, 3 ); SListPushBack (& a, 4); SListPushBack (& a, 5); SListPrint (a); SLitsPrintTailToHead (a); // non-recursive time complexity n square SListPrintTailToHeadR (); // recursion} void Test2 () // 2. delete a non-tail node (which cannot be a traversal table) of a single-header table {SListNode * a = NULL; SListNode * pos = NULL; SListPushBack (& a, 1); SListPushBack (&, 2); SListPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (&, 5); SListPrint (a); pos = SListFind (a, 3); SListDelNonTailNode (pos); SListPrint (a);} void Test3 () // 3. insert a node before a node in a single-node table without a header (the repeat table is not allowed) {SListNode * a = NULL; SListNode * pos = NULL; SListPushBack (& a, 1 ); SListPushBack (& a, 2); SListPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (& a, 5); SListPrint (); pos = SListFind (a, 3); SListInsertFrontNode (pos, 8); SListPrint (a);} void Test4 () // 4. single-chain table Implementation of Joseph circle {SListNode * list = NULL; SL IstNode * tail; SListPushBack (& list, 1); SListPushBack (& list, 2); SListPushBack (& list, 3); SListPushBack (& list, 4 ); SListPushBack (& list, 5); tail = SListFind (list, 5); tail-> next = list; printf ("Last survivor: % d \ n ", slistjoseph phcircle (list, 3)-> data);} void Test5 () // 5. // reverse/reverse a single-chain table {SListNode * list = NULL; SListNode * newList; SListPushBack (& list, 1); SListPushBack (& list, 2); SListPushBack (& list, 3); SListPushBack (& list, 4); SListPushBack (& list, 5); newList = SListReverse (list); SListPrint (newList);} void Test6 () // 6. single-chain table sorting (Bubble Sorting & quick sorting) {SListNode * list = NULL; SListPushBack (& list, 1); SListPushBack (& list, 22); SListPushBack (& list, 33); SListPushBack (& list, 40); SListPushBack (& list, 5); SListBubbleSort (list); SListPrint (list);} void Test7 () // 7. merge two ordered linked lists. The merged list is still ordered {SListNode * a = NULL; SListNode * B = NULL; SListNode * c = NULL; SListPushBack (&, 1); SListPushBack (& a, 2); SListPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (& a, 5); SListPushBack (&, 7); SListPushBack (& a, 9); SListPushBack (& B, 2); SListPushBack (& B, 2); SListPushBack (& B, 3); SListPushBack (& B, 4); SListPushBack (& B, 5); c = SListMerge (a, B); SListPrint (c);} void Test8 () // 8. to find the intermediate node of a single-chain table, you must traverse only one linked list {SListNode * a = NULL; SListNode * B = NULL; SListPushBack (& a, 1); SListPushBack (&, 2); SListPushBack (& a, 3); SListPus HBack (& a, 4); SListPushBack (& a, 5); B = SListFindMidNode (a); SListPrint (B);} void Test9 () // 9. to find the last k nodes of a single-chain table, you must traverse only one linked list {SListNode * a = NULL; SListNode * B = NULL; SListPushBack (& a, 1 ); SListPushBack (& a, 2); SListPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (& a, 5); B = SListFindTailKNode (, 2); SListPrint (B);} void Test10 () // 10. delete the last K node of the linked list {SListNode * a = NULL; SListNode * B = NULL; SListPushBack (& a, 1); SListPushBack (& a, 2); SLi StPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (& a, 5); SListFindPop (a, 3); SListPrint ();} void Test11_12_13 () // 11. judge whether to carry a ring 12. the length of the ring is 13. Evaluate the entry point of the ring {SListNode * a = NULL; SListNode * enter = NULL; SListNode * tail = NULL; SListNode * cur = NULL; SListPushBack (& a, 1 ); SListPushBack (& a, 2); SListPushBack (& a, 3); SListPushBack (& a, 4); SListPushBack (& a, 5); tail = SListFind (, 5); // construct the belt ring enter = SListFind (a, 3); tail-> next = enter; cur = SListIsCycle (a); printf ("Whether the belt ring: % d \ n ", cur-> data); printf (" Ring length: % d \ n ", SListCycleLen (cur); printf (" Ring entry point: % d \ n ", SListCrossEntreNode (a, cur)-> data);} void Test14 () // 14. Determines whether two linked lists are intersecting. If yes, the intersection is obtained. (Assuming the linked list does not contain loops) {SListNode * a, * B; SListNode * n1 = BuySListNode (1); SListNode * n2 = BuySListNode (2 ); SListNode * n3 = BuySListNode (3); SListNode * n4 = BuySListNode (4); a = BuySListNode (7); B = BuySListNode (8); n1-> next = n2; n2-> next = n3; n3-> next = n4; a-> next = B; B-> next = n3; printf ("whether to carry a ring: % d \ n ", SListIsCycle (a, n1); printf ("the entry point of the ring is % d \ n", SListEnterNode (a, n1)-> data );}
Main function:
int main(){//Test1();//Test2();//Test3();//Test4();//Test5();//Test6();//Test7();//Test8();//Test9();//Test10();//Test11_12_13();Test14();system("pause");return 0;}
The above is the solution to the typical interview questions. Flexible application, which will be updated later. <

Related Article

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.