7.19.2017

자료구조 Node로 표현하기

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


typedef struct Node {
  int data;
        struct Node * next;

}Node ;
//노드의 생성/소멸
//노드 추가
//노드 탐색
//노드 삽입

Node * CreateNode(int NewData) {
        Node *NewNode = (Node *)malloc(sizeof(Node)); //현재 생성한 노드 메모리 할당
        NewNode->data = NewData//현재 생성한 노드에 data 대입
        NewNode->next = NULL//현재 생성한 노드의 next가 NULL을 참조
        return NewNode; //현재 생성한 노드의 주소값 반환
}

void DestroyNode(NodeNode) {
        free(Node);
}

void AppendNode(Node** HeadNodeNewNode) {
       //헤드노드가 NULL이라면 새로운 노드가 Head
        if ((*Head) == NULL) {
                *Head NewNode;
        }
        else {
                //tail을 찾아 NewNode를 연결함.
                Node*tail = (*Head);
                while (tail->next != NULL) {
                        tail = tail->next;
                }tail->next = NewNode;
        }
}

void InsertAfter(NodeCurrentNodeNewNode) {
        NewNode->next = Current->next;
        Current->next = NewNode;
}

void InsertNewHead(Node** HeadNodeNewHead) {
        if ((*Head) == NULL) {
                *Head NewHead;
        }
        else {
                NewHead->next = *Head;
                *HeadNewHead;
        }
}

void RemoveNode(Node** Head, Node* Remove) {
if (*Head == Remove) {
*Head = Remove->next;
}
else {
Node* Current = *Head;
while (Current != NULL && Current->next != Remove) {
Current = Current->next;
}
if (Current != NULL) {
Current->next = Remove->next;
}
}
}


Node* SequentialSearch(NodeHeadint Target) {
        Node* Current = Head;
        while (Current!=NULL && Current->data != Target) {
                Current = Current->next;
        } return Current;
}

//1. 전진이동법
Node* MoveToFront(Node** Headint Target) {
        Node* Current = *Head;
        Node* Match = NULL;
        Node* Previous = NULL;

        while (Current != NULL) {
                if (Current->data == Target) {
                        Match = Current;
                        if (Previous != NULL) {
                                //Current의 앞 노드와 다음 노드 연결
                                Previous->next = Current->next;

                                //Current를 리스트의 헤드로 옮기기
                                Current->next = (*Head);
                                (*Head) = Current;
                        }break;
                }
                else {
                        PPrevious = Previous;
                        Previous = Current;
                        Current = Current->next;
                }
        } return Match;
}

//2. 전위법
Node* TransPose(Node** Headint Target) {
        Node* Current = *Head;
        Node* Match = NULL;
        Node* Previous = NULL//이전 노드
        Node* PPrevious = NULL//이전 노드의 이전 노드


        while (Current != NULL) {
                if (Current->data == Target) {
                        Match = Current;
                        if (Previous != NULL) {
                                if (PPrevious != NULL)
                                        PPrevious->next = Current;
                                else (*Head) = Current;

                                Previous->next = Current->next;
                                Current->next = Previous;
                        }break;
                }
                else {
                        PPrevious = Previous;
                        Previous = Current;
                        Current = Current->next;
                }

        } return Match;
}

//3. 빈도 계수법

void main() {
        Node* List = NULL;
        Node* Match = NULL;
        Node* Current = NULL;
        int Count = 0;
        for (int i = 0; i < 5; i++) {
                NewNode = CreateNode(2 * i + 1);
                AppendNode(&List, NewNode);
        }
        Count = GetNodeCount(List);
        for (int i = 0; i < Count; i++) {
                Current = GetNodeAt(List, i);
                printf("List[%d] : %d\n", i, Current->data);
        }
        printf("After MoveToFront... \n\n");
        Current = MoveToFront(&List, 7);
        Count = GetNodeCount(List);
        for (int i = 0; i < Count; i++) {
                Current = GetNodeAt(List, i);
                printf("List[%d] : %d\n", i, Current->data);
        }
}

댓글 없음:

댓글 쓰기