XOR Linked List – A Memory Efficient Doubly Linked List 프로그래밍 기초

참고 : http://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-2/

프로그램을 먼저 작성하고 이게 맞는지 인터넷에서 찾아봤는데...

쓸때없는 부분도 많았고 게다가 링크 코드를 보면 내가 생각지도 못한 head에 노드를 삽입하는 방식으로

더 깔끔한 코드가 되었다.

아래는 내가 처음 작성한 코드


#include <stdio.h>

#include <stdlib.h>



typedef int element;

typedef struct linkedNode

{

    element data;

    struct linkedNode* xorPrNe;

} linkedNode;


typedef struct xordll

{

    linkedNode* head;

}xordll;


xordll* createDLL()

{

    xordll* newxor = (xordll*)malloc(sizeof(xordll));

    newxor->head = NULL;

    return newxor;

}


linkedNode* getXOR(linkedNode* a, linkedNode* b)

{

    return (linkedNode*)(((unsigned long)a) ^ ((unsigned long)b));

}


void insertNodeElement(xordll* xordll, element data)

{

    linkedNode* newNode = (linkedNode*)malloc(sizeof(linkedNode));

    linkedNode* prev = NULL;

    linkedNode* next = NULL;

    newNode->data = data;

    int cnt = 0;

    

    linkedNode* target;

    // 처음

    if (xordll->head == NULL) {

        newNode->xorPrNe = getXOR(prev, next);

        xordll->head = newNode;

    }

    else

    {

        target = xordll->head;

        while (target) {

            cnt++;

            //첫번째 노드의 경우

            if (cnt == 1) {

                prev = NULL;

                next = target->xorPrNe;

            }

            else

            {

                next = getXOR(target->xorPrNe, prev);

                prev = getXOR(target->xorPrNe, next);

            }

            prev = target;

            target = next;

        }

        target = prev;

        prev = getXOR(target->xorPrNe, NULL);

        newNode->xorPrNe = getXOR(target, NULL);

        next = newNode;

        target->xorPrNe = getXOR(prev, next);

    }

}


void printElement(xordll* dll)

{

    if (dll->head == NULL) {

        printf("\n List is empty");

        return;

    }

    

    int cnt = 0;

    linkedNode* target = dll->head;

    linkedNode *prev = NULL, *next = NULL;

    

    while(target) {

        cnt++;

        // 노드의 경우

        if (cnt == 1) {

            prev = NULL;

            next = target->xorPrNe;

        }

        else

        {

            next = getXOR(target->xorPrNe, prev);

            prev = getXOR(target->xorPrNe, next);

        }

        

        printf("\n %d ", target->data);

        prev = target;

        target = next;

    }

    

}


int main()

{

    

    xordll* newdll = createDLL();

    printf("\ninsert data ....");

    insertNodeElement(newdll, 1);

    insertNodeElement(newdll, 2);

    insertNodeElement(newdll, 3);

    insertNodeElement(newdll, 4);

    

    printElement(newdll);

    

}


malloc error 프로그래밍 기초

흠 말도 안돼는 상황에 빠져서 허우적대고 있네.

그래프 깊이 탐색 코딩중에 발생한 문제

typedef struct graphNode {

    int vertex;

    struct graphNode *link;

} graphNode;



void insertEdge(graphType *g, int u, int v)

{

    graphNode* node;

    if (u>=g->n || v>=g->n)

    {

        printf("\n 그래프에 없는 정점입니다.");

        return;

    }

    

    node = (graphNode*) malloc(sizeof(graphNode*));   <----------------여기서 에러남 ㅠㅠ

    node->vertex = v;

    node->link = g->adjList_H[u];

    g->adjList_H[u] = node;

}


하~

이부분

graphNode로 sizeof 했었어야 했는데 graphNode* 로 sizeof해버렸네

node = (graphNode*) malloc(sizeof(graphNode*));


창피하라고 남김.ㅎㅎ



c 진수표현 프로그래밍 기초

코드를 보다보면 가끔씩 가물가물할 때가 있어서 남겨놓았다.

8진수
숫자 앞에 0을 붙인다.

ex) 0144

16진수
숫자 앞에 0x를 붙인다.

ex) 0x64


참고로 위 두개를 10진수로 변경하면 100 이다.

C 함수의 구조 프로그래밍 기초

C 언어에서 작은 부분에서 놓치는 것들이 많이있다.
그중 하나로 아래 코드와 같은 표현이다.

int add(x,y)

int x,y;

{

    int z = 0;

    z = x + y;

    return z;

}


보통은 이렇게


int add(int x,int y)

{

    int z = 0;

    z = x + y;

    return z;

}



파라미터(인수) 부분을 위와같이 자료형을 아래에 기술할 수 있다. 

이렇게도 할 수 있다 라는 것 정도 밖에는 아직 잘 모르겠다.


역추적 알고리즘 (모든 이진 문자열 생성)

char *A;

void binary(int n) {

    if (n<1) {

        printf("%s\n", A);

    }else {

        A[n-1] = '0';

        binary(n-1);

        A[n-1] = '1';

        binary(n-1);

    }

}


int main() {

    int n = 3;

    A = new char[n];

    binary(n);

}



역시 재귀 알고리즘이라서 간단하면서도 알쏭달쏭합니다.


1 2