C Selection Sort로 오름차순 정렬하는 방식입니다.

시간복잡도 : O(n^2)


#include <stdio.h>


void printArray(int value[], int count) {

int i = 0;

for (i = 0; i < count; i++) {

printf("%d ", value[i]);

}

printf("\n");

}


void selectionSort(int value[], int count) {

int i = 0, j = 0, min = 0, temp = 0;

for (i = 0; i < count - 1; i++) {

min = i;

for (j = i + 1; j < count; j++) {

if (value[j] < value[min]) {

min = j;

}

}

temp = value[i];

value[i] = value[min];

value[min] = temp;

printf("Step-%d, ", i + 1);

printArray(value, count);

}

}


int main(int argc, char *argv[]) {

int values[] = { 80, 50, 70, 10, 60, 20, 40, 30 };

printf("Before Selction\n");

printArray(values, 8);


selectionSort(values, 8);


printf("\nAfter Sort\n");

printArray(values, 8);

return 0;

}

'Programming > C' 카테고리의 다른 글

Sorting(5) Merge Sort(합병 정렬)  (0) 2016.11.01
Sorting(4) Quick Sort(퀵 정렬)  (0) 2016.11.01
Sorting(2) Insertion Sort(삽입 정렬)  (0) 2016.11.01
Sorting(1) Bubble Sort(버블 정렬)  (0) 2016.11.01
(6) CircularList 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,

C Insertion Sort로 오름차순 정렬하는 방식입니다.


시간복잡도 : O(n^2)


#include <stdio.h>


void printArray(int value[], int count) {

int i = 0;

for (i = 0; i < count; i++) {

printf("%d ", value[i]);

}

printf("\n");

}


void insertionSort(int value[], int count) {

int i = 0, j = 0, temp = 0;

for (i = 1; i < count; i++) {

temp = value[i];

j = i;

while ((j > 0) && value[j - 1] > temp) {

value[j] = value[j - 1];

j = j - 1;

}

value[j] = temp;

printf("Step-%d,", i);

printArray(value, count);

}

}

int main(int argc, char *argv[]) {

int values[] = { 80, 50, 70, 10, 60, 20, 40, 30 };

printf("Before InsertionSort\n");

printArray(values, 8);


insertionSort(values, 8);


printf("\nAfter Sort\n");

printArray(values, 8);


return 0;

}

'Programming > C' 카테고리의 다른 글

Sorting(4) Quick Sort(퀵 정렬)  (0) 2016.11.01
Sorting(3) Selection Sort(선택 정렬)  (0) 2016.11.01
Sorting(1) Bubble Sort(버블 정렬)  (0) 2016.11.01
(6) CircularList 사용하기  (0) 2016.10.30
(5) DoubleLinkedList 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,

C Bubble Sort로 오름차순 정렬하는 방식입니다.

시간복잡도 : O(n^2)


#include <stdio.h>


void printArray(int value[], int count) {

int i = 0;

for (i = 0; i < count; i++) {

printf("%d ", value[i]);

}

printf("\n");

}


void bubbleSort(int value[], int count) {

int i = 0, j = 0, temp = 0;

for (i = count - 1; i >= 0; i--) {

for (j = 0; j <= i; j++) { // i 뒤쪽은 다 정렬이 된 자료들이 남아있게 되므로, j <= i 가 된다.

if (value[j - 1] > value[j]) {

temp = value[j - 1];

value[j - 1] = value[j];

value[j] = temp;

}

}

printf("Step-%d, ", count - i);

printArray(value, count);

}

}

int main(int argc, char *argv[]) {

int values[] = { 80, 50, 70, 10, 60, 20, 40, 30 };

printf("Before BubbleSort\n");

printArray(values, 8);

bubbleSort(values, 8);


printf("\nAfter Sort\n");

printArray(values, 8);

return 0;

}



WRITTEN BY
SiriusJ

,

Circular list를 이용하여 List에 데이터를 넣었다가 빼고, 리스트에 존재하는지 등에 대한 Function들을 작성해보았습니다.

(비교, 단일 LinkedList 사용하기 : http://jwprogramming.tistory.com/230 , 

DoubleLinkedList 사용하기 : http://jwprogramming.tistory.com/232 )


(1) circularlistMain.c

#include <stdio.h>

#include <stdlib.h>

#include "Circularlist.h"


int main(int argc, char* argv[]) {

int i = 0;

int arrayCount = 0;

CircularList * pList = NULL;

CircularListNode * pNode = NULL;

CircularListNode node;

pList = createCircularList();

if (pList != NULL) {

node.data = 1;

addCLElement(pList, 0, node);

node.data = 3;

addCLElement(pList, 1, node);

node.data = 5;

addCLElement(pList, 2, node);

displayCircularList(pList);

removeCLElement(pList, 0);

displayCircularList(pList);

deleteCircularList(pList);

}

return 0;

}


(2) circularlist.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "circularlist.h"


int addCLElement(CircularList* pList, int position, CircularListNode element) {

int ret = FALSE;

int i = 0;

CircularListNode *pPreNode = NULL, *pNewNode = NULL, *pLastNode = NULL;


if (pList != NULL) {

if (position >= 0 && position <= pList->currentElementCount) {

pNewNode = (CircularListNode*)malloc(sizeof(CircularListNode));

if (pNewNode == NULL) {

printf("오류, 메모리할당 addLLElement() \n");

return ret;

}

*pNewNode = element;

pNewNode->pLink = NULL;

if (position == 0) {

if (pList->currentElementCount == 0) {

pList->pLink = pNewNode;

pNewNode->pLink = pNewNode;

}

else {

pLastNode = pList->pLink;

while (pLastNode->pLink != pList->pLink) {

pLastNode = pLastNode->pLink;

}

pList->pLink = pNewNode;

pNewNode->pLink = pLastNode->pLink;

pLastNode->pLink = pNewNode;

}

}

else {

pPreNode = pList->pLink;

for (i = 0; i < position - 1; i++) {

pPreNode = pPreNode->pLink;

}

pNewNode->pLink = pPreNode->pLink;

pPreNode->pLink = pNewNode;

}

pList->currentElementCount++;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d], addCLElement() \n", position);

}

}


return ret;

}


int removeCLElement(CircularList* pList, int position) {

int ret = FALSE;

int i = 0, arrayCount = 0;

CircularListNode *pPreNode = NULL, *pDelNode = NULL, *pLastNode = NULL;


if (pList != NULL) {

arrayCount = getCircularListLength(pList);

if (position >= 0 && position < arrayCount) {

if (position == 0) {

pDelNode = pList->pLink;

if (arrayCount == 1) {

free(pDelNode);

pList->pLink = NULL;

}

else {

pLastNode = pList->pLink;

while (pLastNode->pLink != pList->pLink) {

pLastNode = pLastNode->pLink;

}

pLastNode->pLink = pDelNode->pLink;

pList->pLink = pDelNode->pLink;

free(pDelNode);

}

}

else {

pPreNode = pList->pLink;

for (i = 0; i < position - 1; i++) {

pPreNode = pPreNode->pLink;

}

pDelNode = pPreNode->pLink;

pPreNode = pDelNode->pLink;

free(pDelNode);

}

pList->currentElementCount--;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] removeCLElement() \n", position);

}

}

return ret;

}


CircularListNode* getCLElement(CircularList* pList, int position) {

int i = 0;

CircularListNode* pNode = NULL;

if (pList != NULL) {

if (position >= 0 && position < pList->currentElementCount) {

pNode = pList->pLink;

for (i = 0; i <= position; i++) {

pNode = pNode->pLink;

}

}

}

return pNode;

}


CircularList* createCircularList() {

CircularList *pReturn = NULL;

int i = 0;


pReturn = (CircularList *)malloc(sizeof(CircularList));

if (pReturn != NULL) {

memset(pReturn, 0, sizeof(CircularList));

}

else {

printf("오류, 메모리할당 createCircularList()\n");

return NULL;

}

return pReturn;

}


void displayCircularList(CircularList* pList) {

int i = 0;

if (pList != NULL) {

printf("현재 원소 개수: %d\n", pList->currentElementCount);

for (i = 0; i < pList->currentElementCount; i++) {

printf("[%d], %d\n", i, getCLElement(pList, i)->data);

}

}

else {

printf("원소가 없습니다.\n");

}

}


int getCircularListLength(CircularList* pList) {

int ret = 0;

if (pList != NULL) {

ret = pList->currentElementCount;

}


return ret;

}


void deleteCircularList(CircularList* pList) {

int i = 0;

if (pList != NULL) {

clearCircularList(pList);

free(pList);

}

}


void clearCircularList(CircularList* pList) {

if (pList != NULL) {

if (pList->currentElementCount > 0) {

removeCLElement(pList, 0);

}

}

}


(3) circularlist.h

#ifndef _CIRCULARLIST_

#define _CIRCULARLIST_


typedef struct CircularListNodeType

{

int data;

struct CircularListNodeType* pLink;

} CircularListNode;


typedef struct CircularListType

{

int currentElementCount;

CircularListNode* pLink;

} CircularList;


void displayCircularList(CircularList* pList);

CircularList* createCircularList();

int addCLElement(CircularList* pList, int position, CircularListNode element);

int removeCLElement(CircularList* pList, int position);

void deleteCircularList(CircularList* pList);

void clearCircularList(CircularList* pList);

int getCircularListLength(CircularList* pList);


#endif


#ifndef _COMMON_LIST_DEF_

#define _COMMON_LIST_DEF_

#define TRUE 1

#define FALSE 0


#endif


WRITTEN BY
SiriusJ

,

이번에는 Double LinkedList를 이용하여 데이터를 넣었다가 빼고, 리스트에 존재하는지 등에 대한 Function들을 작성해보았습니다.

(SingleLinkedList와 비교 : http://jwprogramming.tistory.com/230 )

(CircularList와 비교 : http://jwprogramming.tistory.com/233 )


(1) doublylistMain.c

#include <stdio.h>

#include <stdlib.h>

#include "doublelist.h"


int main(int argc, char* argv[]) {

int i = 0;

int arrayCount = 0;

DoublyList *pList = NULL;

DoublyListNode *pValue = NULL;

DoublyListNode node = { 0, };


pList = createDoublyList();

if (pList != NULL) {

node.data = 1;

addDLElement(pList, 0, node);

node.data = 3;

addDLElement(pList, 1, node);


node.data = 5;

addDLElement(pList, 2, node);

displayDoublyList(pList);

removeDLElement(pList, 0);

displayDoublyList(pList);

deleteDoublyList(pList);

}

return 0;

}


(2) doublylist.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "doublelist.h"


DoublyList* createDoublyList() {

DoublyList *pReturn = NULL;

int i = 0;


pReturn = (DoublyList *)malloc(sizeof(DoublyList));

if (pReturn != NULL) {

memset(pReturn, 0, sizeof(DoublyList));


pReturn->headerNode.pLLink = &(pReturn->headerNode);

pReturn->headerNode.pRLink = &(pReturn->headerNode);

}

else {

printf("오류, 메모리 할당 createDoublyList() \n");

return NULL;

}


return pReturn;

}


int addDLElement(DoublyList* pList, int position, DoublyListNode element) {

int ret = FALSE, i = 0;


DoublyListNode *pPreNode = NULL, *pNewNode = NULL, *pTempNode = NULL;

if (pList != NULL) {

if (position >= 0 && position <= pList->currentElementCount) {

pNewNode = (DoublyListNode *)malloc(sizeof(DoublyList));

if (pNewNode == NULL) {

printf("오류, 메모리 할당 addDLElement()\n");

return ret;

}

*pNewNode = element;

pNewNode->pLLink = NULL;

pNewNode->pRLink = NULL;


pPreNode = &(pList->headerNode);


for (i = 0; i < position; i++) {

pPreNode = pPreNode->pRLink;

}

pNewNode->pLLink = pPreNode;

pNewNode->pRLink = pPreNode->pRLink;

pPreNode->pRLink = pNewNode;

pNewNode->pRLink->pLLink = pNewNode;


pList->currentElementCount++;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] addDLElement()\n", position);

}

}


return ret;

}


int removeDLElement(DoublyList* pList, int position) {

int ret = FALSE;

int i = 0, arrayCount = 0;

DoublyListNode *pPreNode = NULL, *pDelNode = NULL, *pTempNode = NULL;


if (pList != NULL) {

arrayCount = getDoublyListLength(pList);

if (position >= 0 && position < arrayCount) {

pPreNode = &(pList->headerNode);

for (i = 0; i < position; i++) {

pPreNode = pPreNode->pRLink;

}

pDelNode = pPreNode->pRLink;


pPreNode->pRLink = pDelNode->pRLink;

pDelNode->pRLink->pLLink = pPreNode;

free(pDelNode);


pList->currentElementCount--;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] removeDLElement() \n", position);

}

}

return ret;

}


DoublyListNode* getDLElement(DoublyList* pList, int position) {

DoublyListNode* pReturn = NULL;

int i = 0;

DoublyListNode* pNode = NULL;

if (pList != NULL) {

if (position >= 0 && position < pList->currentElementCount) {

pNode = pList->headerNode.pRLink;

for (i = 0; i < position; i++) {

pNode = pNode->pRLink;

}

pReturn = pNode;

}

else {

printf("오류, 위치 인덱스-[%d] getDLElement()\n", position);

}

}

return pReturn;

}


void displayDoublyList(DoublyList* pList) {

int i = 0;

if (pList != NULL) {

printf("현재 노드 개수: %d\n", pList->currentElementCount);


for (i = 0; i < pList->currentElementCount; i++) {

printf("[%d], %d\n", i, getDLElement(pList, i)->data);

}

}

else {

printf("원소가 없습니다.\n");

}

}


void deleteDoublyList(DoublyList* pList) {

if (pList != NULL) {

clearDoublyList(pList);

free(pList);

}

}


void clearDoublyList(DoublyList * pList) {

if (pList != NULL) {

while (pList->currentElementCount > 0) {

removeDLElement(pList, 0);

}

}

}


int getDoublyListLength(DoublyList* pList) {

int ret = 0;

if (pList != NULL) {

ret = pList->currentElementCount;

}


return ret;

}


(3) doublelist.h

#ifndef _DOUBLYLIST_

#define _DOUBLYLIST_


typedef struct DoublyListNodeType {

int data;

struct DoublyListNodeType* pLLink;

struct DoublyListNodeType* pRLink;

} DoublyListNode;


typedef struct DoublyListType {

int currentElementCount;

DoublyListNode headerNode;

} DoublyList;



DoublyList* createDoublyList();

int addDLElement(DoublyList* pList, int position, DoublyListNode element);

int removeDLElement(DoublyList* pList, int position);

DoublyListNode* getDLElement(DoublyList* pList, int position);

void displayDoublyList(DoublyList* pList);

void deleteDoublyList(DoublyList* pList);

void clearDoublyList(DoublyList * pList);

int getDoublyListLength(DoublyList* pList);

#endif



#ifndef _COMMON_LIST_DEF_

#define _COMMON_LIST_DEF_


#define TRUE 1

#define FALSE 0


#endif

'Programming > C' 카테고리의 다른 글

Sorting(1) Bubble Sort(버블 정렬)  (0) 2016.11.01
(6) CircularList 사용하기  (0) 2016.10.30
(4) Linkedlist 를 이용하여 Reverse, Concat 응용  (0) 2016.10.30
(3) Linkedlist 사용하기  (0) 2016.10.30
(2) ArrayStack 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,

Linkedlist를 이용하여 List에 대한 사용은 지난 포스팅http://jwprogramming.tistory.com/230 ) 에서 살펴보았습니다. 이제 Linkedlist를 이용하여 데이터를 reverse하고, Concat하는 부분을 해보았습니다.


(참고 : http://jwprogramming.tistory.com/230 )

아래 소스를 위해서는 지난 포스팅에서 진행했었던 linkedlist.h 가 추가로 필요합니다.!


(1) linkedlistopmain.c

#include <stdio.h>

#include <stdlib.h>

#include "linkedlist.h"

#include "linkedlistop.h"


int main(int argc, char* argv[]) {

int i = 0, arrayCount = 0;

LinkedList *pListA = NULL, *pListB = NULL;

ListNode node;

pListA = createLinkedList();

pListB = createLinkedList();

if (pListA != NULL && pListB != NULL) {

node.data = 1;

addLLElement(pListA, 0, node);

node.data = 2;

addLLElement(pListA, 1, node);

node.data = 3;

addLLElement(pListA, 2, node);

node.data = 4;

addLLElement(pListB, 0, node);

node.data = 5;

addLLElement(pListB, 1, node);

iterateLinkedList(pListA);

iterateLinkedList(pListB);

concatLinkedList(pListA, pListB);

printf("After concatLinkedList()\n");

iterateLinkedList(pListA);

iterateLinkedList(pListB);

reverseLinkedList(pListA);

printf("After reverseLinkedList()\n");

iterateLinkedList(pListA);

deleteLinkedList(pListA);

deleteLinkedList(pListB);

}

return 0;

}


(2) linkedlistop.c

#include <stdio.h>

#include <stdlib.h>

#include "linkedlist.h"

#include "linkedlistop.h"


void iterateLinkedList(LinkedList* pList) {

ListNode* pNode = NULL;

int count = 0;

if (pList != NULL) {

pNode = pList->headerNode.pLink;

while (pNode != NULL) {

printf("[%d], %d\n", count, pNode->data);

count++;


pNode = pNode->pLink;

}

printf("노드 개수 : %d\n", count);

}

else {

printf("공백 리스트입니다.\n");

}

}


void concatLinkedList(LinkedList* pListA, LinkedList* pListB) {

ListNode *pNodeA = NULL, *pNodeB = NULL;


if (pListA != NULL && pListB != NULL) {

pNodeA = pListA->headerNode.pLink;

while (pNodeA->pLink != NULL) {

pNodeA = pNodeA->pLink;

}

pNodeA->pLink = pListB->headerNode.pLink;

pListA->currentElementCount += pListB->currentElementCount;


pListB->headerNode.pLink = NULL;

pListB->currentElementCount = 0;

}

}


void reverseLinkedList(LinkedList* pList) {

ListNode *pNode = NULL, *pCurrentNode = NULL, *pPrevNode = NULL;


if (pList != NULL) {

pNode = pList->headerNode.pLink;

while (pNode != NULL) {

pPrevNode = pCurrentNode;

pCurrentNode = pNode;

pNode = pNode->pLink;

pCurrentNode->pLink = pPrevNode;

}

pList->headerNode.pLink = pCurrentNode;

}

}


(3) linkedlistop.h

#ifndef _LINKEDLIST_OP_

#define _LINKEDLIST_OP_


void iterateLinkedList(LinkedList* pList);

void concatLinkedList(LinkedList* pListA, LinkedList* pListB);

void reverseLinkedList(LinkedList* pList);


#endif

'Programming > C' 카테고리의 다른 글

(6) CircularList 사용하기  (0) 2016.10.30
(5) DoubleLinkedList 사용하기  (0) 2016.10.30
(3) Linkedlist 사용하기  (0) 2016.10.30
(2) ArrayStack 사용하기  (0) 2016.10.30
(1) ArrayList 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,

Linkedlist를 이용하여 List에 데이터를 넣었다가 빼고, 리스트에 존재하는지 등에 대한 Function들을 작성해보았습니다.

(비교, ArrayList 사용하기 : http://jwprogramming.tistory.com/228 )


(1) linkedlistmain.c

#include <stdio.h>

#include <stdlib.h>

#include "linkedlist.h"


int main(int argc, char* argv[]) {

int i = 0;

int arrayCount = 0;

LinkedList * pList = NULL;

ListNode * pNode = NULL;

ListNode node;

pList = createLinkedList();

if (pList != NULL) {

node.data = 1;

addLLElement(pList, 0, node);

node.data = 3;

addLLElement(pList, 1, node);

node.data = 5;

addLLElement(pList, 2, node);

displayLinkedList(pList);

removeLLElement(pList, 0);

displayLinkedList(pList);

deleteLinkedList(pList);

}

return 0;

}


(2) linkedlist.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "linkedlist.h"


LinkedList* createLinkedList() {

LinkedList *pReturn = NULL;

int i = 0;

pReturn = (LinkedList *)malloc(sizeof(LinkedList));

if (pReturn != NULL) {

memset(pReturn, 0, sizeof(LinkedList));

}

else {

printf("오류, 메모리할당 createLinkedList() \n");

return NULL;

}

return pReturn;

}

int addLLElement(LinkedList* pList, int position, ListNode element) {

int ret = FALSE;

int i = 0;

ListNode* pPreNode = NULL;

ListNode* pNewNode = NULL;

if (pList != NULL) {

if (position >= 0 && position <= pList->currentElementCount) {

pNewNode = (ListNode*)malloc(sizeof(ListNode));

if (pNewNode != NULL) {

*pNewNode = element;

pNewNode->pLink = NULL;

pPreNode = &(pList->headerNode);

for (i = 0; i < position; i++) {

pPreNode = pPreNode->pLink;

}

pNewNode->pLink = pPreNode->pLink;

pPreNode->pLink = pNewNode;

pList->currentElementCount++;

ret = TRUE;

}

else {

printf("오류, 메모리할당 addLLElement() \n");

return ret;

}

}

else {

printf("오류, 위치 인덱스-[%d], addLLElement() \n", position);

}

}

return ret;

}

int removeLLElement(LinkedList* pList, int position) {

int ret = FALSE;

int i = 0;

int arrayCount = 0;

ListNode* pNode = NULL;

ListNode* pDelNode = NULL;

if (pList != NULL) {

arrayCount = getLinkedListLength(pList);

if (position >= 0 && position < arrayCount) {

pNode = &(pList->headerNode);

for (i = 0; i < position; i++) {

pNode = pNode->pLink;

}

pDelNode = pNode->pLink;

pNode->pLink = pDelNode->pLink;

free(pDelNode);

pList->currentElementCount--;

ret = TRUE;

}

else {

printf("오류, 위치 인덱스-[%d] removeLLElement() \n", position);

}

}

return ret;

}

ListNode* getLLElement(LinkedList* pList, int position) {

ListNode* pReturn = NULL;

int i = 0;

ListNode* pNode = NULL;

if (pList != NULL) {

if (position >= 0 && position < pList->currentElementCount) {

pNode = &(pList->headerNode);

for (i = 0; i <= position; i++) {

pNode = pNode->pLink;

}

pReturn = pNode;

}

}

return pReturn;

}

void deleteLinkedList(LinkedList* pList) {

int i = 0;

if (pList != NULL) {

clearLinkedList(pList);

free(pList);

}

}

void clearLinkedList(LinkedList* pList) {

if (pList != NULL) {

if (pList->currentElementCount > 0) {

removeLLElement(pList, 0);

}

}

}

int getLinkedListLength(LinkedList* pList) {

int ret = 0;

if (pList != NULL) {

ret = pList->currentElementCount;

}

return ret;

}

int isEmpty(LinkedList* pList) {

int ret = FALSE;

if (pList != NULL) {

if (pList->currentElementCount == 0) {

ret = TRUE;

}

}

return ret;

}


void displayLinkedList(LinkedList* pList) {

int i = 0;

if (pList != NULL) {

printf("현재 원소 개수 : %d\n", pList->currentElementCount);

for (i = 0; i < pList->currentElementCount; i++) {

printf("[%d], %d\n", i, getLLElement(pList, i)->data);

}

}

else {

printf("원소가 없습니다.\n");

}

}


(3) linkedlist.h

#ifndef _LINKEDLIST_

#define _LINKEDLIST_


typedef struct ListNodeType

{

int data;

struct ListNodeType* pLink;

} ListNode;


typedef struct LinkedListType

{

int currentElementCount;

ListNode headerNode;

} LinkedList;


void displayLinkedList(LinkedList* pList);

LinkedList* createLinkedList();

int addLLElement(LinkedList* pList, int position, ListNode element);

int removeLLElement(LinkedList* pList, int position);

ListNode* getLLElement(LinkedList* pList, int position);

void deleteLinkedList(LinkedList* pList);

void clearLinkedList(LinkedList* pList);

int getLinkedListLength(LinkedList* pList);

int isEmpty(LinkedList* pList);

void displayLinkedList(LinkedList* pList);


#endif



#ifndef _COMMON_LIST_DEF_

#define _COMMON_LIST_DEF_


#define TRUE 1

#define FALSE 0


#endif

'Programming > C' 카테고리의 다른 글

(5) DoubleLinkedList 사용하기  (0) 2016.10.30
(4) Linkedlist 를 이용하여 Reverse, Concat 응용  (0) 2016.10.30
(2) ArrayStack 사용하기  (0) 2016.10.30
(1) ArrayList 사용하기  (0) 2016.10.30
C언어란?  (0) 2016.04.18

WRITTEN BY
SiriusJ

,

ArrayStack을 이용하여 Stack에 데이터를 넣었다가 빼고, 스택에 존재하는지, 현재 스택 상태 출력 등에 대한 Function들을 작성해보았습니다.


(1) arraystackmain.c

#include <stdio.h>

#include <stdlib.h>

#include "arraystack.h"


int main(int argc, char* argv[]) {

char value = 0;

ArrayStack *pStack = NULL;

ArrayStackNode *pNode = NULL;

pStack = createArrayStack(6);


if (pStack != NULL) {

ArrayStackNode node1 = { 'A' };

ArrayStackNode node2 = { 'B' };

ArrayStackNode node3 = { 'C' };

ArrayStackNode node4 = { 'D' };

pushAS(pStack, node1);

pushAS(pStack, node2);

pushAS(pStack, node3);

pushAS(pStack, node4);

displayArrayStack(pStack);

pNode = popAS(pStack);

if (pNode != NULL) {

printf("Pop 값-[%c]\n", pNode->data);

free(pNode);

}

else {

printf("공백(Empty) 스택\n");

}

displayArrayStack(pStack);

pNode = peekAS(pStack);

if (pNode != NULL) {

printf("Peek 값 - [%c]\n", pNode->data);

}

else {

printf("공백(Empty) 스택\n");

}

displayArrayStack(pStack);

}

return 0;

}


(2) arraystack.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "arraystack.h"


ArrayStack* createArrayStack(int size) {

ArrayStack *pReturn = NULL;

int i = 0;


if (size > 0) {

pReturn = (ArrayStack *)malloc(sizeof(ArrayStack));

if (pReturn != NULL) {

memset(pReturn, 0, sizeof(ArrayStack));

pReturn->maxElementCount = size;

pReturn->currentElementCount = 0;

}

else {

printf("오류, 메모리 할당, createArrayStack()\n");

return NULL;

}

}

else {

printf("오류, 스택의 크기는 0이상이어야 합니다.\n");

return NULL;

}


pReturn->pElement = (ArrayStackNode *)malloc(sizeof(ArrayStackNode)*size);


return pReturn;

}


int pushAS(ArrayStack* pStack, ArrayStackNode element) {

int i = 0;

int ret = FALSE;

if (pStack != NULL) {

if (isArrayStackFull(pStack) == FALSE) {

pStack->pElement[pStack->currentElementCount] = element;

pStack->currentElementCount++;

ret = TRUE;

}

else {

printf("오류, 스택이 가득 찼습니다, pushAS()\n");

}

}

return ret;

}


ArrayStackNode* popAS(ArrayStack* pStack) {

ArrayStackNode* pReturn = NULL;

if (pStack != NULL) {

if (isArrayStackEmpty(pStack) == FALSE) {

pReturn = (ArrayStackNode *)malloc(sizeof(ArrayStackNode));

if (pReturn != NULL) {

*pReturn = pStack->pElement[pStack->currentElementCount - 1];

pStack->currentElementCount--;

}

else {

printf("오류, 메모리할당, popAS()\n");

}

}

}

return pReturn;

}


ArrayStackNode* peekAS(ArrayStack* pStack) {

ArrayStackNode* pReturn = NULL;

if (pStack != NULL) {

if (isArrayStackEmpty(pStack) == FALSE) {

pReturn = &(pStack->pElement[pStack->currentElementCount - 1]);

}

}

return pReturn;

}


void deleteArrayStack(ArrayStack* pStack) {

if (pStack != NULL) {

if (pStack->pElement != NULL) {

free(pStack->pElement);

}

free(pStack);

}

}


int isArrayStackFull(ArrayStack* pStack) {

int ret = FALSE;


if (pStack != NULL) {

if (pStack->currentElementCount == pStack->maxElementCount) {

ret = TRUE;

}

}

return ret;

}


int isArrayStackEmpty(ArrayStack* pStack) {

int ret = FALSE;


if (pStack != NULL) {

if (pStack->currentElementCount == 0) {

ret = TRUE;

}

}


return ret;

}


void displayArrayStack(ArrayStack *pStack) {

int i = 0;

if (pStack != NULL) {

int size = pStack->maxElementCount;

int top = pStack->currentElementCount;

printf("스택 크기: %d, 현재 노드 개수: %d\n", pStack->maxElementCount, pStack->currentElementCount);

for (i = size - 1; i >= top; i--) {

printf("[%d]-[Empty]\n", i);

}

for (i = top - 1; i >= 0; i--) {

printf("[%d]-[%c]\n", i, pStack->pElement[i].data);

}

}

}


(3) arraystack.h

#ifndef _ARRAY_STACK_

#define _ARRAY_STACK_


typedef struct ArrayStackNodeType {

char data;

} ArrayStackNode;


typedef struct ArrayStackType {

int maxElementCount;

int currentElementCount;

ArrayStackNode *pElement;

} ArrayStack;


ArrayStack* createArrayStack(int size);

int pushAS(ArrayStack* pStack, ArrayStackNode element);

ArrayStackNode* popAS(ArrayStack* pStack);

ArrayStackNode* peekAS(ArrayStack* pStack);

void deleteArrayStack(ArrayStack* pStack);

int isArrayStackFull(ArrayStack* pStack);

int isArrayStackEmpty(ArrayStack* pStack);

void displayArrayStack(ArrayStack *pStack);


#endif


#ifndef _COMMON_STACK_DEF_

#define _COMMON_STACK_DEF_


#define TRUE 1

#define FALSE 0


#endif

'Programming > C' 카테고리의 다른 글

(5) DoubleLinkedList 사용하기  (0) 2016.10.30
(4) Linkedlist 를 이용하여 Reverse, Concat 응용  (0) 2016.10.30
(3) Linkedlist 사용하기  (0) 2016.10.30
(1) ArrayList 사용하기  (0) 2016.10.30
C언어란?  (0) 2016.04.18

WRITTEN BY
SiriusJ

,