用独立头结点的双向链表:用于数据按顺序插入

参照这位大神的内容,我把昨天的双向链表做了改进,变成了有独立表头的双向链表:
http://codingfreak.blogspot.com/2012/02/implementation-of-doubly-linked-list.html
独立头结点很重要,可以方便去搜索整个链表,昨天的链表问题就是出现在这里
我根据这个博客的内容,和自己的课题相关,又重写了一个双向数据链表,结果如下
可见无序的输入变成了有序的连接,这很重要
代码如下

#include<string.h>
#include<iostream>
#include<iomanip>
using namespace std;
typedef struct
{
    double x0;
    double y0;
    double x1;
    double y1;
    int flag;
}LineDxfData;
typedef struct LineNode
{
    LineDxfData linedata;
    struct LineNode* priorNode;
    struct LineNode* nextNode;
}LineNode;
//加入专用的head使得链表中的头结点和尾结点都指向NULL,这样逻辑才清晰
typedef struct Head
{
    unsigned int length;
    struct LineNode *first;//注意这里不是用struct Head *first,因为将来first是用来指向LineNode类型的指针的。
}Head;
LineNode*CreatNode(LineDxfData linedata);
Head*CreatHead();
Head*InsertLineNode(Head*head, LineNode*node, LineDxfData lindedata);
LineNode* FlineNode_xystart(Head*head, LineDxfData linedata);
LineDxfData LineDatSwap(LineDxfData linedata);
int main()
{
    Head* head = NULL;
    LineNode* Newnode = NULL;
    LineDxfData linedata;
    head = CreatHead();
    int ShowAllLineNode(Head*head);
    do
    {
        printf("链表测试。先输入链表中的数据,格式为:x0 y0 x1 y1 flag\n");
        fflush(stdin);
        cin >> linedata.x0 >> linedata.y0 >> linedata.x1 >> linedata.y1 >> linedata.flag;Newnode = CreatNode(linedata);//创造一个存放数据的结点
        //要把结点挂到链表上
        head = InsertLineNode(head, Newnode, linedata);//统一用插入,可以有前插,后插
        if (0 == linedata.flag)
        {
            break;
        }

    } while (1);
    ShowAllLineNode(head);//把所有的结点打印出来
}
LineNode*CreatNode(LineDxfData linedata)
{
    LineNode*newNode = (LineNode*)malloc(sizeof(LineNode));
    newNode->linedata = linedata;
    newNode->priorNode = NULL;//node是双向链表的头结点前向后向指针都为NULL;
    newNode->nextNode = NULL;
    return newNode;
}
Head*CreatHead()
{
    Head*head = (Head*)malloc(sizeof(Head));
    head->length = 0;//初始化的时候长度为零,指向的是一个空表。
    head->first = NULL;//初始化的时候要一并把head里面指向的双向链表的表头设置为NULL
    return head;
}
Head*InsertLineNode(Head*head, LineNode*node, LineDxfData linedata)
{
    LineDxfData lineNewdata;//用作数据交换
    LineNode*temp= NULL;
    LineNode*pstart= NULL;//判断是不是起起重合
    LineNode*pend = NULL;//判断是不是止止重合
    LineNode*pSend = NULL;//判断是不是起止重合
    LineNode*pEstart = NULL;//判断是不是止起重合
    //主分空表挂入,和有数据时候的挂入
    //空表挂入
    if (!(head->length))
    {

        head->first = node;
        head->length++;//注意这个用法,不要写成head->length=length++,因为这里不知道后面的length到底是什么
        return head;
    }
    //如果不是空表,那么要根据现在的数据和原来的数据之间的关系进行数据调换和找到插入点。
    //判断是不是起起重合,往前插,如果是那么要将现有的数据前后调换,并插入到原有结点的前面
    pstart = FlineNode_xystart(head, linedata);//pstart就是找到的结点,node该往pstart前面插入
    if (pstart)//要判断pstart是不是头结点,如果是那么pstart->priorNode=NULL,这个是标志。
    {
        if (!(pstart->priorNode))//如果pstart是头结点的情况下,就要把head->first改变
        {
            lineNewdata = LineDatSwap(linedata);
            node->linedata = lineNewdata;//覆盖原来的数据
            head->first = node;
            node->nextNode = pstart;//node的前向为NULL
            pstart->priorNode = node;//pstart的后向没有变化
            head->length++;
            return head;
        }
        else//如果不是在头结点
        {
            lineNewdata = LineDatSwap(linedata);
            node->linedata = lineNewdata;//覆盖原来的数据
            node->priorNode = pstart->priorNode;
            pstart->priorNode->nextNode = node;
            node->nextNode = pstart;
            pstart->priorNode = node;
            head->length++;
            return head;
        }
    }
    //如果不是空表,现在数据与以往数据之间也没有关系时候,直接往最后挂
    else
    {
        temp = head->first;//遍历用的,此时temp为头结点
        while (temp->nextNode)//当temp->nextNode为NULL的时候说明已经找到最后一个了,这时候要跳出循环
                            //特别注意如果这里用while(temp)那么跳出循环的时候,temp已经是NULL了这不是最后一个结点,要指向NULL才是!!

        {
            temp = temp->nextNode;
        }
        node->priorNode = temp;
        temp->nextNode = node;
        head->length++;//每插入一次就要加一次
        return head;
    }
}
int ShowAllLineNode(Head*head)//if只是判断,没有循环功能
{
    LineNode*temp=NULL;
    int i;//用作循环使用
    if (!(head->length))//空表
        cout << "链表为空" << endl;
    else
    {
        temp= head->first;
        //如果把这里写作是temp->nextNode= head->first那么就会引起访问冲突,
        //因为head->first指的是头结点的地址,这里写成temp->nextNode就会使得temp不知道是哪里位置。
        for (i = 0; i < head->length; i++)//数字控制,也可以是用temp来控制
        {
            cout << temp->linedata.x0 << setw(5) << temp->linedata.y0 << setw(5) << temp->linedata.x1 << setw(5) << temp->linedata.y1 << endl;
            temp = temp->nextNode;
        }
    }
    return 0;
}
//需找是否哪个结点是起起重合
LineNode* FlineNode_xystart(Head*head, LineDxfData linedata)
{
    LineNode*temp = NULL;//用来查找
    double x_start, x_end, y_start, y_end;
    x_start = x_end = y_start = y_end = 0.00;
    x_start = linedata.x0;
    y_start = linedata.y0;
    x_end = linedata.x1;
    y_end = linedata.y1;
    temp = head->first;//此时temp为头结点
    while (temp)//这里不是找最后一个结点,是要全部去比较不能用temp->nextNode,
    {
        if ((x_start ==temp->linedata.x0) && (y_start ==temp->linedata.y0))//这里是不是有问题
            return temp;
        temp = temp->nextNode;
    }
    return NULL;
}
LineDxfData LineDatSwap(LineDxfData linedata)
{

    LineDxfData LineNewdata;
    LineNewdata.x0 = linedata.x1;
    LineNewdata.y0 = linedata.y1;
    LineNewdata.x1 = linedata.x0;
    LineNewdata.y1 = linedata.y0;
    return LineNewdata;
}

注:事实上数据之间的关系可以是起起相同,起止相同,止止相同,止起相同,不同的类型有不同的数据插入方式和处理方法,以上只是起起相同的代码。
ps:Google真是好东西。有空把那位大神的博客翻译算了,讲数据结构讲得太好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值