参照这位大神的内容,我把昨天的双向链表做了改进,变成了有独立表头的双向链表:
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真是好东西。有空把那位大神的博客翻译算了,讲数据结构讲得太好了。