C++ 双链表

#include <stdio.h>
#include <iostream>
#include <easyx.h>
#include <graphics.h>
#include <list>
//声明双链表节点
struct Node
{
    int x;
    int y;
    Node* next;                                                //定义一个结构体指针指向当前节点的下一个节点;
    Node* prev;                                                //定义一个结构体指针指向当前节点的上一个节点;
};

//声明存放双链表指针结构体
struct Linklist                                                //双链表结构体;
{
    Node* head;                                                //定义一个头节点指针;
    Node* tail;                                                //尾节点定义一个子节点指针;
    int size;                                                //定义变量来保存双链表中节点的数量;
};

//1.初始化一个双链表节点;
Node* createNode(int x, int y)                                //创建双链表子节点;
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->x = x;                                            //将x赋值给结构体的x;
    temp->y = y;                                            //将y赋值给结构体的y    ;        
    temp->next = NULL;                                        //将前后节点都指向空地址;
    temp->prev = NULL;
    return temp;                                            //将创建好的结构体返回;
}

//2.链接2个节点; 
void node_link(Node*  n1, Node* n2)
{
    n1->next = n2;                                            //将n1的下一个节点指向n2
    n2->prev = n1;                                            //将n2的上一个节点指向n1

}

//3.初始化空的双链表; 
void  Linklist_init(Linklist *list)
{
    list->head = NULL;                                    //头指针为空;
    list->tail = NULL;                                    //尾指针也为空;
    list->size = 0;                                        //定义变量来保存节点中的节点的数量;
}

void push_head(Linklist* list, int x, int y)
{
    if (list->size == 0)//判断双链表的头指针是否为空,则表示当前新建的是第一个节点
    {
        list->head = createNode(x, y);
        list->tail = list->head; //由于创建的是第一个节点,那么头节点和一个节点都是空;
        list->size = 1;        //双链表存放的节点大小变为1;
    }
    else
    {
        Node* temp = createNode(x, y);  //创建一个双链表节点
        node_link(temp, list->head);  //将双链表链接 由于是头部挂载,所以传参时节点在前,list列表在后
        list->head = temp;   //head的头节点变为temp
        list->size++;        //双链表节点数量+1;
    }
}

void  push_end(Linklist* list, int x, int y)
{
    if (list->size == 0)
    {
        list->head = createNode(x, y);
        list->tail = list->head; //由于创建的是第一个节点,那么头节点和一个节点都是空;
        list->size = 1;        //双链表存放的节点大小变为1;
    }
    else
    {
        Node*  temp = createNode(x, y);
        node_link(list->tail, temp); //将双链表链接 由于是尾部挂载,所以传参时节点在后,list在前
        list->tail = temp;  //原链表尾节点变为temp
        list->size++;       //双链表节点数量+1;
    }

}

//头部开始打印双链表
Linklist* print_list_head(Linklist* list)
{
    if (list->head == NULL) return list;
    Node* temp=list->head;         //申请一个指针指向头节点
    while (temp != NULL) //判断当前节点是否为空,否则退出循环
    {
        printf("%d %d->", temp->x, temp->y);
        temp = temp->next;  //将temp指针指向下一个节点
    }
    printf("NULL\n");
}

void test(Linklist* list)
{

    while (1)
    {
        for (Node* temp = list->head;temp!=NULL; temp = temp->next)
        {
          
            
                printf("%d,%d->", temp->x, temp->y);
               
        }
        
        for (Node* temp = list->tail; temp != NULL; temp = temp->prev)
        {
            printf("%d,%d->", temp->x, temp->y);
       
        }
                
        Sleep(1000);
      
    }      
 }



//从尾部向前打印双链表
Linklist* print_list_end(Linklist* list)
{
    if (list->head == NULL) return list;   //如果头节点为空,则表示双链表为空
    Node* temp = list->tail;  //定义一个节点指针指向尾结点的指针 
    while (temp != NULL)    //判断当前节点是否为空
    {
         printf("%d %d->", temp->x, temp->y); 
         temp = temp->prev; //将指针指向temp的上一个节点
    };
    printf("NULL\n");
}

Linklist*  free_someone(Linklist* list, int x, int y)
{


    return list;


}


int main()
{
    Linklist* list1 = (Linklist*)malloc(sizeof(Linklist));   //动态申请一个双链表;
    Linklist_init(list1);
    
    for (int i = 1; i < 10; i++)
    {      
       push_head(list1, i , i);

    }
    test(list1);



    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shenji/p/12526427.html