C language – fgets

อ่านข้อมูลจากไฟล์ทีละบรรทัด

[code lang=”c”]
/* fgets exmaple */

#include

#define myfile "control"

int main() {
FILE * fp;

fp = fopen(myfile, "r");
if (fp == NULL)
perror("Error opening file");
else {
char mystring[100];
while (fgets(mystring, sizeof(mystring), fp)) {
printf(mystring);
}
fclose(fp);
}
return 0;
}
[/code]

ที่มา: cplusplus

Posted in C

C language – linked list

linked list ของตัวอักษร โดยการเพิ่มข้อมูลต่อท้าย ข้อมูลเดิม
[code lang=”c”]
/*linklist.h*/

typedef struct node * NODEPTR;

struct node {
char * data;
NODEPTR next;
} NODE;

void print_list(NODEPTR link);
int insert_list(char * value, NODEPTR * link);
NODEPTR make_node(char * value);
[/code]

[code lang=”c”]
/*linklist.c*/

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

#include "linklist.h"

int main() {

NODEPTR link = NULL;
char str1[] = "A10";
char str2[] = "A20";
char str3[] = "B1";
char str4[] = "B5";

insert_list(str1, &link);
insert_list(str2, &link);
insert_list(str3, &link);
insert_list(str4, &link);
print_list(link);
return 0;
}

void print_list(NODEPTR link) {
for (; link != NULL; link = link->next) {
printf("%s\n", link->data);
}
}

int insert_list(char * value, NODEPTR * link) {
NODEPTR curr = *link, prev = NULL, make_node(), temp;
for (; curr != NULL ; curr = curr->next)
prev = curr;
if ((temp = make_node(value)) != NULL) {
temp->next = curr;
if (prev == NULL)
*link = temp;
else
prev->next = temp;
}
return (temp != NULL);
}

NODEPTR make_node(char * value) {
NODEPTR newptr;

if ((newptr = (NODEPTR) malloc(sizeof(struct node))) != NULL) {
newptr->data = value;
newptr->next = NULL;
}
return (newptr);
}
[/code]

Posted in C

C language – strcpy

[code lang=”c”]
/* strcpy example */
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int main() {
char str1[] = "Sample string";
char str2[40];
char str3[40];

strcpy(str2, str1);
strcpy(str3, "copy successful");

printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
return 0;
}
[/code]

เปลี่ยน str2, str3 จาก array เป็น linked list
[code lang=”c”]
/* strcpy example */
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

int main() {
char str1[] = "Sample string";
char * str2 = (char*) calloc(0, sizeof(str1));
char * str3 = (char*) calloc(0, sizeof(str1));

strcpy(str2, str1);
strcpy(str3, "copy successful");

printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
return 0;
}
[/code]
ที่มา: cplusplus.com

Posted in C

C language – linked list

linked list ของ Integer  โดยการแทรกข้อมูล แบบเรียงลำดับจากน้อยไปมาก

ที่มา: การโปรแกรมภาษา C ดร.ดวงแก้ว สวามิภักดิ์

linklist.h

[code lang=”c”]
/*linklist.h*/

typedef struct node * NODEPTR;

struct node {
int data;
NODEPTR next;
} NODE;

void print_list(NODEPTR link);
int insert_list(int val, NODEPTR * link);
NODEPTR make_node(int value);
[/code]

linklist.c

[code lang=”c”]
/*linklist.c*/

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

int main() {

NODEPTR link = NULL; // the sorted linked list
int val; // input value

val = 10;
insert_list(val, &link);
val = 20;
insert_list(val, &link);
val = 1;
insert_list(val, &link);
val = 15;
insert_list(val, &link);

print_list(link);
return 0;
}

void print_list(NODEPTR link) {
for (; link != NULL; link = link->next) {
printf("%d\n", link->data);
}
}

int insert_list(int val, NODEPTR * link) {
NODEPTR curr = *link;
NODEPTR prev = NULL, make_node(), temp;
for (; curr != NULL && val > curr->data; curr = curr->next)
prev = curr;
if ((temp = make_node(val)) != NULL) {
temp->next = curr;
if (prev == NULL)
*link = temp;
else
prev->next = temp;
}
return (temp != NULL);
}

NODEPTR make_node(int value) {
NODEPTR newptr;
//char *malloc();

if ((newptr = (NODEPTR) malloc(sizeof(struct node))) != NULL) {
newptr->data = value;
newptr->next = NULL;
}
return (newptr);
}
[/code]

Posted in C