找出链表的中间结点 C语言实现

来源:互联网 发布:软件项目经理责任制 编辑:程序博客网 时间:2024/06/07 00:38

代码实现

#include<stdio.h>#include<stdlib.h>#define N 50typedef struct linklist{    int data;    struct linklist *next;}list, *plist;/*创建链表*/void creat_list(plist* head){    int i;    plist p;    for (i = 1; i < N; i++)    {        if (i%3 == 0)        {            p = (plist)malloc(sizeof(list));            p->data = i;            p->next = *head;            *head = p;        }    }}/* 打印输出 */void print_list(plist head){    plist p = head;    while (p != NULL)    {        printf("%d ", p->data);        p = p->next;    }    printf("\n");}void find_middle_node(plist head){    int i,count = 0;    plist pcur = head;    if (pcur == NULL)    {        printf("空表\n");        return;    }    while (pcur!=NULL)    {        count++;        pcur = pcur->next;    }    /*当前指针pcur指向头结点*/    pcur = head;    for (i = 0; i < count / 2; i++)/*移动至链表中间位置处为止*/    {        pcur = pcur->next;        if (pcur->next == NULL)        {            break;        }    }    printf("中间结点:%d\n", pcur->data);}void main(){    plist head;    head = NULL;    printf("链表为:\n");    creat_list(&head);    print_list(head);    find_middle_node(head);    system("pause");}

这里写图片描述

0 0
原创粉丝点击