• 数据结构-寻找单链表的中间元素


    1:这里使用一个只用一遍扫描的方法。描述如下:

        假设mid指向当前已经扫描的子链表的中间元素,cur指向当前已扫描链表的末节点,那么继续扫描即移动cur到cur->next,这时只需判断一下应不应该移动mid到mid->next就行了。所以一遍扫描就能找到中间位置。代码如下:

    // ConsoleApplication15.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <malloc.h>
    #include <iostream>
    using namespace std;
    
    typedef struct node//定义链表结构体
    {
        int data;//节点内容
        node *next;//指向结构体的指针,下一个节点
    }node;
    
    node *create()//创建单链表
    {
        int i = 0;//链表中数据的个数
        node *head, *p, *q;//这些的本质是节点的地址
        int x = 0;
        head = NULL;
        q = NULL;//初始化q,q代表末节点
        p = NULL;
        while (1)
        {
            printf("please input the data:");
            scanf_s("%d", &x);
            if (x == 0)
                break;//data为0时创建结束
            p = (node *)malloc(sizeof(node));//用于每次输入链表的数据
            p->data = x;
            if (++i == 1)//链表头的指针指向下一个节点
            {
                head = p;
                q = p;
            }
            else
            {
                q->next = p;//连接到链表尾端
                q = p;
            }
            q->next = NULL;/*尾结点的后继指针为NULL(空)*/
        }
        return head;
    }
    
    int length(node *head)
    {
        int len = 0;
        node *p;
        p = head->next;
        while (p != NULL)
        {
            len++;
            p = p->next;
        }
        return len;
    }
    
    void print(node *head)
    {
        node *p;
        p = head;
        while (p)/*直到结点q为NULL结束循环*/
        {
            printf("%d ", p->data);/*输出结点中的值*/
            p = p->next;/*指向下一个结点*/
        }
    }
    
    node *search_node(node *head, int pos)//查找单链表pos位置的节点,返回节点的指针。pos从0开始,0返回head节点
    {
        node *p = head->next;
        if (pos < 0)//pos位置不正确
        {
            printf("incorrect position to search node!");//pose位置不正确
            return NULL;
        }
        if (pos == 0)
        {
            return head;
        }
        if (pos == NULL)
        {
            printf("Link is empty!");//链表为空
            return NULL;
        }
        while (--pos)
        {
            if ((p = p->next) == NULL)
            {
                printf("incorrect position to search node!");//超出链表返回
                break;
            }
        }
        return p;
    }
    
    node *insert_node(node *head, int pos, int data)//单链表的插入
    {
        node *item = NULL;
        node *p;
        item = (node *)malloc(sizeof(node));
        item->data = data;
        if (pos == 0)//插在head后面
        {
            head->next = item;//head后面是item
            return head;
        }
        p = search_node(head, pos);//获得pos的节点指针
        if (p != NULL)
        {
            item->next = p->next;//item指向原pos节点的后一个节点
            p->next = item;//把item插入到pos的后面
        }
        return head;
    }
    
    node *delete_node(node *head, int pos)//删除节点
    {
        node *item = NULL;
        node *p = head->next;
        if (p = NULL)
        {
            printf("link is empty!");
            return NULL;
        }
        p = search_node(head, pos - 1);//获得位置pos节点的指针
        if (p != NULL&&p->next != NULL)
        {
            item = p->next;
            p->next = item->next;
            delete item;
        }
        return head;
    }
    
    node *reverse(node *head)//链表的逆置
    {
        node *next;
        node *prev = NULL;
        while (head != NULL)
        {
            next = head->next;
            head->next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }
    
    node *search(node *head)
    {
        int i = 0;
        int j = 0;
        node *current = NULL;
        node *middle = NULL;
        current = middle = head->next;
        while (current != NULL)
        {
            if (i / 2 > j)
            {
                j++;
                middle = middle->next;
            }
            i++;
            current = current->next;
        }
        return middle;
    }
    int main()
    {
        node *head = create();//创建单链表
        print(head);//输出链表
        node *mid;
        mid = search(head);
        cout << "中间的元素是:"<<mid ->data << endl;
    
    
        return 0;
    }
    View Code

    运行结果:

  • 相关阅读:
    JSON开源库API【转载】https://nlohmann.github.io/json/index.html
    ZeroMQ示例(C/C++/PHP)详解三种模式
    Makefile精髓篇【转】
    JNI数组操作
    【摘要】malloc、calloc和realloc的用法
    容器内部安装scp,拷贝到外部物理机
    关于VMware虚拟机安装镜像时黑屏的解决办法
    linux下打压缩解压
    高效载入“大”图片
    后台执行Bitmap加载
  • 原文地址:https://www.cnblogs.com/lovemi93/p/7599310.html
Copyright © 2020-2023  润新知