- 论坛徽章:
- 0
|
//该程序运行为什么实现不了输出功能?
#include <iostream>
using namespace std;
typedef struct splist{
int *date;
int size;
int maxsize;
}list;
list initlist(list l)
{
cout<<"请输入初始化顺序表的最大存储数据元素个数:"<<endl;
int a;
cin>>a;
if ((l.date=(int*)malloc(a*sizeof(int)))==NULL)
{
cout<<"分配内存错误\n"<<endl;
exit(1);
}
l.maxsize=a;
l.size=0;
return(l);
}
void outputlist(list l)
{
int i=0;
while (i<l.size)
{
cout<<l.date<<' ';
i++;
}
cout<<endl;
}
list evaluate(list l)
{
l=initlist(l);
cout<<"请输入元素值,输入0时结束插入操作:"<<endl;
int i;
for (i=0;i<l.maxsize;i++)
{
int a;
cin>>a;
if(a!=0) l.date=a;
else break;
}
return(l);
}
list deletelist(list l)
{
cout<<"请输入删除顺序表中元素的位置:";
int i;
cin>>i;
if (i<0||i>=l.size) cout<<"输入位置错误"<<endl;
for (;i<l.size-1;i++) l.date=l.date[i+1];
l.size--;
return(l);
}
int findlist(list l)
{
cout<<"请输入查找元素的值:";
int a;
cin>>a;
cout<<"查找元素在顺序表的位置:";
int i;
for (i=0;i<l.size;i++)
{
if(a!=l.date)
{
cout<<"查找的元素不存在";
i=-1;
}
}
return(i);
}
list insertlist(list l)
{
cout<<"请输入元素插入位置:";
int i;
for(;
{
cin>>i;
if(i>l.size) cout<<"输入错误!请重新输入插入位置;";
else break;
}
cout<<"请输入要插入的元素:";
int a;
cin>>a;
l.size++;
for (;i<l.size;i++) l.date[i+1]=l.date;
l.date=a;
return(l);
}
list mergelist()
{
list l1,l2;
cout<<"请输入顺序表l1:";
evaluate(l1);
cout<<"请输入顺序表L2:";
evaluate(l2);
cout<<"合并后的顺序表l2为:";
int i,e;
for (i=0;i<l1.size;i++)
{
e=l1.date;
int n;
for(n=0;n<l2.size;n++)
{
if (e!=l2.date)
{
l2.size++;
l2.date[l2.size]=e;
}
else break;
}
}
return(l2);
}
int main()
{
int a,b;
list l;
for( ; ; )
{
cout<<"1.初始化\n";
cout<<"2.赋值\n";
cout<<"3.插入\n";
cout<<"4.查找\n";
cout<<"5.合并\n";
cout<<"6.删除\n";
cout<<"7.输出\n";
cout<<"Input 1-7: ";
cin>>a;
switch (b=a)
{
case 1: initlist(l);
break;
case 2: evaluate(l);
break;
case 3: initlist(l);
break;
case 4: findlist(l);
break;
case 5: mergelist();
break;
case 6:deletelist(l);
break;
case 7: outputlist(l);
break;
default: printf("输入错误!\n");
exit(0);
}
}
return 0;
}
[ 本帖最后由 LK.QQ 于 2009-3-22 21:41 编辑 ] |
|