链表

《Data Structures and Algorithm Analysis in C》(数据结构与算法分析 C语言描述)

目录

        • 单链表
        • 双链表,循环链表

单链表

#include 
#include 
#include 
using namespace std;

/*
    带头节点header的链表
    header --> first_node --> ...
*/
typedef int element_type;
struct Node
{
    element_type element;
    Node *next;
};
typedef Node *node_ptr;
typedef node_ptr List;
typedef node_ptr Position;

int is_empty_(List H)
{
    return H->next == NULL;
}
int is_last(Position P)
{
    return P->next == NULL;
}
void print_all(List H)
{
    Position t = H->next;
    int cnt = 1;
    while (t != NULL)
    {
        cout << "node " << cnt++ << ": " << t->element << endl;
        t = t->next;
    }
}

Position find_x(element_type x, List H)
{
    Position t = H->next;
    while (t != NULL && t->element != x)
    {
        t = t->next;
    }
    return t;
}
Position find_previous(element_type x, List H)
{
    Position p = H;
    while(p->next != NULL && p->next->element != x)
        p = p->next;
    return p;
}
void delete_fir_x(element_type x, List H)
{
    Position p = find_previous(x, H);  // 等于x的前一个位置
    // p --> to_del --> q
    if(!is_last(p))
    {
        // p->next = p->next->next;

        Position temp = p->next;  // x所在位置
        p->next = temp->next;     // p重新指向x下一位置
        free(temp);
    }
}
void insert_aft_p(element_type x, List H, Position p)
{
    // p --> (to_ins -->) q
    List to_ins = (List)malloc(sizeof(Node));
    to_ins->element = x;

    to_ins->next = p->next;  
    p->next = to_ins;       // 先此步则找不到q
}
void delete_list(List H)
{
    Position next, p = H->next;  // 先暂时记录其指向的位置
    H->next = NULL;        // 后破坏指针(不再指向)
    while(p != NULL)
    {
        next = p->next;
        free(p);
        p = next;
    }
}

void append_x(element_type x, List H)
{
    Position p = H;
    while(!is_last(p))
        p = p->next;
    List last = (List)malloc(sizeof(Node));
    last->element = x;
    last->next = NULL;
    p->next = last;
}
void push_x(element_type x, List H)
{
    insert_aft_p(x, H, H);
}
void test()
{
    List header = (List)malloc(sizeof(Node));
    header->next = NULL;
    cout << is_empty_(header) << endl;

    List first = (List)malloc(sizeof(Node));
    first->element = 1;
    first->next = NULL;

    Node second;
    second.element = 2;
    second.next = NULL;
    
    List third = (List)malloc(sizeof(Node));
    third->element = 4;
    third->next = NULL;

    header->next = first;
    first->next = &second;
    second.next = third;

    cout << "find x=2: ";
    Position p = find_x(2, header);
    if (p != NULL)
    {
        cout << p->element << endl;   
    }

    print_all(header);printf("---\n");
    delete_fir_x(2, header);
    print_all(header);printf("---\n");
    insert_aft_p(3, header, find_x(4, header));
    print_all(header);printf("---\n");
    delete_list(header);
    cout << is_empty_(header) << endl;
    append_x(2, header);
    append_x(1, header);
    push_x(5, header);
    print_all(header);
}
int main()
{
    test();
    return 0;
}

双链表,循环链表

#include 
#include 
#include 
using namespace std;

/*
    双链表,循环链表
*/
typedef int element_type;
struct Node
{
    element_type element;
    Node *next;
    Node *prev;
};
typedef Node *node_ptr;
typedef node_ptr List;
typedef node_ptr Position;

void travers_backward(Position last)
{
    Position p = last;
    while (p != NULL)
    {   
        cout << "node: " << p->element << endl;
        p = p->prev;
    }
}
void delete_node(List del, Position first)
{
    if(first == NULL || del == NULL)
        return;
    if(first == del)
    {
        first = del->next;  // 改变首节点指针
    }
    // 简化了删除
    if(del->next != NULL)
        del->next->prev = del->prev;
    if(del->prev != NULL)
        del->prev->next = del->next;
    free(del);
}

void travers_any(Position any)
{
    if(any == NULL)
        return;
    cout << "node: " << any->element << endl;
    Position t = any->next;
    while(t != any)
    {
        cout << "node: " << t->element << endl;
        t = t->next;
    }
}
void test_dll()
{
    List first = (List)malloc(sizeof(Node));
    first->element = 1;
    first->next = first->prev = NULL;

    List second = (List)malloc(sizeof(Node));
    second->element = 2;
    second->next = NULL;
    second->prev = first;
    first->next = second;
    
    List third = (List)malloc(sizeof(Node));
    third->element = 3;
    third->next = NULL;
    third->prev = second;
    second->next = third;

    travers_backward(third);
    cout << "---" << endl;
    delete_node(first, first);
    travers_backward(third);
    cout << "---" << endl;
   
}
void test_cll()
{
    List first = (List)malloc(sizeof(Node));
    first->element = 1;
    first->next = first->prev = NULL;

    List second = (List)malloc(sizeof(Node));
    second->element = 2;
    second->next = NULL;
    second->prev = first;
    first->next = second;
    
    List third = (List)malloc(sizeof(Node));
    third->element = 3;
    third->prev = second;
    second->next = third;

    third->next = first;
    first->prev = third;

    travers_any(second);
}
int main()
{
    // test_dll();
    test_cll();
    return 0;
}

你可能感兴趣的:(#,数据结构,Algorithm,数据结构)