Using fast and slow pointer to access the data of middle node of (bidirectional) linked list (bidirectional linked list with head node)

Keywords: Linux

This paper mainly introduces the method of using fast and slow pointers to access the intermediate nodes. As for other operations of the linked list, it has been written before, which will not be introduced here

#include<stdio.h>
#include<stdlib.h>
typedef struct student{
    int score;
    char name[50];
}student_t;

typedef struct listnode{
    student_t data;
    struct listnode*next;
    struct listnode*prev;
}student_node_t,*student_list_t;
void InputData(student_node_t*head);//Passing in data to the linked list
void PrintMidData(student_node_t* head,student_node_t* temp);//Output data of intermediate nodes
void PrintAllInfo(student_node_t* head);//Output all data information
int main(){
    student_node_t*head,* temp;
    head=(student_list_t)malloc(sizeof(student_node_t));
    head->next=head->prev=head;
    char ch;
    do{
        printf("\n-----------------------------------------------------------------------------------\n");
        printf("\n");
        printf("\t\t\t[A].Add data");
        printf("\n\n\t\t\t[P].Show student data");//Print in the order of initial input by default
        printf("\n\n\t\t\t[M].Output data of intermediate node");
        printf("\n\n\t\t\t[E].Sign out");
        printf("\n\n-----------------------------------------------------------------------------------");
        printf("\n\n Please enter options:");
        ch=getchar();
        switch(ch){
            case 'a':
            case 'A':
                InputData(head);//Save data in linked list first
                break;
            case 'p':
            case 'P':
                PrintAllInfo(head);
                while('\n'!=getchar());
                break;
            case 'M':
            case 'm':
                PrintMidData(head,temp);
                while('\n'!=getchar());
                break;
        }
    }while(ch!='e'&&ch!='E');
    return 0;
}


void PrintMidData(student_node_t* head,student_node_t* temp){//Query intermediate node data with fast and slow pointer
    student_node_t* fast;
    temp=(student_node_t*)malloc(sizeof(student_node_t));
    temp=head->next;//Place slow pointer at second node
    fast=temp->next;//Place the fast pointer at the next node of the slow pointer
    int flag;
    while(1){
        if(fast==head){
            flag=0;//If the fast pointer can finally point to the head node, it indicates that there are odd data nodes
            break;
        }
        if(fast==head->prev){
            flag==1;
            break;
        }//If the fast pointer can finally point to the tail node, there are even data nodes
        temp=temp->next;
        fast=fast->next->next;
    }
    if(flag==0){
            printf("The data of intermediate students are as follows:\n\n");
            printf("\n------------------------------------------------------------\n");
            printf("\n\t\t%-20s%-20s\n","Name","Score");
            printf("\n\n\t\t%-20s%-20d\n",temp->data.name,temp->data.score);
    }
    else{
            printf("The data of two students in the middle node are as follows:\n\n");
            printf("---------------------------------------------------------------\n");
            printf("\n\t\t%-20s%-20s","Name","Score");
            printf("\n\n\t\t%-20s%-20d",temp->data.name,temp->data.score);
            printf("\n\n\t\t%-20s%-20d",temp->next->data.name,temp->next->data.score);
            }
}
void InputData(student_node_t* head){//Input the relevant data of students, and use the tail interpolation method    
    student_node_t* temp=NULL;
    char choice;
    while(1){   
        temp=(student_list_t)malloc(sizeof(student_node_t));
        printf("\nInput score:");
        scanf("%d",&temp->data.score);
        while('\n'!=getchar());
        printf("\nInput name:");
        scanf("%[^\n]",temp->data.name);
        temp->next=head;
        head->prev->next=temp;
        temp->prev=head->prev;
        head->prev=temp;
        printf("\n[C].Continue         [E].Exit\n\n");
        while('\n'!=getchar());//It's not clear how to empty the buffer in Linux
        printf("\n\nInput you choice:");
        choice=getchar();
        if(choice=='E'||choice=='e')break;
    }
    while('\n'!=getchar());
}
void PrintAllInfo(student_node_t*head){//Show all data
    if(head->next==head){
        printf("\n Data not saved yet!\n");
        return ;
    }
    student_node_t *temp;
    temp=head->next;
    printf("\n\nHere is all student information:\n\n");
    printf("-------------------------------------------------------\n\n");
    printf("\n\t\t%-20s%-20s","Name","Score");
    while(temp!=head){
        printf("\n\n\t\t%-20s",temp->data.name);
        printf("%-20d",temp->data.score);
        temp=temp->next;
    }
}

Query method of fast and slow pointer of middle node data in one-way linked list with head node: traverse the linked list by fast and slow pointer, first point the slow pointer to the next node of the head node, place the fast pointer to the next node of the node indicated by the slow pointer, and end the traversal under the following conditions:

(1) . fast - > next = null; it means that the number of data nodes is even, and there are two intermediate nodes. When outputting the data indicated by the slow pointer, the data of two intermediate nodes should be outputted, one is the data domain indicated by the slow pointer, the other is the data of the next node indicated by the slow pointer, and both are intermediate nodes
(2) (fast - > next) - > next = null; it means that there are odd data nodes, and the data field pointed by the slow pointer is the data of the intermediate node

Posted by T_Hayden on Thu, 13 Feb 2020 13:59:53 -0800