[C ++ Interview Questions] cyclic linked list, queue, stack, and heap (see a interview question on csdn-solution)

Source: Internet
Author: User
ArticleDirectory
    • 1. N people are known (with numbers 1, 2, 3 ,..., N indicates respectively) the person sitting around a Round Table starts reporting data from the person numbered K, and the person counting m is listed. His next person starts reporting data from K, the person counting to m goes out of the column and repeats until all round-table members are out of the column. Use C ++ to compile and implement the SDK.
    • 2. Implement queuing/queuing operations by programming.
    • 3. Use two stacks to implement a queue function. Use C ++.
    • 4. Tell us about the differences between heap and stack.
    • 5. Today, I saw an interview question on csdn.
1. N people are known (with numbers 1, 2, 3 ,..., N indicates respectively) the person sitting around a Round Table starts reporting data from the person numbered K, and the person counting m is listed. His next person starts reporting data from K, the person counting to m goes out of the column and repeats until all round-table members are out of the column. Use C ++ to compile and implement the SDK.

Resolution:This is the actual scenario of the Joseph Ring problem. We need to input n, m, K three positive integers to find the column sequence. This problem uses the data structure of a typical circular linked list, that is, to point the tail element pointer of a linked list to the first element of the team:

P-> link = head;

The core steps for solving the problem are as follows:

(1) create a circular linked list with N chain nodes and headless nodes.

(2) determine the location of the first reporter.

(3) Delete the chain node from the linked list until the linked list is empty.

Answer:

# Include <iostream> using namespace STD; typedef struct lnode {int data; struct lnode * link;} lnode, * linklist; // n indicates the total number, K is the first person to start reporting data, and m is the number void Joseph us (int n, int K, int m) shouted by the column reporter {// P is the current node, R is the secondary node and points to the front node of P. list is the first node linklist P, R, list, curr; // resume loop linked list p = (linklist) malloc (sizeof (lnode); P-> DATA = 1; p-> link = P; curr = P; For (INT I = 2; I <= N; I ++) {linklist t = (linklist) malloc (sizeof (lnode); t-> DATA = I; t-> link = curr-> link; curr-> link = T; curr = T;} // The Person Who moves the current pointer to the First Report, r = curr; while (k --) r = P, P = p-> link; while (n --) {for (int s = m-1; s --; r = P, P = p-> link ); r-> link = p-> link; printf ("% d->", p-> data); free (p); P = r-> link ;}}

 

2. Implement queuing/queuing operations by programming.

Answer:

# Include <iostream> using namespace STD; typedef struct student {int data; struct student * Next;} node; typedef struct linkqueue {node * First, * rear;} queue; // queue * insert (queue * HQ, int X) {node * s; S = (node *) malloc (sizeof (node )); s-> DATA = x; s-> next = NULL; If (HQ-> rear = NULL) {HQ-> first = s; HQ-> rear = s ;} else {HQ-> rear-> next = s; HQ-> rear = s;} return (HQ);} // queue * remove (queue * HQ) {node * P; If (HQ-> first = NULL) printf ("\ n Yichu"); else {P = HQ-> first; if (HQ-> first = HQ-> rear) {HQ-> first = NULL; HQ-> rear = NULL ;} else {HQ-> first = HQ-> first-> next; free (p) ;}return (HQ );}}

 

3. Use two stacks to implement a queue function. Use C ++.

Resolution:The idea is as follows:

Assume that both stacks A and B are empty.

Stack a provides the inbound queue function, while stack B provides the outbound queue function.

Inbound queue: Inbound stack.

Outbound queue:

(1) If Stack B is not empty, the data of stack B is directly displayed.

(2) If Stack B is empty, the data of stack a is popped up in sequence, put in stack B, and then the data of stack B is popped up.

Answer:

# Include <iostream> # include <stack> using namespace STD; Template <class T> struct queue {void push (T & T) {s1.push (t);} t Front () {If (s2.empty () {If (s1.size () = 0) Throw; while (! S1.empty () {s2.push (s1.top (); s1.pop () ;}} return s2.top () ;}void POP () {If (s2.empty () {While (! S1.empty () {s2.push (s1.top (); s1.pop () ;}} if (! S2.empty () s2.pop () ;}stack <t> S1; stack <t> S2 ;}

 

4. Tell us about the differences between heap and stack.

Resolution:For C/C ++ programmingProgramThe knowledge of the memory is more accurate. Memory that often needs to be operated can be divided into the following categories:

(1) STACK: the stack is automatically allocated and released by the compiler, and stores function parameter values and local variable values. The operation method is similar to the stack in the data structure.

(2) Heap: Generally, it is allocated and released by programmers. If the programmer does not release the heap, it may be recycled by the operating system when the program ends. Note that it is different from the heap in the data structure. The allocation method is similar to the linked list.

(3) Global (static): the storage of global variables and static variables is put together, and the initialized global variables and static variables are in one area, uninitialized global variables and static variables are in another adjacent area. The program is released by the system.

(4) text constant area: the constant string is placed here, and is released by the system after the program ends.

(5) ProceduresCodeZONE: stores the binary code of the function body.

 

Answer:

(1) The stack space is automatically allocated/released by the operating system, and the heap space is manually allocated/released.

(2) The stack space is limited, and heap is a large free storage zone.

(3) the malloc function in C allocates memory space on the heap, and the c ++ corresponds to the new operator.

(4) During the compilation period, the program allocates both variable and Function Memory on the stack, and transmits parameters during function calling during the program running.

 

 

5. Today, I saw an interview question on csdn.

Signature of the given method:

 
Movesubarraytotheend (INT [] array, int numberofelements)

 

Input an array such as {1, 2, 3, 4, 5, 6, 7}
Move a subset of the head before the array to the end of the array.
For example: Input numberofelements = 3, then {1, 2, 3, 4, 5, 6, 7 }=>{ 4, 5, 6, 7, 1, 2, 3}
Input numberofelements = 5, then {1, 2, 3, 4, 5, 7 }=>{ 6, 7, 1, 2, 3, 4, 5}
Please do not use the class library functions provided by FCL ..

 

Here, I would like to ask you how to writeAlgorithm..

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.