LinkedList practice in C

1. single (direction) linked list:

– linkedlist.c:

#include <stdio.h>
#include <stdlib.h>

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

node* create_node(int);
void insert_node(node*, node*);
void remove_node(node*);
void print_lists(node*);
void free_lists(node*);


node* create_node(int data){
node* n = (node*) malloc(sizeof(node));
n->data = data;
n->next = NULL;

return n;
}

void insert_node(node* n1, node* n2){
n2->next = n1->next;
n1->next = n2;
}

void remove_node(node* n1){
n1->next = n1->next->next;
}

void print_lists(node* lists){
node* n = lists;

while(n!=NULL){
printf(“%d”, n->data);
n = n->next;
}
printf(“n”);
}

void free_lists(node* lists){
if(lists->next != NULL){
free_lists(lists->next);
}
free(lists);
}


– main.c:

#include “linkedlist.c”

int main(void){
printf(“Hello worldn”);

node* singleLinkedListHead = create_node(9);

for(int i=0; i<10; i++){
node *tempNode = create_node(i);
insert_node(singleLinkedListHead, tempNode);
}

print_lists(singleLinkedListHead);
return 0;

}

2. double (direction) linked list :
– 
參考:
1. Infinite Loop:「演算」連結串列:
http://program-lover.blogspot.tw/2008/05/linked-list.html
2. [資料結構]教學:
http://notepad.yehyeh.net/Content/DS/CH04/3.php

How to create multithread in C

#include <stdio.h>#include <pthread.h>#include <stdlib.h>static int count=0;void thread(void){ int i; count++; for(i=0; i<3; i++){ printf(“This is %d pthreadn”, count); }}int main(int argc, char* argv[]){ printf(“Hello worldn”); pthread_t id; int i, ret; ret = pthread_create(&id, NULL, (void *) thread, NULL); if(ret!=0){ printf(“Create pthread error!n”); exit(1); } for(i=0; i<20 ; i++){ printf(“This is main process~~n”); } pthread_join(id, NULL); return 0;}

補充:
1. create new thread:
extern int pthread_create __P ((pthread_t *__thread, __const pthread_attr_t *__attr,void *(*__start_routine) (void *), void *__arg));
pthread_create的參數裡分成四個部分,
第一個參數把宣告好的pthread丟進去,
第二個參數是更改原本pthread的預設屬性(pthread_attr),
第三個參數是thread要做的事,
第四個參數是thread要做的事所吃的參數
2. waiting process:
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一個參數是main process要等待的thread,
第二個參數是thread的return值,把資源給回收回來
也可以使用pthread_exit()
3. 如何修改pthread的預設屬性? (pthread_attr)
#include <pthread.h>
pthread_attr_t attr;
pthread_t tid;

/*初始化属性值,均设为默认值*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

pthread_create(&tid, &attr, (void *) my_function, NULL);

4. 如何更改thread的priority? (sched_param)
#include <pthread.h>
#include <sched.h>
pthread_attr_t attr;
pthread_t tid;
sched_param param;
int newprio=20;

pthread_attr_init(&attr);
pthread_attr_getschedparam(&attr, &param);
param.sched_priority=newprio;
pthread_attr_setschedparam(&attr, &param);
pthread_create(&tid, &attr, (void *)myfunction, myarg);
5. More:(看參考中的網址)
1) Mutual exclusive
2) Semaphore
參考:
1. Linux Multi-Thread programming:
https://sites.google.com/site/myembededlife/Home/applications–development/linux-multi-thread-programming

sprintf in C & String.format in Java

可將字串插入可以變動的int
用for就可以產生一大批
ex. 1-01.txt, 1-02.txt, 1-03.txt ….等 很像但是有規則的字串
最常用在檔名處理上

sprintf in C:

sprintf(str, “%d”, num);
printf(“str = %sn”, str);

—————————————————————

String.format in Java:

// Store the formatted string in ‘result’
String result = String.format(“%4d”, i * j);
// Write the result to standard output
System.out.println( result );

System Programming ( Signal ) in C language – Fishing Game

#include <stdio.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
bool fishingRodFlag= false;
bool fishEscapeFlag= true;
bool waitingFishAlarm= true;
static int fish=0;
static int pid=0;
static int ranFishUpTime=0;
static int ranFishEscapeTime=0;
void sig_handler(int used){
switch(used){
case SIGINT:
if(!fishingRodFlag){
printf(“nCast the fishing rod!n”);
printf(“Bait into water, waiting fish…n”);
fishingRodFlag = true;
waitingFishAlarm = true;
fishEscapeFlag = true;
ranFishUpTime = (rand()%3)+3;
alarm(ranFishUpTime);
}else{
printf(“nPull the fishing rodn”);
if(!fishEscapeFlag){
fish++;
printf(“Catch a fish!n”);
alarm(0);
}else{
if(!waitingFishAlarm)
printf(“The bait is eatenn”);
}
fishingRodFlag = false;
alarm(0);
printf(“nFishing rod is ready!n”);
}
break;
case SIGTSTP:
printf(“nTotally catch fishes: %dn”,fish);
kill(pid, SIGSTOP);
break;
case SIGALRM:
if(waitingFishAlarm){
if(fishingRodFlag){
printf(“nFish is bitting, pull the fishing rod!n”);
ranFishEscapeTime= 3;  // 3 secs later will escape
alarm(ranFishEscapeTime);
fishEscapeFlag = false;
}
waitingFishAlarm = false;
}else{
fishEscapeFlag = true;
if(fishEscapeFlag){
printf(“nThe fish was escaped!n”);
}
}
break;
}
}
int main(int argc, char* argv[]){
pid = getpid();
//printf(“%d”, pid);
signal(SIGINT, sig_handler);
signal(SIGTSTP, sig_handler);
srand(time(NULL));
signal(SIGALRM, sig_handler);
printf(“nnThis program designed by RongSonHo from CCU comm departmentn”);
printf(“ for System Programming course.n”);
printf(“Designed Date: 2016/05/25(Wed.)n”);
printf(“nnWelcome to RS Fishing Game!n”);
printf(“This Game use system signal to implement.n”);
printf(“nRules:n”);
printf(“ Casting down the fishing rod / Pulling up the rod: Ctrl+Cn”);
printf(“ Exit the Game and get the fishing result: Ctrl+Zn”);
printf(“nnFishing rod is ready!n”);
while(1)
sleep(1);
return 0;

}

How to run a C++/Java program?

1. C++:

用隨意的編輯軟體打好程式後
ex:

#include <iostream>
using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
    if(argc!=4){
        cout<<“wrong argument number”<<endl;
    }
    else{
        cout<<“argv[1]=”<<argv[1]<<endl;
        cout<<“argv[2]=”<<argv[2]<<endl;
        cout<<“argv[3]=”<<argv[3]<<endl;
    }
    cout << “hello world!” << endl;
    return 0;
}
在terminal裡切到該目錄後先compile
>> g++ hw1.cpp
>> ./a.out

compile後會出現a.out
run時為什麼要打./
因為要告訴檔案是在目前目錄下的檔
而不是指令 例如ls 這種容易混淆的檔名
by 系統程式的熊老師

2. Java:

用隨意的編輯軟體打好程式後
ex:

public class hw1
{
    public static void main(String args[])
    {
        System.out.println(“helo world!”);
    }
}

在terminal裡切到該目錄後先compile
>> javac hw1.java
>> java hw1

compile後會出現hw1.class
但是run的時候不需要打出.class