Http://topic.csdn.net/u/20090603/13/05ea579e-ba1d-416f-9305-f0bf9aa56e53.html
Http://kinghuimail.blog.163.com/blog/static/95820408201011139931364/
Http://fishlife.iteye.com/blog/641330
1. Write a program in C ++. How can I determine whether an operating system is 16-bit or 32-bit? The sizeof () function cannot be used.
A1:
In a 16-bit system,
Int I = 65536;
Cout <I; // output 0;
Int I = 65535;
Cout <I; // output-1;
In a 32-bit system,
Int I = 65536;
Cout <I; // output 65536;
Int I = 65535;
Cout <I; // output 65535;
A2:
Int A = ~ 0;
If (A> 65536)
{
Cout <"32 bit" <Endl;
}
Else
{
Cout <"16 bit" <Endl;
}
2. Switch the values of two parameters without using third-party parameters.
# Include <stdio. h>
Void main ()
{
Int I = 60;
Int J = 50;
I = I + J;
J = I-j;
I = I-j;
Printf ("I = % d \ n", I );
Printf ("J = % d \ n", J );
}
Method 2:
I ^ = J;
J ^ = I;
I ^ = J;
Method 3:
// Use addition and subtraction without Overflow
A = a + B-(B =)
3. struct
{
Char T: 4;
Char K: 4;
Unsigned short I: 8;
Unsigned long m;
}
Sizeof (A) =? (Boundary alignment is not considered)
All the answers to this question are: 6, 7, and 8 on VC ++ 6.0;
4.
Union {
Int a_int1;
Double a_double;
Int a_int2;
};
Typedef struct
{
A A1;
Char y;
} B;
Class C
{
Double c_double;
B B1;
A A2;
};
Output The cout <sizeof (c) <Endl; result?
Note the boundary alignment of the struct.
A:
In the vc6 environment, the result is 32.
5
. Program correction
Class MML
{
PRIVATE:
Static unsigned int X;
Public:
MML () {x ++ ;}
MML (static unsigned Int &) {x ++ ;}
~ MML {X --;}
Pulic:
Virtual Mon () {}= 0;
Static unsigned int MMC () {return X ;}
......
};
Class NNL: Public MML
{
PRIVATE:
Static unsigned int y;
Public:
NNL () {x ++ ;}
NNL (static unsigned Int &) {x ++ ;}
~ NNL {X --;}
Public:
Virtual Mon (){};
Static unsigned int NNC () {return y ;}
......
};
Code snippet:
MML * PP = new NNL;
..........
Delete pp;
A:
The destructor of the base class should be virtual functions.
Virtual ~ MML {X --;}
6
Difference between strncpy () and memcpy:
Strncpy () string, memcpy () Copy memory content.
Strncpy
Prototype: extern char * strncpy (char * DEST, char * SRC, int N );
Usage: # include <string. h>
Function: copy the first n Bytes of the string ending with null in SRC to the array indicated by DeST.
Note:
If the first n Bytes of SRC do not contain null characters, the result will not end with null characters.
If the SRC length is less than n Bytes, fill the Dest with null until the n Bytes are completely copied.
The memory areas specified by Src and DEST cannot overlap and DEST must have enough space to hold SRC strings.
Returns the pointer to DeST.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # include <string. h>
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST.
7
Recursive and non-Recursive Algorithms for reverse order of a single-chain table (conversion)
# Include <iostream>
Using namespace STD;
Struct Node
{
Int data;
Node * next;
};
Typedef struct node;
Node * reverselist (node * head)
{
If (! Head |! Head-> next)
{
Return head;
}
Node * P1 = head;
Node * P2 = p1-> next;
Head-> next = NULL;
While (P2)
{
P1 = P2;
P2 = P2-> next;
P1-> next = head;
Head = p1;
}
Return head;
}
Node * recreverselist (node * head) // Recursive Method
{
If (! Head |! Head-> next)
{
Return head;
}
Node * newhead = recreverselist (Head-> next );
Head-> next = head;
Head-> next = NULL;
Return newhead;
}
Void main ()
{
Node A, B, C;
A. Data = 1, A. Next = & B;
B. Data = 2, B. Next = & C;
C. Data = 3, C. Next = NULL;
Node * TMP = &;
While (TMP)
{
Cout <TMP-> data <"";
TMP = TMP-> next;
}
Cout <Endl;
TMP = recreverselist (& );
While (TMP)
{
Cout <TMP-> data <"";
TMP = TMP-> next;
}
Cout <Endl;
}
1. Linked List
1. The M-last element in the linked list
For a one-way linked list, design a time-saving and space-saving algorithm to find the M-last element in the list. Implement this algorithm.
The M-last element is specified as follows: When M is 0, the last element (tail element) of the linked list is returned.
Start a "post Pointer" after the M step"
2. Empty and cyclic linked lists
Given a linked list, it may be a non-circular linked list ending with "null" or a circular linked list ending with a circular structure. Write a function to determine whether the linked list is a circular linked list or a non-circular linked list. This function cannot modify the linked list itself.
With two pointers, one is fast and one is slow. If it is a circular linked list, the fast pointer will sooner or later exceed the slow pointer. Time Complexity: O (n), the maximum is 3n.
Int determinetermination (node * head)
{
Node * fast, * slow;
Fast = slow = head;
While (1 ){
If (! Fast |! Fast-> next ){
Return 0;
}
Else if (fast = slow | fast-> next = slow ){
Return 1;
}
Else {
Slow = slow-> next;
Fast = fast-> next;
}
}
}
Ii. Others
1. byte storage in ascending order and in descending order
Compile a function to determine whether the byte storage order of a computer is ascending (little-Endian) or descending (big-Endian ).
The "Byte storage order" refers to the order in which each byte of Multi-byte data is stored on a computer. For example, the order of the four bytes used to represent the integer includes two types: one is to store data in the order from the byte (LSB) to the maximum byte (MSB; the other is the order from MSB to LSB for storage. The "High Byte" here refers to the high byte in the word. If the byte represents the low-value part of a word, we can say that it is the LSB of the word. For example, the LSB in the value "5a6c" is "6C ". Conversely, MSB in "5a6c" is
"5A ".
In a computer that uses the byte descending Storage Scheme, MSB will be stored in the second-bit address; in a computer that uses the byte ascending storage scheme, the LSB is stored in the second-bit address. For example, a computer that uses the byte descending storage scheme saves "A4" in the hexadecimal value "a45c" in the first byte, store "5c" in the second byte, while a computer that adopts the byte ascending storage solution saves "5c" in the first byte, save "A4" in the second byte.
In this topic, you can use a character pointer to obtain the content in a byte.
/* Return 1 if the machine is little-Endian, 0 if
* Machine is big-Endian
*/
Int endianness (void)
{
Int testnum;
Char * PTR;
Testnum = 1;
PTR = (char *) & testnum;
Return (* PTR );
}
There is a more subtle solution, using the "union" type: This type is similar to "struct,
All its data members are stored from the same location in the memory.
This allows you to use the same data as different variable types.
/* Return 1 if the machine is little-Endian, 0 if
* Machine is big-Endian
*/
Int endianness (void)
{
Union {
Int theinteger;
Char singlebyte;
} Endiantest;
Endiantest. theinteger = 1;
Return endiantest. singlebyte;
}