您的位置:首页 > 其它

单向链表的有关操作(链式存储结构)

2012-05-30 21:44 246 查看
⑴随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。

⑵遍历单向链表。

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

⑷在单向链表中删除所有的偶数元素结点。

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

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

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

⑻利用算法1建立的链表,实现将其分解成两个链表,其中一个全部为奇数,另一个全部为偶数(尽量利用已知的存储空间)

⑽在主函数中设计一个简单的菜单,分别调试上述算法。

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

typedef struct Lnode{
int data;
struct Lnode * next;
}Lnode,*Linklist;

Lnode * input()//输入;
{
cout<<"请输入整型数据,以^Z结束输入\n";

Linklist p1,p2,headlist;
int str;
headlist=(Lnode *)malloc(sizeof(Lnode));
p1=p2=headlist;
if(p1==NULL)
{cout<<"存储空间申请失败,结束输入:\n";return NULL;}

headlist->next=NULL;
while(scanf("%d",&str)!=EOF)
{

p1=(Lnode *)malloc(sizeof(Lnode));

p1->data=str;
p2->next=p1;
p2=p1;
p2->next=NULL;
}

return headlist;
}

bool output(Linklist headlist)//输出;
{
Linklist p1;
p1=headlist->next;
if(!p1)
return 0;
while(p1!=NULL)
{
cout<<p1->data<<' ';
p1=p1->next;
}
cout<<endl;
return 1;
}

int length(Linklist headlist)//求链表长度;
{
Linklist p;
int count=1;
p=headlist->next;

while(p->next!=NULL)
{
count++;
p=p->next;
}
return count;
}

bool fz(Linklist headlist,int n)//对链表元素进行反转;
{
int i,j,k,t;
Linklist p1,p2;
k=n/2;
p1=p2=headlist;
if(headlist->next==NULL)
return false;
for(i=1;i<=k;i++)
{p2=p2->next;

p1=p2;
for(j=i+1;j<=n-i+1;j++)
{p1=p1->next;}
t=p2->data;p2->data=p1->data;p1->data=t;
}
return true;
}

int delete_even(Linklist headlist)//删除偶数;
{

Linklist p1,p2;
if(headlist->next==NULL)
{cout<<"链表是空表\n";return 0;}
p1=headlist->next;
p2=headlist;
while(p1)
{
if(p1->data%2==0)
{p2->next=p1->next;
free(p1);
p1=p2->next;
}
else
{
p1=p1->next;
p2=p2->next;
}
}

return 1;
}

Linklist divide(Linklist headlist)//将链表分割为两个;返回偶数链表的头地址;
{
Linklist p1,p2,p3;
int t;
if(headlist->next==NULL)
{cout<<"链表为空表\n";return NULL;}
int i,j,k;
for(p1=headlist->next;p1->next!=NULL;p1=p1->next)
{
p3=p1;
if(p3->data%2==1)
continue;
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if(p2->data%2==1)
p3=p2;
}
if(p1!=p3)
{
t=p1->data;p1->data=p3->data;p3->data=t;
}
}

for(p1=headlist->next,p2=headlist;p1!=NULL;p1=p1->next,p2=p2->next)
{
if(p1->data%2==0)
break;
}

if((p1!=NULL)&&(p1!=headlist->next))
{p3=(Linklist)malloc(sizeof(Lnode));
p3->next=p1;
p2->next=NULL;
return p3;
}
else if(p1==headlist->next)
{cout<<"链表元素全为偶数\n";return headlist;}
else
{cout<<"链表元素全为奇数\n";return NULL;}

}

int delete_list(Linklist headlist)//将链表清空;
{   if(headlist==NULL)
return 0;
Linklist p1,p2;
p1=headlist;p2=p1->next;
while(p2)
{

free(p1);
p1=p2;
p2=p2->next;
}
free(p1);
return 1;
}

int insert(Linklist headlist,int e)//非递减插入元素;
{Linklist p2,p1,p3;
p2=headlist;
p1=p2->next;
while(p1)
{if(p1->data>e)
break;
p2=p2->next;
p1=p1->next;
}

p3=(Linklist)malloc(sizeof(Lnode));
if(p3==NULL)
{cout<<"空间申请失败\n";return 0;}
p3->data=e;
p3->next=p1;
p2->next=p3;
return 1;
}

int length_list(Linklist headlist)//求链表长度;
{
Linklist p;
int count=1;
p=headlist->next;

while(p->next!=NULL)
{
count++;
p=p->next;
}
return count;
}

bool sort(Linklist headlist,int n)//排序;
{
int i,j,k;
int t,s;
Linklist p1,p2;
p1=p2=headlist->next;
k=n-1;
for(i=k;i>=1;i--)
{p1=p2=headlist->next;
p1=p2->next;
for(j=1;j<=i;j++)
{
if(p2->data<p1->data)
{t=p2->data;p2->data=p1->data;p1->data=t;}
p1=p1->next;
p2=p2->next;
}
}
return true;
}

Linklist union1_list(Linklist head1,Linklist head2)//将两个链表合并为一个,合并后仍为非递减;
{
Linklist p;
p=head2->next;
while(p)
{
insert(head1,p->data);
p=p->next;
}
delete_list(head2);
return head1;
}

int main(int argc, char *argv[])
{Linklist headlist;
headlist=input();
cout<<"输出输入的元素\n";

output(headlist);

fz(headlist,length(headlist));
cout<<"逆置后的元素为\n";

output(headlist);
delete_even(headlist);
cout<<"删除偶数后的链表序列为:\n";
output(headlist);
cout<<"将链表分割为奇数和偶数的两部分\n";
Linklist p=divide(headlist);
if(p==NULL)
{cout<<"偶数链表为空表\n";
cout<<"奇数链表为:\n";
output(headlist);
delete_list(headlist);
}
else if(p==headlist)
{cout<<"奇数链表为空表\n";
cout<<"偶数链表为\n";
output(headlist);
delete_list(headlist);
}
else
{  cout<<"偶数链表的元素为\n";
output(p);
delete_list(p);
cout<<"奇数链表的元素为\n";
output(headlist);
delete_list(headlist);
}

cout<<"非递减整数序列:\n";
headlist=input();
cout<<"输入的元素为\n";
output(headlist);
cout<<"请输入要插入的元素\n";
int e;
cin>>e;
insert(headlist,e);
cout<<"插入后的链表为\n";
output(headlist);
delete_list(headlist);
Linklist head1,head2,head3;
cout<<"非递减整数序列1:\n";
head1=input();
cout<<"输入的表1为\n";
output(head1);
cout<<"非递减整数序列2:\n";
head2=input();
cout<<"输入的表2为\n";
output(head2);

head3=union1_list(head1,head2);
cout<<"合并后的非递减链表为\n";
output(head3);
sort(head3,length_list(head3));
cout<<"合并后的非递增链表为\n";

output(head3);
delete_list(head3);

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