Showing posts with label Linux programming. Show all posts
Showing posts with label Linux programming. Show all posts

Insert Node into the end of list

struct node{
    int data;
    struct node *next;
};

struct node *getLastNode(struct node *head);

void insertNode(struct node **head, int data)
{

    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->next = NULL;
    newNode->data = data;
    if(*head == NULL)
    {
        *head = newNode;
    }
    else
    {
        getLastNode(*head)->next = newNode;
    }
}

struct node *getLastNode(struct node *head)
{
    struct node *lastNode = head;
    if(head == NULL)
        return NULL;
    else{
        while(lastNode->next != NULL)
            lastNode = lastNode->next;
    }
    return lastNode;
}

Remove Node with Key

struct node{
    int data;
    struct node *next;
};

void removeNodeWithData(struct node **head, int removeData)
{
    struct node *current = *head;
    struct node *prev=*head;
    while(current->data == removeData) //head
    {
        *head = (*head)->next;
        free(current);
        current = *head;
    }

    prev = *head;
    current = (*head)->next;
    while(current != NULL)
    {
        if(current->data == removeData)
        {
            prev->next = current->next;
            free(current);
        }
        else{
            prev = current;

        }
        current = prev->next;
    }

}

[Gem 5] Recover ubuntu root password in Virtualbox (reset password trong ubuntu)

some how you get lost your password. Then, I can not remember what your password is. you can reset your password.

Step1: restart your ubuntu and hold shift key until the boot screen appears.

Step2: choose the generic second boot option

Step3: the new screen appears, choose "Drop into root shell prompt"

Step4: run below command:
           >> mount -rw -o remount /

Step5: run below command to create new root password
          >> sudo passwd root


If you see some error like:
passwd: Authentication token manipulation error
passwd: password unchanged

This means that you did not run the command at step 4. If so, try to run the command at step 4 and then command at step 5.

--
Lấy lại password trong ubuntu dùng virtualbox. Theo các bước ở trên

[gem5] How to install Cuda on ubuntu 14.04

Step 1: get the suitable cuda repo from https://developer.nvidia.com/cuda-downloads
    in this case, cuda for ubuntu 14.04 is fetched using below command:
      >> wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.0-28_amd64.deb

Step 2: get cuda and install:
    >> sudo dpkg -cuda-repo-ubuntu1404_7.0-28_amd64.deb

Step 3: install cuda tookit
    >> sudo apt-get install cuda

When you are done with installation, you should set the Cuda environment variable. It is better to place below commands into your .bashrc file in your home director

export CUDA_HOME=/usr/local/cuda-7.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64

PATH=${CUDA_HOME}/bin:${PATH}
export PATH 


---
Cách cài đặt Cuda trên ubuntu 14.04. Theo từng bước như ở trên.

[Python] Inheritance

Python is an object-oriented programming language, so it also has inheritance, overloading, overriding, etc. Below is one of the simple examples of how inheritance is implemented in Python.

By looking at below example, it is easy to learn how a class inherits from other classes and how a class is declared, defined and used in Python.

--------------------------------
Python là một ngôn ngữ lập trình hướng đối tượng! bên dưới là một ví dụ đơn giản về tính thừa kế trong Python.

[Python] Small Python program applying Skeleton

This is an example of how to apply skeleton in the previous post.
This program is just the simplest calculator in the world. The purpose is just to prove how efficient to apply the skeleton into your project. It makes the project have a good look and well-organized.

--------------------
Chương trình máy tính python đơn giản. Cách áp dụng coding style ở bài trước
--------------------

[Python] Skeleton

A good programming habit is to have a skeleton for every new project. Below is a simple skeleton Python project which can be served as an entry point for a initial start of writing your own skeleton:

----------
Bộ khung mẫu của một chương trình trong python! Nó sẽ giúp ích trong việc tổ chức code
----------

import sys
def main():
    func1()
    func2()
    pass

def func1():
    # doing your work
    pass

def func2():
    # doing your work
    pass
    
if __name__ == '__main__':
    sys.exit(main())

Boring C code ever! - Easy to get error

Khi sử dụng một #define trong C, chúng ta nên chú ý dùng dấu ngoặc trong các biểu thức được define.
Ngoài ra, nếu trước đó ta đã khai báo một biến, sau đó lại define một tên trùng với tên biến đó. Thì từ lúc define đó trở đi, tên được define sẽ được sử dụng cho tới khi ta undef nó ( như ví dụ với M bên dưới)

#include

#define in printf
#define N 100
#define length1 3 + 5
#define length2 (3+5)
void main()
{
    int M = 200;
    int x = 2*length1;
    int y = 2*length2;
    in("\n N= %d M=%d",N,M);
#define M 300
    in("\nM=%d",M);
#undef M
    in("\nM=%d",M);
    in("\n%d",x);
    in("\n%d",y);
}

[Thuật toán cơ bản] Xác định số nguyên tố

Xác định số nguyên tố: ngôn ngữ C

#include
#include

void main()
{
int i,n,ngt=1;
printf("Nhap so n = "); scanf("%d",n);
for(i=0;i
(ng_to=1)?printf("%d la so nguyen to",i):printf("%d khong la hop so",i);
getch();
}

Lập trình C: Tìm số nguyên tố
Tìm số nguyên tố dùng C
Thuật toán tìm số nguyên tố dùng C
Thuật toán xác định số nguyên tố dùng C

[Thuật toán cơ bản] A simple algorithm for getting a square root of a number

Thuật toán tính căn bậc 2 dùng ngôn ngữ C / Lập trình C
 
#include
#include
#include

void main()
{
    double a,xn,c;
    printf("\nNhap: ");scanf("%lf",&a);
    xn = a;

    do
    {
        c = xn;
        xn = (xn*xn + a)/(2*xn);
    }while(fabs((xn-c)/c) >= 0.00001);
    printf("\ncan bac 2 sai so: %lf",xn);
    printf("\nchinh sac: %lf", sqrt(a));
}



Thuật toán tính căn bậc hai dùng ngôn ngữ C / Lập trình C
Lập trình C: Tính căn bậc hai
Ngôn Ngữ C: Tính căn bậc 2

Boring C code ever! - solve quadratic equations

( Giải phương trình bậc hai dùng ngôn ngữ C )
Doing all of the calculation in only 1 code line for solving quadratic equation
How boring it is, isn't it :D

#include
#include
#include

void main()
{
    double a,b,c;
    printf("=======================================\n=============== GPTB 2 ================\n=======================================\nNhap du lieu a,b,c cua phuong ax^2 + bx + c = 0\n");
    printf("Nhap a = "); scanf("Nhap a = %lf",&a);
    printf("\n");
    printf("Nhap b = "); scanf("%lf",&b);
    printf("\n");
    printf("Nhap c = "); scanf("%lf",&c);
    printf("\n");

    (a == 0)?((c==0)?((b == 0)?printf("PT vo so nghiem\n"):printf("PT vo nghiem")):((b == 0)?printf("PT vo nghiem\n"): printf("PT co 1 nghiem: %lf",-c/b))):((c != 0) ? ((b == 0) ?(( c< 0) ?printf("PT co nghiem: x = +/-%lf",sqrt(-c/a)):printf("PT vo nghiem\n")):(((b*b - 4*a*c) > 0) ?printf("PT co 2 nghiem: \nx1 = %lf \nx2 = %lf\n",(-b + sqrt(b*b - 4*a*c))/(2*a),(-b-sqrt(b*b - 4*a*c))/(2*a)):printf("PT vo nghiem\n"))):((b == 0)?printf("PT co nghiem x=0"):printf("PT co 2 nghiem x1 = 0 and x2 = %lf",-b/a)));
}


Click on the image to enlarge your view :)

C programing trick!!

What is wrong ??
#include
#include

void main()
{
    printf("%10.0f",-3456);
    getch();
}


There might be some tricks on C programming section in an interview test. Have you ever experienced such kind of trick. If not, above is one of a typical examples which might be on your interview test.

Try to figure out what is going on with above short code !!!

At the first glance, you may ask yourself what is wrong with it. It is just a simple code, nothing wrong with it. But, when you actually run this kind of code. the printing result is not as your expected. It's gonna be 0.

What is the problem here? ---> Try to figure it out by yourself ;)

How to fix that code to run in a proper way ?
--->  it is so easy, isn't it. Just need to put 3456.0 instead of 3456

[Programming] Các kiểu dữ liệu cơ bản

Integer types having exactly the specified width
typedef signed char int8_t
typedef unsigned char uint8_t
typedef signed int int16_t
typedef unsigned int uint16_t
typedef signed long int int32_t
typedef unsigned long int uint32_t
typedef signed long long int int64_t
typedef unsigned long long int uint64_t

Integer types capable of holding object pointers

These allow you to declare variables of the same size as a pointer.
typedef int16_t intptr_t
typedef uint16_t uintptr_t

Minimum-width integer types

Integer types having at least the specified width
typedef int8_t int_least8_t
typedef uint8_t uint_least8_t
typedef int16_t int_least16_t
typedef uint16_t uint_least16_t
typedef int32_t int_least32_t
typedef uint32_t uint_least32_t
typedef int64_t int_least64_t
typedef uint64_t uint_least64_t

Fastest minimum-width integer types

Integer types being usually fastest having at least the specified width
typedef int8_t int_fast8_t
typedef uint8_t uint_fast8_t
typedef int16_t int_fast16_t
typedef uint16_t uint_fast16_t
typedef int32_t int_fast32_t
typedef uint32_t uint_fast32_t
typedef int64_t int_fast64_t
typedef uint64_t uint_fast64_t

Greatest-width integer types

Types designating integer data capable of representing any value of any integer type in the corresponding signed or unsigned category
typedef int64_t intmax_t
typedef uint64_t uintmax_t

Limits of specified-width integer types

C++ implementations should define these macros only when __STDC_LIMIT_MACROS is defined before <stdint.h> is included
#define INT8_MAX   0x7f
#define INT8_MIN   (-INT8_MAX - 1)
#define UINT8_MAX   (__CONCAT(INT8_MAX, U) * 2U + 1U)
#define INT16_MAX   0x7fff
#define INT16_MIN   (-INT16_MAX - 1)
#define UINT16_MAX   (__CONCAT(INT16_MAX, U) * 2U + 1U)
#define INT32_MAX   0x7fffffffL
#define INT32_MIN   (-INT32_MAX - 1L)
#define UINT32_MAX   (__CONCAT(INT32_MAX, U) * 2UL + 1UL)
#define INT64_MAX   0x7fffffffffffffffLL
#define INT64_MIN   (-INT64_MAX - 1LL)
#define UINT64_MAX   (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)

Limits of minimum-width integer types

#define INT_LEAST8_MAX   INT8_MAX
#define INT_LEAST8_MIN   INT8_MIN
#define UINT_LEAST8_MAX   UINT8_MAX
#define INT_LEAST16_MAX   INT16_MAX
#define INT_LEAST16_MIN   INT16_MIN
#define UINT_LEAST16_MAX   UINT16_MAX
#define INT_LEAST32_MAX   INT32_MAX
#define INT_LEAST32_MIN   INT32_MIN
#define UINT_LEAST32_MAX   UINT32_MAX
#define INT_LEAST64_MAX   INT64_MAX
#define INT_LEAST64_MIN   INT64_MIN
#define UINT_LEAST64_MAX   UINT64_MAX

Limits of fastest minimum-width integer types

#define INT_FAST8_MAX   INT8_MAX
#define INT_FAST8_MIN   INT8_MIN
#define UINT_FAST8_MAX   UINT8_MAX
#define INT_FAST16_MAX   INT16_MAX
#define INT_FAST16_MIN   INT16_MIN
#define UINT_FAST16_MAX   UINT16_MAX
#define INT_FAST32_MAX   INT32_MAX
#define INT_FAST32_MIN   INT32_MIN
#define UINT_FAST32_MAX   UINT32_MAX
#define INT_FAST64_MAX   INT64_MAX
#define INT_FAST64_MIN   INT64_MIN
#define UINT_FAST64_MAX   UINT64_MAX

Limits of integer types capable of holding object pointers

#define INTPTR_MAX   INT16_MAX
#define INTPTR_MIN   INT16_MIN
#define UINTPTR_MAX   UINT16_MAX

Limits of greatest-width integer types

#define INTMAX_MAX   INT64_MAX
#define INTMAX_MIN   INT64_MIN
#define UINTMAX_MAX   UINT64_MAX

Limits of other integer types

C++ implementations should define these macros only when __STDC_LIMIT_MACROS is defined before <stdint.h> is included
#define PTRDIFF_MAX   INT16_MAX
#define PTRDIFF_MIN   INT16_MIN
#define SIG_ATOMIC_MAX   INT8_MAX
#define SIG_ATOMIC_MIN   INT8_MIN
#define SIZE_MAX   (__CONCAT(INT16_MAX, U))

Macros for integer constants

C++ implementations should define these macros only when __STDC_CONSTANT_MACROS is defined before <stdint.h> is included.
These definitions are valid for integer constants without suffix and for macros defined as integer constant without suffix
#define INT8_C(value)   ((int8_t) value)
#define UINT8_C(value)   ((uint8_t) __CONCAT(value, U))
#define INT16_C(value)   value
#define UINT16_C(value)   __CONCAT(value, U)
#define INT32_C(value)   __CONCAT(value, L)
#define UINT32_C(value)   __CONCAT(value, UL)
#define INT64_C(value)   __CONCAT(value, LL)
#define UINT64_C(value)   __CONCAT(value, ULL)
#define INTMAX_C(value)   __CONCAT(value, LL)
#define UINTMAX_C(value)   __CONCAT(value, ULL)


Detailed Description

 #include <stdint.h> 
Use [u]intN_t if you need exactly N bits.
Since these typedefs are mandated by the C99 standard, they are preferred over rolling your own typedefs.

Define Documentation

#define INT16_C ( value)    value
define a constant of type int16_t
#define INT16_MAX   0x7fff
largest positive value an int16_t can hold.
#define INT16_MIN   (-INT16_MAX - 1)
smallest negative value an int16_t can hold.
#define INT32_C ( value)    __CONCAT(value, L)
define a constant of type int32_t
#define INT32_MAX   0x7fffffffL
largest positive value an int32_t can hold.
#define INT32_MIN   (-INT32_MAX - 1L)
smallest negative value an int32_t can hold.
#define INT64_C ( value)    __CONCAT(value, LL)
define a constant of type int64_t
#define INT64_MAX   0x7fffffffffffffffLL
largest positive value an int64_t can hold.
#define INT64_MIN   (-INT64_MAX - 1LL)
smallest negative value an int64_t can hold.
#define INT8_C ( value)    ((int8_t) value)
define a constant of type int8_t
#define INT8_MAX   0x7f
largest positive value an int8_t can hold.
#define INT8_MIN   (-INT8_MAX - 1)
smallest negative value an int8_t can hold.
#define INT_FAST16_MAX   INT16_MAX
largest positive value an int_fast16_t can hold.
#define INT_FAST16_MIN   INT16_MIN
smallest negative value an int_fast16_t can hold.
#define INT_FAST32_MAX   INT32_MAX
largest positive value an int_fast32_t can hold.
#define INT_FAST32_MIN   INT32_MIN
smallest negative value an int_fast32_t can hold.
#define INT_FAST64_MAX   INT64_MAX
largest positive value an int_fast64_t can hold.
#define INT_FAST64_MIN   INT64_MIN
smallest negative value an int_fast64_t can hold.
#define INT_FAST8_MAX   INT8_MAX
largest positive value an int_fast8_t can hold.
#define INT_FAST8_MIN   INT8_MIN
smallest negative value an int_fast8_t can hold.
#define INT_LEAST16_MAX   INT16_MAX
largest positive value an int_least16_t can hold.
#define INT_LEAST16_MIN   INT16_MIN
smallest negative value an int_least16_t can hold.
#define INT_LEAST32_MAX   INT32_MAX
largest positive value an int_least32_t can hold.
#define INT_LEAST32_MIN   INT32_MIN
smallest negative value an int_least32_t can hold.
#define INT_LEAST64_MAX   INT64_MAX
largest positive value an int_least64_t can hold.
#define INT_LEAST64_MIN   INT64_MIN
smallest negative value an int_least64_t can hold.
#define INT_LEAST8_MAX   INT8_MAX
largest positive value an int_least8_t can hold.
#define INT_LEAST8_MIN   INT8_MIN
smallest negative value an int_least8_t can hold.
#define INTMAX_C ( value)    __CONCAT(value, LL)
define a constant of type intmax_t
#define INTMAX_MAX   INT64_MAX
largest positive value an intmax_t can hold.
#define INTMAX_MIN   INT64_MIN
smallest negative value an intmax_t can hold.
#define INTPTR_MAX   INT16_MAX
largest positive value an intptr_t can hold.
#define INTPTR_MIN   INT16_MIN
smallest negative value an intptr_t can hold.
#define PTRDIFF_MAX   INT16_MAX
largest positive value a ptrdiff_t can hold.
#define PTRDIFF_MIN   INT16_MIN
smallest negative value a ptrdiff_t can hold.
#define SIG_ATOMIC_MAX   INT8_MAX
largest positive value a sig_atomic_t can hold.
#define SIG_ATOMIC_MIN   INT8_MIN
smallest negative value a sig_atomic_t can hold.
#define SIZE_MAX   (__CONCAT(INT16_MAX, U))
largest value a size_t can hold.
#define UINT16_C ( value)    __CONCAT(value, U)
define a constant of type uint16_t
#define UINT16_MAX   (__CONCAT(INT16_MAX, U) * 2U + 1U)
largest value an uint16_t can hold.
#define UINT32_C ( value)    __CONCAT(value, UL)
define a constant of type uint32_t
#define UINT32_MAX   (__CONCAT(INT32_MAX, U) * 2UL + 1UL)
largest value an uint32_t can hold.
#define UINT64_C ( value)    __CONCAT(value, ULL)
define a constant of type uint64_t
#define UINT64_MAX   (__CONCAT(INT64_MAX, U) * 2ULL + 1ULL)
largest value an uint64_t can hold.
#define UINT8_C ( value)    ((uint8_t) __CONCAT(value, U))
define a constant of type uint8_t
#define UINT8_MAX   (__CONCAT(INT8_MAX, U) * 2U + 1U)
largest value an uint8_t can hold.
largest value an uint_fast16_t can hold.
largest value an uint_fast32_t can hold.
largest value an uint_fast64_t can hold.
#define UINT_FAST8_MAX   UINT8_MAX
largest value an uint_fast8_t can hold.
largest value an uint_least16_t can hold.
largest value an uint_least32_t can hold.
largest value an uint_least64_t can hold.
#define UINT_LEAST8_MAX   UINT8_MAX
largest value an uint_least8_t can hold.
#define UINTMAX_C ( value)    __CONCAT(value, ULL)
define a constant of type uintmax_t
#define UINTMAX_MAX   UINT64_MAX
largest value an uintmax_t can hold.
#define UINTPTR_MAX   UINT16_MAX
largest value an uintptr_t can hold.

Typedef Documentation

typedef signed int int16_t
16-bit signed type.
typedef signed long int int32_t
32-bit signed type.
typedef signed long long int int64_t
64-bit signed type.
Note:
This type is not available when the compiler option -mint8 is in effect.
typedef signed char int8_t
8-bit signed type.
fastest signed int with at least 16 bits.
fastest signed int with at least 32 bits.
fastest signed int with at least 64 bits.
Note:
This type is not available when the compiler option -mint8 is in effect.
fastest signed int with at least 8 bits.
signed int with at least 16 bits.
signed int with at least 32 bits.
signed int with at least 64 bits.
Note:
This type is not available when the compiler option -mint8 is in effect.
signed int with at least 8 bits.
typedef int64_t intmax_t
largest signed int available.
typedef int16_t intptr_t
Signed pointer compatible type.
typedef unsigned int uint16_t
16-bit unsigned type.
typedef unsigned long int uint32_t
32-bit unsigned type.
typedef unsigned long long int uint64_t
64-bit unsigned type.
Note:
This type is not available when the compiler option -mint8 is in effect.
typedef unsigned char uint8_t
8-bit unsigned type.
fastest unsigned int with at least 16 bits.
fastest unsigned int with at least 32 bits.
fastest unsigned int with at least 64 bits.
Note:
This type is not available when the compiler option -mint8 is in effect.
fastest unsigned int with at least 8 bits.
unsigned int with at least 16 bits.
unsigned int with at least 32 bits.
unsigned int with at least 64 bits.
Note:
This type is not available when the compiler option -mint8 is in effect.
unsigned int with at least 8 bits.
largest unsigned int available.
Unsigned pointer compatible type.

Src: http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html
--
Mathhoang
vietam_hoangminhnguyen
 
Related Posts Plugin for WordPress, Blogger...