C ++ doubly linked list to practice, to achieve the ball rolling on the screen, pit row

#include <stdio.h> 
#include <stdlib.h> 
#include <easyx.h>
 // define a doubly-linked list node 
struct the Node 
{ 
    int X; // data 
    int Y;
     int Z; 
    the Node * Next; // next data 
    the node * last; // the data 
}; 

// define storage node pointer list 
struct List 
{ 
    int size; // storage node size of the node 
    the node * head; // pointer to the head node 
    the node * tail; // pointer tail node points 

}; 

// initialize the linked list node bis
Node* init_node(int x,int y)
{
    Node* temp = (Node*)malloc(sizeof(Node));
    temp->x = x;
    temp->y = y;
    temp->z = 1;
    temp->next = temp->last = NULL;
    return temp;
}

//初始化双链表
List* init_list()
{
    List* temp = (List*)malloc(sizeof(List));
    temp->head = NULL;
    temp-> tail = NULL; 
    TEMP -> size = 0 ;
     return TEMP; 
} 

// link parameter passing node, a first front parameter data, second parameter data in the 
void   link_node (the Node N1 *, * the Node N2 ) 
{ 
    N1 -> Next = N2; 
    N2 -> Last = N1; 
} 


// double linked list 
List push_head * (* List List, the Node * N1) 
{ 
    IF (list-> size == 0 ) 
    { 
        List -> head N1 =;     // dual linked list to add the first node, then only one element in the doubly linked list, list head and tail pointers point to a linked list, 
        list-> tail = list-> head;
        List -> size = . 1 ; 
    } 
    the else 
    { 
        link_node (N1, List -> head); 
        List -> head = N1; 
        List -> size ++ ; 
    } 
    return List; 
} 

// scratch doubly linked 
void list_head_printf (List * List) 
{ 
    IF (list-> head == NULL) 
    { 
        the printf ( " doubly linked list is empty \ n-! " );
         return ; 
    } 
    the else 
    { 
        the printf ( "Doubly linked list output starts from the head output: " ); 

        for (the Node * TEMP = list-> head; NULL = TEMP; TEMP = temp->! Next) 
        { 
            the printf ( " % D " , temp-> X);
             IF (TEMP == list-> tail) 
                the printf ( " \ n- " );
             the else 
                the printf ( "  " ); 
        } 
    } 
} 


void push_end (* Data List, the Node * temp1) // double insertion end of the list 
{ 
    List * TEMP = Data ;
     IF (temp-> == headNULL) 
    {
        TEMP -> head = temp1; 
        TEMP -> tail = temp-> head; 
        TEMP -> size = . 1 ;
         return ; 
    } 
    the else 
    { 
        link_node (TEMP -> tail, temp1); 
        temp1 -> Last = temp-> tail ;   // the precursor temp1 hanging on the tail node, 
        temp-> tail = temp1;         // value now points to the tail node temp1 
        temp-> size ++ ;
         return ; 
    } 

} 


void del_node (List List *, int NUM)   // doubly-linked To delete a location node mass participation: 1. doubly linked list node, 2, location to be deleted
 { 
    the Node * TT = (the Node *) the malloc ( the sizeof (the Node)); // the dynamic pointer is used to apply a structure free objects 
    IF (list-> head == NULL) // If the list is empty bis directly returns 
    { 
        the printf ( " doubly linked list is empty \ n-! " );
         return ; 
    } 
    IF (list-> size <NUM) // and determining whether the position look space size range 
    { 
        the printf ( " double linked list does not exist that position \ n-! " );
         return ; 
    } 
    iF (list-> == size . 1 ) //Remove node is determined doubly linked list, a doubly linked list node only
     {
        TT = list-> head;
         Free (TT); 
        List -> head = list-> tail = NULL; 
        List -> size = 0 ;
         return ; 
    } 
    IF (list-> size == 2 ) // Analyzing deleted doubly-linked node, only two doubly linked list node 
    { 
        the node * TEMP = (the node *) the malloc ( the sizeof (the node)); 
        TEMP = list-> tail;
         Free (TEMP); 
        List -> head-> Next = NULL; 
        List - > = tail list-> head; 
        List -> size-- ;
         return ; 
    } 
    IF (list-> size> 2 ) // is determined that deleting the doubly linked list node, only two or more doubly linked list nodes 
    {
         int n-= . 1 ;
         for (the node * TEMP = list-> head;! temp-> Next = NULL; TEMP = temp-> Next) // loop through the doubly linked list node 
        {
             IF (== n-NUM)   // find the need to remove the current node 
            {
                 IF (temp-> Next == NULL) // determines whether the node to be removed in the final position, 
                { 
                    TT = TEMP; 
                    TEMP= temp->last;
                    list->tail = temp;
                    free(tt);
                    list->size--;
                    return;
                }
                else
                {
                    tt = temp;
                    link_node(temp->last, temp->next);
                    free(tt);
                    list->size--;
                    return;
                }
            }
            n++;
        }
    }
}

int main()
{
    initgraph(600, 600);

    List* list = init_list();

    for (int i = 1; i <= 10; i++)
    {
        push_end(list, init_node(i * 50, i * 50));
    }
    
    int a = 1;

    while (1)
    {
        cleardevice();
        BeginBatchDraw();
        for (Node* temp=list->head; temp!=NULL;temp=temp->next)
        {    
        
                for (Node* temp = list->head; temp != NULL; temp = temp->next)
                {
                    if (temp->z == 1)
                    {
                        circle(temp->x, temp->y++, 20);
                        if (temp->y + 33 >= 600)
                            temp->z = 0;
                    }
                    if (temp->z == 0)
                    {
                        circle(temp->x, temp->y--, 20);
                        if (temp->y - 33 <= 30)
                            temp->z = 1;
                    }
                }
                    
            }
        EndBatchDraw();
        Sleep(20);
        }
        
        
    
    closegraph();
    return 0;
    
}

 

Current problem encountered, when drawing the graphical interface, using a local variable is a variable defined as 0 and 1, the judgment value y doubly linked list structure members with the ball if statement to coordinate movement, but will appear at the interface of the ball when not in neat scrolling, but this variable is defined in the doubly linked list node membership, then the ball will be in the manner set neatly scrolled on the screen, currently an unknown reason.

 

The method can reach a function realization, but unable to reach a second method functions to achieve

//方法一:
while (1)
    {
        cleardevice();
        BeginBatchDraw();
        for (Node* temp=list->head; temp!=NULL;temp=temp->next)
        {    
        
                for (Node* temp = list->head; temp != NULL; temp = temp->next)
                {
                    if (temp->z == 1)
                    {
                        circle(temp->x, temp->y++, 20);
                        if (temp->y + 33 >= 600)
                            temp->z = 0;
                    }
                    if (temp->z == 0)
                    {
                        circle(temp->x, temp->y--, 20);
                        if (temp->y - 33 <= 30)
                            temp->z = 1;
                    }
                }
                    
            }
        EndBatchDraw();
        Sleep(20);
        }

//方法二:
    int a = 1;
    while (1)
    {
        cleardevice();
        BeginBatchDraw();
        for (Node* temp=list->head; temp!=NULL;temp=temp->next)
        {    
        
                for (Node* temp = list->head; temp != NULL; temp = temp->next)
                {
                    if (a == 1)
                    {
                        circle(temp->x, temp->y++, 20);
                        if (temp->y + 33 >= 600)
                            a = 0;
                    }
                    if (a == 0)
                    {
                        circle(temp->x, temp->y--, 20);
                        if (temp->y - 33 <= 30)
                            a = 1;
                    }
                }
                    
            }
        EndBatchDraw();
        Sleep(20);
        }

 

Now found problems need to determine the change in y-axis coordinates of each of the ball, then the time required for the doubly linked list is defined in a double linked list identified members, each time when the change in y-axis coordinates of the ball occurs, i.e. corresponding temp -> to determine whether the value of y in FIG graphical interface coordinate range,

So as to implement each ball can move freely in the interface, if you want a doubly-linked list externally defined variables, will cause the control temp-> interface coordinate value of y is not being set to not moved by the ball.

Problems plagued many days, when you find the problem and resolve, only to find the principles so simple ..

Guess you like

Origin www.cnblogs.com/shenji/p/12563276.html