[置顶] 从二级指针看华为和迅雷两道小题

原题:http://blog.csdn.net/v_july_v/article/details/11921021

1. 华为

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>



/*

 * HuaWei: A string compressed example

 * */

void stringZip(const char *pInputStr, char *pOutputStr) {

    int len = strlen(pInputStr);

    char *tmp = (char *)malloc(sizeof(len * sizeof(char)));

    int start = 0;

    int end = start + 1;

    int cnt = 0;

    int i = 0;



    while(pInputStr[start]) {

	if(pInputStr[start] == pInputStr[end]) {

	    cnt++;

	    end++;

	}

	else {

	    if(cnt == 0)  tmp[i++] = pInputStr[start];

	    else if(cnt > 0) {

		tmp[i++] = cnt + 1 + '0';

		tmp[i++] = pInputStr[start];

		cnt = 0;

	    }

	    start = end;

	    end = start + 1;

	}

    }

    tmp[i] = '\0';

    strncpy(pOutputStr, tmp, strlen(tmp));

    free(tmp);

}

void main() {  

    char str[1024];  

    char compress_str[1024];

    printf("Input a string:");

    gets(str);  

    stringZip(str, compress_str);

    printf("The compressed string is:");

    puts(compress_str);

}  


 

2. 迅雷

 

#include <stdio.h>

#include <stdlib.h>

/*

 * XunLei: difference of linked list

 * */

typedef struct node {

    int elem;

    struct node *next;

}node;



void create(node **head, int n) {

    int elem;

    int i;

    node *p;

    node *r;

    for(i = 0;i < n;i++) {

	if(scanf("%d", &elem) != 1) {

	    perror("input error!\n");

	    exit(1);

	}

	p = (node *)malloc(sizeof(node *));

	if(!p) {

	    perror("memory error!\n");

	    exit(2);

	}

	p->elem = elem;

	p->next = 0;

	if(!*head) *head = p;

	else {

	    r->next = p;

	}

	r = p;

    }

}

void show(node *head) {

    node *p = head;

    while(p) {

	printf("%d\n", p->elem);

	p = p->next;

    }

}

void difference(node **LA, node *LB) {

    node *p, *q, *pre;

    node *head;

    p = *LA;

    pre = *LA;

    head = p;

    while(p) {

	q = LB;

	while(q) {

	    if(p->elem == q->elem) break;

	    q = q->next;

	}

	if(q) {

	    if(pre == p) {

		p = p->next;

		pre->next = 0;

		free(pre);

		pre = p;

		head = p;

	    }

	    else {

		pre->next = p->next;

		p->next = 0;

		free(p);

		p = pre->next;

	    }

	}

	else {

	    pre = p;

	    p = p->next;

	}

    }

    *LA = head;

}

void main() {

    node *head1 = NULL, *head2 = NULL;

    create(&head1, 5);

    printf("----------\n");

    create(&head2, 3);

    difference(&head1, head2);

    printf("----------\n");

    show(head1);

}


[置顶] 从二级指针看华为和迅雷两道小题

 

 

你可能感兴趣的:(华为)