카테고리

asm (27) bootloader_x86_grub (1) C (92) compile (11) config (76) CPP (13) CSS (1) debugging (7) gimp (1) Go (1) html (1) Java (1) JavaScript (1) kernel (19) LibreOffice (3) Linux system progamming (21) MFC (1) opencv (4) OpenGL (1) PHP (1) Python (4) qemu (29) shell (3) socket (7) troubleshooting (2) ubuntu18.04 (2) windows (1)

2019/01/04

callback test

/*
 * Callback.c - 연결 목록 콜백 구현
 */

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

typedef struct NODE {
struct NODE *link;
int data;
}Node;

Node **create_sll(const int);
void insert_data(Node **);
void show_list(Node **);
void sort_list(Node **);

int compare_ints(void const *, void const *); /* 프로토타입 함수 */
Node *search_list(Node *, void const *, int(void const *, void const *)); /* 프로토타입 */

int main(void){
int (*compare)(void const *, void const *) = compare_ints;

int value, val2find;
int nodes; /* 노트 카운트 */
Node **rootp; /* 루투 위치 */
Node *desired_node;

puts("\n**Program creates Singly Linked List**");
puts("**And allows users to perform various operations on the"
"list**\n");
puts("User, specify number of nodes in the list, in range 1"
" through some positive no.");
scanf("%d", &nodes);

rootp = create_sll(nodes);

printf("Les's insert %d integers in the list...\n", nodes);
insert_data(rootp);

puts("**Les's show up the list**");
show_list(rootp);

puts("Let's sort the list, in ascending order...");
sort_list(rootp);
puts("Let's show up the list**");
show_list(rootp);

puts("**Let's use Callback() function**");
printf("User, enter an integer you want to see into the "
"Singly Linked List...\n");
scanf("%d", &val2find);

/* 콜밸 함수 호출 */
desired_node = search_list(*rootp, &val2find, compare);

if(desired_node != NULL)
puts("Desired value is found.");
else
puts("Desired value NOT found.");
return 0;
}

Node *search_list(Node *node, void const *value, int compare(void const *, void const *)){
while(node != NULL){
if(compare(&node->data, value) == 0)
break;
node = node->link;
}

return node;
}

int compare_ints(void const *p2nv, void const *p2v){
if(*(int *)p2nv == *(int *)p2v)
return 0;
else
return 1;
}

Node **create_sll(const int nodes){
int i;
Node *current;
static Node *root;
Node **rootp = &root;

root = (Node *)malloc(nodes * sizeof(Node));

if(root == NULL){
puts("Error: Not Enough Memory!");
exit(1);
} else {
current = root;

for(i = 1; i <= nodes; i++){
if(i == nodes) {
current->link = NULL;
} else {
current->link = current + 1;
current++;
}
}
printf("List with %d nodes created successfully!\n", nodes);
puts("");
}
return rootp;
}

void insert_data(Node **linkp){
Node *next = *linkp;
Node *current;

do {
current = next;
scanf("%d", &(current->data));
next = current->link;
}while(current->link != NULL);
puts("");
}

void show_list(Node **linkp){
Node *next = *linkp;
Node *current;

do{
current = next;
printf("%d", current->data);
next = current->link;
}while(current->link != NULL);
puts("\n");
}

void sort_list(Node **linkp){
int temp;
Node *current = *linkp;
Node *next = current->link;

while(current->link != NULL){
while(next != NULL){
if(current->data > next->data){
temp = next->data;
next->data = current->data;
current->data = temp;
}
next = next->link;
}
current = current->link;
next = current->link;
}
}
/*
 * ./callback

**Program creates Singly Linked List**
**And allows users to perform various operations on thelist**

User, specify number of nodes in the list, in range 1 through some positive no.
5
List with 5 nodes created successfully!

Les's insert 5 integers in the list...
32
53
1
08
0556

**Les's show up the list**
325318556

Let's sort the list, in ascending order...
Let's show up the list**
183253556

**Let's use Callback() function**
User, enter an integer you want to see into the Singly Linked List...
53
Desired value is found */

댓글 없음:

댓글 쓰기