본문 바로가기
DataStructure

이중 연결 리스트 -Double Linked List(2) (삭제, 삽입, 생성,추가) - C 언어

by neohtux 2019. 6. 3.
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Node* CreateNode(int NewData)
{
    Node* NewNode = (Node*)malloc(sizeof(Node));
 
    NewNode->Data = NewData;
    NewNode->Next = NULL;
    NewNode->Pre = NULL;
 
    return NewNode;
}
 
Node* DeleteNode(Node *Node)
{
    delete(Node);
}
 
Node* AppendNode(Node** Head, Node* NewNode)
{
    if (*Head == NULL)
    {
        *Head = NewNode;
    }
    else
    {
        //테일을 찾아서 Newnode를 연결
        Node *Tail = *Head;
        while (Tail->Next != NULL)
        {
            Tail = Tail->Next;
        }
 
        Tail->Next = NewNode;
        NewNode->Pre = Tail; //기존 테일을 새로운 테일의 이전을 가리킴.
    }
}
Node* GetNodeAt(Node** Head, int Location)
{
    Node* Current = Head;
    while (Current != NULL && (--Location) >= 0)
    {
        Current = Current->Next;
    }
 
    return Current;
}
Node* RemoveNode(Node** Head, Node* Remove)
{
    if (*Head == Remove) //제거할 노드가 헤드노드인경우
    {
        *Head = Remove->Next; // 삭제할 노드가 가리키던 포인터를 가리키게 바꾸고
        if (*Head != NULL)
        {
            (*Head)->Pre = NULL;
        }
        Remove->Pre = NULL;
        Remove->Next = NULL//삭제할 노드는 아무것도 가리켜선 안됨.
    }
    else
    {
        Node* Temp = Remove;
        Remove->Pre->Next = Temp->Next; 
 
        if (Remove->Next != NULL)
        {
            Remove->Next->Pre = Temp->Pre; 
        }
        Remove->Pre = NULL;
        Remove->Next = NULL;
    }
}
 
void InsertNode(Node* Current, Node* NewNode)
{
    NewNode->Next = Current->Next; //새로 추가되는 노드가 현재의 다음 링크를 가리키게함
    NewNode->Pre = Current;
    
    if (Current->Next != NULL)
    {
        Current->Next->Pre = NewNode; //다음의 이전링크에 새노드를 연결
    }
    Current->Next = NewNode;
}
 
 
300x250

댓글