下载此文档

数据结构实验报告.docx


文档分类:高等教育 | 页数:约20页 举报非法文档有奖
1/20
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/20 下载此文档
文档列表 文档介绍
数据结构实验报告
设有两个无头结点的单链表,头指针分别为ha,hb,链中有数据域data,链域next,两链表的数据都按递增序存放,现要求将hb表归到ha表中,且归并后ha仍递增序,归并中ha表中已有的数据若hb中也有,则hb中的数据不归并到ha中,hb的链表在算法中不允许破坏。
源代码:
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node * next;
};
void createstack(struct Node **headp)
{
int n;
struct Node * p;
*headp=NULL;
cin>>n;
while(n!=0)
{
p=(struct Node *)malloc(sizeof(struct Node));
p->data=n;
p->next=*headp;
*headp=p;
cin>>n;
}
}
int outputNode(struct Node *head)
{
int length=0;
struct Node *p;
p=head;
while(p)
{
cout<<p->data<<"\t";
p=p->next;
length++;
}
cout<<endl;
return length;
}
void sortNod(struct Node ** head,int n)
{
int i,j,temp;
struct Node *p1,*p2;
p1=*head;
for(i=0;i<n;i++,p1=p1->next)
{
p2=p1->next;
for(j=i+1;j<n;j++,p2=p2->next)
{
if(p1->data>p2->data)
{
temp=p2->data;
p2->data=p1->data;
p1->data=temp;
}
}
}
}
bool isinclude(int n,struct Node **head)
{
struct Node *p;
p=*head;
while(p!=NULL)
{
if(n==p->data)
{
return true;
}
p=p->next;
}
return false;
}
struct Node * insertNode(int x,struct Node **headp)
{
struct Node *other,*last,*current;
other=(struct Node *)malloc(sizeof(struct Node));
other->data=x;
current=*headp;
while(x>current->data&&current->next!=NULL)
{
last=current;
current=current->next;
}
if(x<=current->data)
{
if(current==*headp)
{
other->next=*headp;
*headp=other;
}
else{
other->next=current;
last->next=other;
}
}
else{
other->next=NULL;
current->next=other;
}
return other;
}
void merge(struct Node **heada,struct Node **headb)
{
struct Node *pa,*pb;
pa=*heada;pb=*headb;
while(pb!=NULL)
{
if(!isinclude(pb->data,heada))
{
insertNode(pb->data,heada);
}
pb=pb->next;
}
}
int main()
{
struct Node *heada,*headb,*pa,*pb;
int lengtha,lengthb;
cout<<"input Node ha numbers end of 0:"<<endl;
createstack(&heada);
cout<<"input Node hb numbers end

数据结构实验报告 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数20
  • 收藏数0 收藏
  • 顶次数0
  • 上传人mh900965
  • 文件大小118 KB
  • 时间2017-12-18