Linked list, constructors, macros, pointers

Source: Internet
Author: User

First, how to judge a single linked list is a ring?   (Note that you cannot use the flag bit, up to two extra pointers) to struct node {char val; node* next;}    BOOL Check (const node* head) {}//return false: No ring; True: There is an O (n) method that is (make two pointers, one step at a time, one increment of each step, and if there is a ring, the two must overlap, and vice versa):            BOOL Check (const node* head) {if (head==null) return false;         Node *low=head, *fast=head->next;               while (Fast!=null && fast->next!=null) {low=low->next;               fast=fast->next->next;        if (low==fast) return true;   } return false;    } Second, delete the most intermediate element of a single list, requiring time as short as possible (cannot use two cycles) struct link{int data; struct link *next;};    void Delmiddle (link *head) {if (head = = NULL) return;            else if (Head->next = = NULL) {delete head;    Return            } else {link *low = head;            Link *fast = head->next; while (fast! = NULL && Fast->next! = null) {fast =fast->next->next;                       if (fast = = NULL) break;            Low = low->next;            } Link *temp = low->next;            Low->next = low->next->next;      Delete temp;       }}int Main () {struct link *head,*l;       struct link *s;       Head = (link*) malloc (sizeof);       head->data=0;       Head->next = NULL;       L = head;            for (int i=1; i<9; i++) {s = (link*) malloc (sizeof);            S->data = i;            S->next = NULL;            l->next= s;       L = l->next;       } print (head);       Delmiddle (head);       Print (head); return 0;} Three, enter N, for a n*n matrix, specify the matrix along the 45-degree line increment (via)/** * Get a two-dimensional array of the following style * zigzag (the sequence of pixel data in JPEG encoding) * * 0, 1, 5, 6,14,15,27,28,* 2, 4, 7,13,16,26 , 29,42,* 3, 8,12,17,25,30,41,43,* 9,11,18,24,31,40,44,53,* 10,19,23,32,39,45,52,54,* 20,22,33,38,46,51,55,60,* 2 1,34,37,47,50,56,59,61,* 35,36,48,49,57,58,62,63 */void Zigzag (int n) {int **a = (int**) malloc (n*sizeof (int *)); Allocate space if (NULL = = a) return; int i;                for (i = 0; i < n; i++) {if ((a[i] = (int*) malloc (n * sizeof (int.)) = = NULL) {while (--i>=0)            Free (a[i]);            Free (a);        Return }} bool flag = FALSE; This flag is used to determine whether to generate an int count = 0 from a 45-degree angle or a 225-degree angle;    for (i=0; i<n; i++)//The upper half of the generated data {if (flag) {for (int r = 0; r<=i; r++) {A[r][i-r] = count;   count++;  } flag = false;    } else {for (int r = i; r>=0; r--) {A[r][i-r] = count;   count++;  } flag = true;  }} for (i=n-1; i>=0; i--)//Generate data for the lower half {//cout<<i<<endl;       if (flag) {for (int r = 0; r<=i-1; r++) {int r1 = N-i+r;  Represents the current line int c1 = 2*N-I-1-R1;    Represents the current column a[r1][c1] = count;   count++;  } flag = false;    } else {for (int r = i-1; r>=0; r--) {cout<< "ddd" <<endl;    int r1 = N-i+r; int C1 = 2*N-I-1-R1;    cout<<r1<< "," <<c1<<endl;    A[R1][C1] = count;   count++;  } flag = true;  }} for (int r = 0; r<n; r++) {for (int c=0; c<n; C + +) cout<<a[r][c]<< ","; cout<<endl; }}int Main () {int n; cin>>n; zigzag (n); return 0;}  Another person on the web wrote a more ingenious algorithm:/*** get a two-dimensional array of the following styles * zigzag (the sequence of pixel data in JPEG encoding) * * 0, 1, 5, 6,14,15,27,28,* 2, 4, 7,13,16,26,29,42,* 3, 8,12,17,25,30,41,43,* 9,11,18,24,31,40,44,53,* 10,19,23,32,39,45,52,54,* 20,22,33,38,46,51,55,60,* 21,34,37,47,50    , 56,59,61,* 35,36,48,49,57,58,62,63*/#include <stdio.h>int main () {int N;    int S, I, J;    int Squa;    scanf ("%d", &n);    /* Allocate space */int **a = malloc (N * sizeof (int *));    if (a = = NULL) return 0;                for (i = 0; i < N; i++) {if (a[i] = malloc (N * sizeof (int.)) = = NULL) {while (--i>=0)            Free (a[i]);            Free (a);        return 0;        }}/* Array assignment */Squa = n*n; for (i = 0; i < N;            i++) for (j = 0; J < N; J + +) {s = i + j;            if (S < N) A[i][j] = s* (s+1)/2 + (((i+j)%2 = = 0)? i:j);                else {s = (n-1-i) + (N-1-J);            A[I][J] = squa-s* (s+1)/2-(N-(((i+j)%2 = = 0)? i:j)); }/* Print output */for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) printf ("%-6d", a[i        ][J]);    printf ("\ n"); } return 0;}    Four, printing integers 1 to 1000, cannot use Process Control statements (For,while,goto, etc.) also cannot use recursive 1.typedef struct _test{static int A;        _test () {printf ("%d\n", _test::a);    a++;  }}test;  int test::a = 1;    int main () {Test tt[1000];  return 0; } 2. #include <stdio.h> #define B p,p,p,p,p,p,p,p,p,p #define P l,l,l,l,l,l,l,l,l,l #define L i,i,i,i     , I,i,i,i,i,i,n #define I printf ("%3d", i++) #define N printf ("\ n") int main () {int I = 1; B  } or # define A (x) x;x;x;x;x;x;x;x;x;x;int main () {int n = 1;   A (A (A (printf ("%d", n++))); return 0;}         V. struct S {int i; int * p; };         void Main () {s S;         int * p = &s.i;         P[0] = 4;         P[1] = 3;         S.P = p;         S.P[1] = 1; S.p[0] = 2; Ask the program which line will die.          (Microsoft) Solution: s S;        int * p = &s.i;                    The address of the S.I is stored in p p[0] = 4;                    Modified S.I p[1] = 3;                    Modified the S.P S.P = p;               S.P point s.i s.p[1] = 1;               Modify S.P itself s.p[0] = 2;   S.P point is 0x00000001, try to write here, error s.p[0] = 2; Error because S.P is S.I address, s.p[1] is S.P, when S.p[1]=1, S.P at this time storage is 1, instead of address s.i, so in s.p[0] = 2 o'clock error. At this time the equivalent of s.p=ox00000001; address ox0000001 = 2; Of course it went wrong. If the statement s.p[0] =2 precedes s.p[1]=1, the program will not go wrong. At this time the statement is equivalent to S.i=2;s.p=1;   Six, the title description: 1.     int swap (int *x,int *y) {if (x==null¦¦y==null) return-1;     *x + = *y; *y = *x-*Y       *x-= *y; return 1; Please make a mistake, overflow has been considered, not error 2.     void foo (int *x, int *y) {*x + = *y; *x + = *y;   } void Fun (int *x, int *y) {*x + = 2 * (*y); } ask whether the two functions are equivalent and can be exchanged for answers: The function of the first question is exchange. But if x is considered, y is pointing to the same variable, and the result is that the value of this variable is 0. The two functions of the second question are different, also consider that x, Y is pointing to the same variable. The result of the first function is 4 times times that of the variable. But the result of the second function is 3 times times the variable.

Linked list, constructors, macros, pointers

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.