linkedlist_a + linkedlist_b
//작성자: 김준현
//작성일: 2015 08
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int DATA;
typedef struct linkedList
{
DATA element;
struct linkedList* connect;
}linkedList;
typedef struct
{
linkedList* head;
linkedList* tail;
DATA nNodeCnt;
}link;
void initiallize_linkedList(link*);//초기화
void linkedList_Data_Input(link*, const int);//데이터 삽입
void linkedList_Data_Printf(link*);
void merge(link*, link*,link*);//link1, link2, link1+2
int main(void)
{
link linkedlist_1;
link linkedlist_2;
link linkedlist_1_sum_linkedlist_2;
//linkedlist_1 과 linkedlist_2 초기화
initiallize_linkedList(&linkedlist_1);
initiallize_linkedList(&linkedlist_2);
initiallize_linkedList(&linkedlist_1_sum_linkedlist_2);
//linkedlist_1 과 linkedlist_2 데이터 삽입
//LINKEDLIST_1
linkedList_Data_Input(&linkedlist_1, 10);
linkedList_Data_Input(&linkedlist_1, 0);
linkedList_Data_Input(&linkedlist_1, 8);
linkedList_Data_Input(&linkedlist_1, 2);
linkedList_Data_Input(&linkedlist_1, 1);
linkedList_Data_Input(&linkedlist_1, 111);
linkedList_Data_Input(&linkedlist_1, 12);
linkedList_Data_Input(&linkedlist_1, 3);
linkedList_Data_Input(&linkedlist_1, 5);
linkedList_Data_Input(&linkedlist_1, 77);
printf("linkedlist_1: ");
linkedList_Data_Printf(&linkedlist_1);
//LINKEDLIST_2
linkedList_Data_Input(&linkedlist_2, 22);
linkedList_Data_Input(&linkedlist_2, 13);
linkedList_Data_Input(&linkedlist_2, 1114);
linkedList_Data_Input(&linkedlist_2, 212);
linkedList_Data_Input(&linkedlist_2, 30);
printf("linkedlist_2: ");
linkedList_Data_Printf(&linkedlist_2);
merge(&linkedlist_1, &linkedlist_2, &linkedlist_1_sum_linkedlist_2);
printf("linkedlist_1 + linkedlist_2: ");
linkedList_Data_Printf(&linkedlist_1_sum_linkedlist_2);
return 0;
}
void initiallize_linkedList(link* L)
{
L->head = (linkedList*)malloc(sizeof(linkedList));
memset(L->head, 0, sizeof(linkedList));
L->tail = L->head;
L->nNodeCnt = 0;//데이터 갯수 초기화
}
void linkedList_Data_Input(link* L, const int DATA)
{
linkedList* new_node = (linkedList*)malloc(sizeof(linkedList));
if (new_node == NULL)
{
printf("동적할당 실패!!\n");
return;//종료
}
else//(new_node != NULL)
{
memset(new_node, 0, sizeof(linkedList));
if (L->head->connect == NULL)
{
L->nNodeCnt++; // 데이터 갯수 +1
new_node->element = DATA;
L->tail->connect = new_node;// L->tail-------new_node
L->tail = new_node;
L->tail->connect = NULL;
}
else
{
linkedList* temp_index = L->head;
int temp_count = 0;
while (temp_count != L->nNodeCnt)
{
temp_index = temp_index->connect;
temp_count++;
}
temp_index->connect = new_node;
L->nNodeCnt++;// 데이터 갯수 +1
new_node->element = DATA;
L->tail = new_node;
L->tail->connect = NULL;
}
}
}
void linkedList_Data_Printf(link* L)
{
linkedList* index_ = L->head->connect;
while (index_ != NULL)
{
printf("%d ", index_->element);
index_ = index_->connect;
}
printf("\n");
}
void merge(link* L1, link* L2, link* L12)
{
int total_size = 0;
total_size = L1->nNodeCnt + L2->nNodeCnt;
DATA* array_temp = (DATA*)malloc(sizeof(DATA)* (L1->nNodeCnt + L2->nNodeCnt));
memset(array_temp, 0, sizeof(DATA)* total_size);
int j = 0;//index
int i = 0;
int temp_exchange = 0;
int result = 0;
L1->tail->connect = L2->head->connect;
L12->head = L1->head;
L12->tail = L2->tail;
L12->nNodeCnt = total_size;
linkedList* index_temp = L12->head->connect;
while (index_temp != NULL)
{
array_temp[i] = index_temp->element;
i++;
index_temp = index_temp->connect;
}
/*
printf("test----print---------------\n");
for (i = 0; i < L12->nNodeCnt; i++)
{
printf("%d ", array_temp[i]);
}
*/
printf("\n");
//bubble sort
for (i = 0; i < L12->nNodeCnt - 1; i++)
{
result = 0;
for (j = 0; j < L12->nNodeCnt - 1; j++)
{
if (array_temp[j]>array_temp[j + 1])
{
temp_exchange = array_temp[j];
array_temp[j] = array_temp[j + 1];
array_temp[j + 1] = temp_exchange;
result = 1;
}
}
if (result == 0)
{
break;
}
}
index_temp = L12->head->connect;
i = 0;
while (index_temp != NULL)
{
index_temp->element = array_temp[i];
i++;
index_temp = index_temp->connect;
}
}
'언어' 카테고리의 다른 글
naver 지식인 - 1 (2015/ 08/ 30) (0) | 2015.08.30 |
---|---|
2015_08_27 (0) | 2015.08.27 |
swap (0) | 2015.08.27 |
linkedlist _ 오름차순 정렬 (0) | 2015.08.25 |
LOTTO_VER2 (0) | 2015.08.24 |
linkedlist _ 오름차순 정렬
//작성자: 김준현
//작성일: 2015 08
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef int DATA;
typedef struct linkedList
{
DATA element;
struct linkedList* connect;
}linkedList;
typedef struct
{
linkedList* head;
linkedList* tail;
}link;
DATA nNodeCnt = 0;//데이터의 갯수 counting
void initiallize_linkedList(link*);//초기화
void linkedList_Data_Input(link*, const int);//데이터 삽입
void linkedList_Data_Printf(link*);
void temp_ARRAY_DATA_SORT(link*);
int main(void)
{
link linkedlist_1;
initiallize_linkedList(&linkedlist_1);
linkedList_Data_Input(&linkedlist_1, 10);
linkedList_Data_Input(&linkedlist_1, 0);
linkedList_Data_Input(&linkedlist_1, 8);
linkedList_Data_Input(&linkedlist_1, 2);
linkedList_Data_Input(&linkedlist_1, 1);
linkedList_Data_Input(&linkedlist_1, 111);
linkedList_Data_Input(&linkedlist_1, 12);
linkedList_Data_Input(&linkedlist_1, 3);
linkedList_Data_Input(&linkedlist_1, 5);
linkedList_Data_Input(&linkedlist_1, 77);
linkedList_Data_Printf(&linkedlist_1);
printf("데이터의 갯수: %d\n", nNodeCnt);
temp_ARRAY_DATA_SORT(&linkedlist_1);
printf("sorting 작업 후----------------\n");
linkedList_Data_Printf(&linkedlist_1);
return 0;
}
void initiallize_linkedList(link* L)
{
L->head = (linkedList*)malloc(sizeof(linkedList));
memset(L->head, 0, sizeof(linkedList));
L->tail = L->head;
}
void linkedList_Data_Input(link* L, const int DATA)
{
linkedList* new_node = (linkedList*)malloc(sizeof(linkedList));
if (new_node == NULL)
{
printf("동적할당 실패!!\n");
return;//종료
}
else//(new_node != NULL)
{
memset(new_node, 0, sizeof(linkedList));
if (L->head->connect == NULL)
{
nNodeCnt++; // 데이터 갯수 +1
new_node->element = DATA;
L->tail->connect = new_node;// L->tail-------new_node
L->tail = new_node;
L->tail->connect = NULL;
}
else
{
linkedList* temp_index = L->head;
int temp_count = 0;
while (temp_count != nNodeCnt)
{
temp_index = temp_index->connect;
temp_count++;
}
temp_index->connect = new_node;
nNodeCnt++;// 데이터 갯수 +1
new_node->element = DATA;
L->tail = new_node;
L->tail->connect = NULL;
}
}
}
void linkedList_Data_Printf(link* L)
{
linkedList* index_ = L->head->connect;
while (index_ != NULL)
{
printf("%d ", index_->element);
index_ = index_->connect;
}
printf("\n");
}
void temp_ARRAY_DATA_SORT(link* L)
{
DATA* array_temp = (DATA*)malloc(sizeof(DATA)* nNodeCnt);
memset(array_temp, 0, sizeof(DATA)* nNodeCnt);
int j = 0;//index
int i = 0;
int temp_exchange = 0;
int result = 0;
linkedList* index_temp = L->head->connect;
while (index_temp != NULL)
{
array_temp[i] = index_temp->element;
i++;
index_temp = index_temp->connect;
}
printf("test----print---------------\n");
for (i = 0; i < nNodeCnt; i++)
{
printf("%d ", array_temp[i]);
}
printf("\n");
//bubble sort
for (i = 0; i < nNodeCnt - 1; i++)
{
result = 0;
for (j = 0; j < nNodeCnt - 1; j++)
{
if (array_temp[j]>array_temp[j + 1])
{
temp_exchange = array_temp[j];
array_temp[j] = array_temp[j + 1];
array_temp[j + 1] = temp_exchange;
result = 1;
}
}
if (result == 0)
{
break;
}
}
index_temp = L->head->connect;
i = 0;
while (index_temp != NULL)
{
index_temp->element = array_temp[i];
i++;
index_temp = index_temp->connect;
}
}
'언어' 카테고리의 다른 글
naver 지식인 - 1 (2015/ 08/ 30) (0) | 2015.08.30 |
---|---|
2015_08_27 (0) | 2015.08.27 |
swap (0) | 2015.08.27 |
linkedlist_a + linkedlist_b (0) | 2015.08.25 |
LOTTO_VER2 (0) | 2015.08.24 |
LOTTO_VER2
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define LOTTO_MAX_SIZE 45
#define LOTTO_MIN_SIZE 6
#define LOTTO_RESULT_SIZE 7
#define The_End 0
typedef int LOTTO_BALL;
////////////////////////////////////////////////////////
void LOTTO_BALL_SETTING(LOTTO_BALL*);
void LOTTO_RANDOM_RECOMMEND(LOTTO_BALL*, LOTTO_BALL[], LOTTO_BALL[]);
void LOTTO_(LOTTO_BALL**, int);
void LOTTO_AUTO(LOTTO_BALL**, LOTTO_BALL[], LOTTO_BALL[],int);
LOTTO_BALL LOTTO_BONUS(LOTTO_BALL*, LOTTO_BALL[], LOTTO_BALL*, LOTTO_BALL[]);
void LOTTO_MATCHING(LOTTO_BALL**, LOTTO_BALL*,const int);
////////////////////////////////////////////////////////
int main(void)
{
srand((unsigned)time(NULL));
LOTTO_BALL LOTTO[LOTTO_MAX_SIZE] = { 0, };//초기화
LOTTO_BALL LOTTO_TEMP_[LOTTO_MAX_SIZE] = { 0, };//초기화
LOTTO_BALL lotto_random_recommend[LOTTO_MIN_SIZE] = { 0, };//초기화
LOTTO_BALL lotto[LOTTO_MIN_SIZE] = { 0, };
LOTTO_BALL lotto_temp[LOTTO_RESULT_SIZE] = { 0, };
int i, k,j;//index
int result = 0;
int count;
int s;
int l = 0;
/////////////////////함수 호출///////////////////////////
LOTTO_BALL_SETTING(LOTTO);//최초 1~45 setting
LOTTO_RANDOM_RECOMMEND(lotto_random_recommend, LOTTO, LOTTO_TEMP_);
lotto_temp[6] = LOTTO_BONUS(lotto_random_recommend, LOTTO, lotto_temp, LOTTO_TEMP_);
////////////////////////////////////////////////////////
printf("몇 번 하시겠습니까??: ");
scanf_s("%d", &count);
LOTTO_BALL** LOTTO_S = NULL;
LOTTO_S = (LOTTO_BALL**)malloc(sizeof(LOTTO_BALL*)*count);
for (i = 0; i < count; i++)
{
LOTTO_S[i] = (LOTTO_BALL*)malloc(sizeof(LOTTO_BALL)*LOTTO_MIN_SIZE);
}
for (i = 0; i < count; i++)
{
for (j = 0; j < LOTTO_MIN_SIZE; j++)
{
LOTTO_S[i][j] = 0;
}
}
printf("자동-> (1)번\n");
printf("수동-> (2)번\n");
i = 0;
///////////////////////////////////////////////////////
while (result != count)
{
printf("입력[%02d]: ", i+1);
scanf_s("%d", &s);
if (s == 1)
{
LOTTO_AUTO(LOTTO_S, LOTTO, LOTTO_TEMP_, i);
}
else if (s == 2)
{
LOTTO_(LOTTO_S, i);
}
i++;
result++;//최초:0 -> 그 후 +1, count하고 같아지면 종료
}
///////////////////////////////////////////////////////
printf("-----------------------------------\n");
for (i = 0; i < count; i++)
{
for (k = 65 + l; k <= 65 + l; k++)
{
printf("%c ", k);
}
for (j = 0; j < LOTTO_MIN_SIZE; j++)
{
printf("%02d ", LOTTO_S[i][j]);
}
l++;
printf("\n");
}
printf("-----------------------------------\n");
printf("금액 %d원\n", 1000 * count);
printf("\n");
//(2)당첨 번호
printf("-----------------------------------\n");
for (i = 0; i < LOTTO_RESULT_SIZE; i++)
{
if (i == LOTTO_RESULT_SIZE - 1)
{
printf(" +보너스 번호: ");
}
printf("%02d ", lotto_temp[i]);
}
printf("\n");
printf("-----------------------------------\n");
////////////////////////////////////////////////////
LOTTO_MATCHING(LOTTO_S, lotto_temp, count);
return The_End;
}
////////////////////////////////////////////////////////
void LOTTO_BALL_SETTING(LOTTO_BALL* lotto_ball)
{
int i;
for (i = 0; i < LOTTO_MAX_SIZE; i++)
{
*(lotto_ball + i) = i + 1;//1부터 45까지 입력
}
}
void LOTTO_RANDOM_RECOMMEND(LOTTO_BALL*lotto_r_r, LOTTO_BALL LOTTO[], LOTTO_BALL LOTTO_TEMP_[])
{
int i, j;//INDEX
int result;
int temp_size = 45;//random number size
int temp_exchange = 0;//bubble sort시 필요함
int temp = 0;//배열에서 임시로 idex 값을 받음
int temp_ball = 0;//배열에서 임시로 값을 받음
int temp_size_max = LOTTO_MAX_SIZE;
int temp_size_min = LOTTO_MIN_SIZE;
for (i = 0; i < LOTTO_MAX_SIZE; i++)
{
LOTTO_TEMP_[i] = LOTTO[i];
}
for (i = 0; i < temp_size_min; i++)
{
temp = rand() % temp_size;
temp_ball = LOTTO_TEMP_[temp];
*(lotto_r_r + i) = temp_ball;
for (j = temp; j < temp_size_max-1; j++)
{
LOTTO_TEMP_[j] = LOTTO_TEMP_[j + 1];
}
temp_size_max--;
temp_size--;
}
//정렬(bubble sort)
for (i = 0; i<LOTTO_MIN_SIZE-1; i++)
{
result = 0;
for (j = 0; j < LOTTO_MIN_SIZE - 1; j++)
{
if (*(lotto_r_r + j) > *(lotto_r_r + (j + 1)))
{
temp_exchange = *(lotto_r_r + j);
*(lotto_r_r + j) = *(lotto_r_r + (j + 1));
*(lotto_r_r + (j + 1)) = temp_exchange;
result = 1;
}
}
if (result == 0)
{
break;
}
}
}
void LOTTO_(LOTTO_BALL** lotto, int count)
{
int i,j;
int result = 0;
int temp_exchange = 0;
printf(" 1 ~ 45 까지 6자리 숫자를 입력하세요: \n");
for (i = 0; i < LOTTO_MIN_SIZE; i++)
{
scanf_s("%d", (*(lotto + count)+i));
}
//정렬(bubble sort)
for (i = 0; i<LOTTO_MIN_SIZE - 1; i++)
{
result = 0;
for (j = 0; j < LOTTO_MIN_SIZE - 1; j++)
{
if (*(*(lotto + count) + j) > *(*(lotto + count) + (j + 1)))
{
temp_exchange = *(*(lotto + count) + j);
*(*(lotto + count) + j) = *(*(lotto + count) + (j + 1));
*(*(lotto + count) + (j + 1)) = temp_exchange;
result = 1;
}
}
if (result == 0)
{
break;
}
}
}
void LOTTO_AUTO(LOTTO_BALL** lotto, LOTTO_BALL LOTTO[], LOTTO_BALL LOTTO_TEMP_[],int count)
{
int i, j;//INDEX
int result = 0;
int temp_size = 45;//random number size
int temp_exchange = 0;//bubble sort시 필요함
int temp = 0;//배열에서 임시로 idex 값을 받음
int temp_ball = 0;//배열에서 임시로 값을 받음
int temp_size_max = LOTTO_MAX_SIZE;
int temp_size_min = LOTTO_MIN_SIZE;
for (i = 0; i < LOTTO_MAX_SIZE; i++)
{
LOTTO_TEMP_[i] = LOTTO[i];
}
for (i = 0; i < temp_size_min; i++)
{
temp = rand() % temp_size;
temp_ball = LOTTO_TEMP_[temp];
*(*(lotto + count) + i) = temp_ball;
for (j = temp; j < temp_size_max - 1; j++)
{
LOTTO_TEMP_[j] = LOTTO_TEMP_[j + 1];
}
temp_size_max--;
temp_size--;
}
//정렬(bubble sort)
for (i = 0; i<LOTTO_MIN_SIZE - 1; i++)
{
result = 0;
for (j = 0; j < LOTTO_MIN_SIZE - 1; j++)
{
if ( *(*(lotto +count)+j) > *(*(lotto +count)+(j + 1)))
{
temp_exchange = *(*(lotto + count) + j);
*(*(lotto + count) + j) = *(*(lotto + count) + (j + 1));
*(*(lotto + count) + (j + 1)) = temp_exchange;
result = 1;
}
}
if (result == 0)
{
break;
}
}
}
LOTTO_BALL LOTTO_BONUS(LOTTO_BALL* lotto, LOTTO_BALL LOTTO_[], LOTTO_BALL* lotto_temp, LOTTO_BALL LOTTO_TEMP_[])
{
int i, j,k;
int temp_ball = 0;
int temp_size_max = LOTTO_MAX_SIZE;
int temp_size_min = LOTTO_MIN_SIZE;
for (i = 0; i < LOTTO_MAX_SIZE; i++)
{
LOTTO_TEMP_[i] = LOTTO_[i];
}
for (i = 0; i < LOTTO_MIN_SIZE; i++)
{
*(lotto_temp + i) = *(lotto+i);
}
for (i = 0; i < temp_size_min; i++)
{
for (j = 0; j <temp_size_max; j++)
{
if (*(lotto + i) == LOTTO_TEMP_[j])
{
for (k = j; k < temp_size_max - 1; k++)
{
LOTTO_TEMP_[k] = LOTTO_TEMP_[k + 1];
}
--temp_size_max;
}
}
}
temp_ball = LOTTO_TEMP_[rand() % temp_size_max];
return temp_ball;
}
void LOTTO_MATCHING(LOTTO_BALL** MeLOTTO, LOTTO_BALL* Match_LOTTO, const int size)
{
//*(*(lotto + count) + j)
printf("당첨 확인을 시작합니다.\n");
int i, j, k;
int count = 0;
int bonus_count = 0;
for (i = 0; i < size; i++)
{
for (j = 0; j < LOTTO_MIN_SIZE; j++)
{
for (k = 0; k < LOTTO_MIN_SIZE; k++)
{
if (*(*(MeLOTTO + i) + j) == *(Match_LOTTO + k))
{
count++;
}
if (*(*(MeLOTTO + i) + j) == *(Match_LOTTO + LOTTO_RESULT_SIZE-1))
{
bonus_count=1;
}
}
}
printf("입력번호:");
for (j = 0; j < LOTTO_MIN_SIZE; j++)
{
printf("%02d ", (*(*(MeLOTTO + i) + j)));
}
printf("보너스 번호:%d ", *(Match_LOTTO + LOTTO_RESULT_SIZE - 1));
printf("번호 일치:%d ", count);
if (count == 6)
{
printf("등수 1등\n");
}
else if (count == 5)
{
if (bonus_count == 1)
{
printf("등수2등\n");
}
else
{
printf("등수3등\n");
}
}
else if (count == 4)
{
printf("등수4등\n");
}
else if (count == 3)
{
printf("등수5등\n");
}
else
{
printf("등수에 없습니다. \n");
}
count = 0;
bonus_count = 0;
}
}
'언어' 카테고리의 다른 글
naver 지식인 - 1 (2015/ 08/ 30) (0) | 2015.08.30 |
---|---|
2015_08_27 (0) | 2015.08.27 |
swap (0) | 2015.08.27 |
linkedlist_a + linkedlist_b (0) | 2015.08.25 |
linkedlist _ 오름차순 정렬 (0) | 2015.08.25 |