求解两个数据结构的题目,具体要用c语言实现,求代码

如题所述

呵呵.我这里刚好有前些天给同学写的一个.看下结果吧.. 代码有点多哦.:
#include<iostream>
#include<stdlib.h>using namespace std;
typedef struct _ListNode // 此处修改为自定义数据类?
{
int data; // 结点数据
_ListNode *link; // 指向下个结点 _ListNode() // 此处添加默认的"构造函数"
{
data = 0;
link = NULL;
}
}ListNode, LinkList;
/******************************函数定义注释头***********************************
* 函数名 : void CreateLinkList(LinkList *head, int length)
* 开发时间 : 2010-3-19 11:00:15
* 函数功能 : 创建一个链表
* 全局变量和结构 : 无
* 调用的主要函数 : 无
* 返回值说明 : 返回链表头结点指针
* 参数表 : head : 链表头结点
length : 链表长度
******************************函数体定义**************************************/
LinkList * CreateLinkList(LinkList *head, int length)
{ LinkList *temp = NULL;
int predata = 0, curdata = 0;
temp = head;
for (int i = 1; i <= length; i++)
{
cout << "输入第" << i <<"个结点data:";
head->link = new ListNode();
head = head->link; if (i == 1)
{
cin >> head->data; // 输入第一个结点的data
predata = head->data; // 保存第一个结点的data
}
else
{
while (1)
{
//cout<<"输入第"<<i<<"个结点的data:";
cin>>curdata;
if (curdata >= predata) // 现在输入的数小于前一个数
{
head->data = curdata;
predata = curdata; // 修改数据
break; // 终止输入
}
else // 否则继续输入
{
cout<<"error!!current data:"<<curdata<<"小于previous data:"
<<predata<<"不满足链表条件,重新输入!"<<endl;
continue;
} // if (curdata >= predata)
} // while (1)
}// if (i == 1)
}// for (int i = 1; i <= length; i++) head = temp; return(head);
}
/******************************函数定义注释头***********************************
* 函数名 : void Connect(const LinkList *listA,
const LinkList *listB,
LinkList *listC)
* 开发时间 : 2010-3-19 11:05:38
* 函数功能 : 进行两个链表的链接
* 全局变量和结构 : 无
* 调用的主要函数 : 无
* 返回值说明 : 无
* 参数表 : listA : 第一个链表
listB : 第二个链表
listC : 链接后的链表头结点
******************************函数体定义**************************************/
void Connect(const LinkList *listA, const LinkList *listB, LinkList *listC)
{
const ListNode *pointer1 = NULL;
const ListNode *pointer2 = NULL;
ListNode *pointer3 = NULL;
pointer1 = listA->link;
pointer2 = listB->link; pointer3 = listC;
while ((pointer1 != NULL) && (pointer2 != NULL))
{
pointer3->link = new ListNode();
pointer3 = pointer3->link;
if (pointer1->data <= pointer2->data)
{
pointer3->data = pointer1->data;
pointer1 = pointer1->link; // 修改指针
}
else
{
pointer3->data = pointer2->data;
pointer2 = pointer2->link; // 修改指针
}
} // 将剩余的数据添加到链接后的链表中
if (pointer1 == NULL)
{
while (pointer2 != NULL)
{
pointer3->link = new ListNode();
pointer3 = pointer3->link; pointer3->data = pointer2->data;
pointer2 = pointer2->link;
}
}
if (pointer2 == NULL)
{
while (pointer1 != NULL)
{
pointer3->link = new ListNode();
pointer3 = pointer3->link; pointer3->data = pointer1->data;
pointer1 = pointer1->link;
}
}
}/******************************函数定义注释头***********************************
* 函数名 : void Display(const LinkList *list)
* 开发时间 : 2010-3-19 13:18:15
* 函数功能 : 输出一个链表中的数据
* 全局变量和结构 : 无
* 调用的主要函数 : 无
* 返回值说明 : 无
* 参数表 : list : 链表头结点
******************************函数体定义**************************************/
void Display(const LinkList *list)
{
const ListNode *p = NULL;
p = list->link; cout<<endl<<"这个链表中的数据为:"<<endl;
while (p != NULL)
{
cout<<p->data<<" ";
p = p->link;
} cout<<endl;
}
/******************************函数定义注释头***********************************
* 函数名 : void Destroy(LinkList *list)
* 开发时间 : 2010-3-19 13:20:23
* 函数功能 : 销毁一个链表
* 全局变量和结构 : 无
* 调用的主要函数 : 无
* 返回值说明 : 无
* 参数表 : 链表头结点
******************************函数体定义**************************************/
void Destroy(LinkList *list)
{
ListNode *p = NULL;
ListNode *temp = NULL;
p = temp = list;
while(p != NULL)
{
temp = p->link;
delete p;
p = temp;
} list = NULL;
}/******************************函数定义注释头***********************************
* 函数名 : void main()
* 开发时间 : 2010-3-19 13:02:47
* 函数功能 :
* 全局变量和结构 :
* 调用的主要函数 :
* 返回值说明 :
* 参数表 :
******************************函数体定义**************************************/
void main()
{
ListNode *h_first = NULL;
ListNode *h_second = NULL; // 分别为第一个和第二个链表的头结点
ListNode *h_result = NULL; // 链接后的链表头结点
int length = 0; // 链表长度
h_first = new ListNode();
h_second = new ListNode(); // 分配空间 cout << "请输入第一个链表的长度:";
cin >> length;
h_first = CreateLinkList(h_first, length);
cout << "请输入第二个链表的长度:";
cin >> length;
h_second = CreateLinkList(h_second, length); Display(h_first);
Display(h_second); h_result = new ListNode();
Connect(h_first, h_second, h_result);
Display(h_result);
Destroy(h_first);
Destroy(h_second);
Destroy(h_result);追问

要求用c语言实现,不是c++,不过还是谢谢了

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答