您的位置:首页 > 其它

一 线性表的有关操作

2017-06-19 12:14 162 查看

 实验一

一、实验目的 

1、掌握单向链表的存储特点及其实现。

2、理解和掌握单链表的类型定义方法和结点生成方法。

3、掌握单向链表的插入、删除算法及其应用算法的程序实现。

二、实验仪器及环境:

    PC计算机;windows XP操作系统、Visual C++6.0

三、实验内容及结果(按照具体实验题目,按照如下格式书写)

1、随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。

2、遍历单向链表(显示)。

3、把单向链表中元素逆置(不允许申请新的结点空间)。

4、在单向链表中删除所有的偶数元素(值为偶数)结点。

5、编写在非递减有序链表中插入一个元素使链表元素仍有序的函数,并利用该函数建立一个非递减有序单向链表。

6、利用算法5建立两个非递减有序单向链表,然后合并成一个非递增链表。

7、利用算法5建立两个非递减有序单向链表,然后合并成一个非递减链表。

8、编写一个主函数,调试上述算法。

 

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef int  ElemType;//元素类型
typedef  struct node
{
ElemType data;
struct node *next;
bool iseven=false;
}LNode,*LinkList;

LNode *creat_list(LNode *head,int n)//创建一个长度为n的链表
{
LNode *p,*q;
int i;
q=head;
for(i=0; i<n; i++)
{
p=(LNode*)malloc(sizeof(LNode));//开辟一个新结点
if(p!=NULL)
{
q->next=p;
q=p;
}
}
p->next=NULL;
return head;//返回头指针,这样就可以对链表进行操作
}
void output(LNode *head)//输出函数,从链表头部输到尾部
{
LNode *p=head;
while(1)
{   p=p->next;
printf("%d \n",p->data);

if(p->next==NULL)
break;
}
}
void input(LNode *head,int n)//产生随机数
{
LNode *p=head->next;
while(n>0)
{
p->data=rand() % 101;  //产生0-100的随机数
if(p->data%2==0)
p->iseven=true;
p=p->next;
n--;
}
}

LNode *find_final(LNode *head,int n)//找到链表最后一个节点
{
LNode *p=head;
for(int i=0;i<n;i++)
p=p->next;
return p;

}

LNode *fanzhuan(LNode *head,int n)//翻转链表
{
LNode *finallist=find_final(head,n);
LNode *p=finallist;//p等于表尾
LNode *q=p;//记录第一个表尾,方便接头

for(int i=n;i>0;i--)
{
finallist=find_final(head,i);
//cout<<finallist->data;
p->next=finallist;//把表尾连上
p=p->next;
//cout<<"i="<<i<<endl;system("pause");
}
p->next=NULL;//最后p的next指向空,不要忘记
head->next=q;
return head;
}

LNode *shanchu(LNode *head)//从头找到一个偶数并删除
{
LNode temp;
LNode *p=head->next,*q,*ans=head;
if(head->next->data%2==0)
ans=p;
else
for(p=head->next;p->next!=NULL;p=p->next)
{
if(p->next->data%2==0)
{
q=p->next->next;
p->next=q;
break;

}

}return ans;
}

LNode *delete_all_even(LNode *head,int n)//删除所有偶数节点
{
LNode *ans=head;
for(int i=0;i<n;i++)
ans=shanchu(ans);
return ans;
}

void paixu(LNode *head)//排序函数
{
LNode temp;
LNode *p,*q,*max;
for(p=head->next;p->next!=NULL;p=p->next)
{
max=p;
for(q=p->next;q!=NULL;q=q->next)
if(q->data< max->data)
max=q;
if(max!=p)      //交换结点的数据域内容,指针内容没变。
{
temp.data=p->data;
p->data=max->data;
max->data=temp.data;
}
}
}

void charu(LNode *w,LNode *head)//插入一个节点到head中
{
LNode *p=head->next,*q;
if(w->data<p->data)
{
head->next=w;
w->next=p;
}
else
for(p=head->next;p->next!=NULL;p=p->next)
if(w->data<p->next->data&&w->data>p->data)
{
q=p->next;
p->next=w;
p=w;
p->next=q;
}
if(w->data>p->data&&p->next==NULL)
{
p->next=w;
p=w;
p->next=NULL;
}
}

int getlength(LNode *head)
{
LNode *p=head;
int ans=0;
while(1)
{   ans++;
p=p->next;
if(p->next==NULL)
break;
}
return ans;
}
LNode *hebing(LNode *head,LNode *head2)
{   LNode *p=head;
while(1)
{
p=p->next;
if(p->next==NULL)
break;
}
p->next=head2->next;
return head;
}
int main()
{
LNode *head=(LNode *)malloc(sizeof(LNode));
int n;
cout<<"请输入链表长度:\n";
cin>>n;
creat_list(head,n);
input(head,n);
cout<<"初始链表中的内容\n";
output(head);

fanzhuan(head,n);
cout<<"翻转后链表中的内容为\n";
output(head);

fanzhuan(head,n);
cout<<"翻转回来链表中的内容为\n";
output(head);
delete_all_even(head,n);
cout<<"删除所有偶数之后链表中的内容为\n";
output(head);
cout<<"现在有一个非递减有序链表,其内容为\n";
paixu(head);
output(head);
cout<<"请输入要插入的元素\n";
LNode *m=(LNode *)malloc(sizeof(LNode));
cin>>m->data;
charu(m,head);
cout<<"插入元素之后的链表中的内容为\n";
output(head);
cout<<"下面将新建立一个链表"<<endl;
LNode *head2=(LNode *)malloc(sizeof(LNode));
int n2;
cout<<"请输入链表长度:\n";
cin>>n2;
creat_list(head2,n2);
input(head2,n2);
cout<<"原链表中的内容\n";
output(head);
cout<<"新链表中的内容\n";
paixu(head2);
output(head2);

cout<<"合并之后的非递增链表为:\n";
hebing(head,head2);
paixu(head);
fanzhuan(head,getlength(head));
output(head);
cout<<"合并之后的非递减链表为:\n";
fanzhuan(head,getlength(head));
output(head);

return 0;
}


 

 

四、实验心得体会:(包括遇到的问题及解决办法)

    链表的操作过程中还是很容易出错的,链表的增加和删除操作必须考虑到所有情况(头,尾,中间),稍有不慎会造成运行错误,比如在编写翻转函数时就因为尾节点的next没有指向空而导致运行错误。所以今后的学习生活中要更加严谨才能避免不必要的错误。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: