Don't take the interview questions too seriously.

Source: Internet
Author: User

 

  malloc()

 

# Include <ctype. h>
# Include <stdlib. h>

Struct bignum
{
Char dig;
Struct bignum * next;
}
* LIST;

Void my_malloc (LIST *);
Void output (LIST );
LIST add (LIST, LIST );
Void my_free (LIST );
Void move (LIST *);
Int get_dig (LIST );
Void put (int, LIST *);

{
LIST p_num1, p_num2;
LIST p_sum;

Puts ("enter an integer :");
P_num1 = get_num ();
Puts ("enter an integer :");
P_num2 = get_num ();


Output (p_sum );
Putchar ('\ n ');

My_free (p_sum );
My_free (p_num1 );
My_free (p_num2 );

Return 0;
}

{
If (* p = malloc (sizeof (** p) = NULL)
Exit (1 );
}

LIST get_num (void)
{
LIST p = NULL;
Int c;
While (c = getchar () & isdigit (c ))
{
LIST tmp;
My_malloc (& tmp );
Tmp-> dig = c-'0 ';
Tmp-> next = p;
P = tmp;
}
Return p;
}

{
If (p-> next! = NULL)
Free (p-> next );

}

{
If (p = NULL)
Return 0;
Return p-> dig;
}

{
If (* pp = NULL)
Return;
* Pp = (* pp)-> next;
}

{
My_malloc (pp );
(* Pp)-> dig = n;
(* Pp)-> next = NULL;
}

{
LIST p_res = NULL;
LIST * pp_res = & p_res;
Char low, high = 0;
While (p1! = NULL | p2! = NULL)
{
Low = high; // high is the carry of the previous time
Low + = get_dig (p1) + get_dig (p2 );
High = low/10; // write down carry
Low % = 10;

Put (low, pp_res); // Add and

Pp_res = & (* pp_res)-> next;

Move (& p1), move (& p2 );
}

If (high! = 0) // process the carry of the highest bit
Put (high, pp_res );

}


Void output (LIST p)
{
If (p-> next! = NULL)
Output (p-> next );

Putchar (p-> dig + '0 ');

}

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.