标题:递增线性表中插入一个元素到适当的位置 这个运行结果不正确看了很长时间不 ...
取消只看楼主
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
结帖率:88.89%
已结贴  问题点数:20 回复次数:1 
递增线性表中插入一个元素到适当的位置 这个运行结果不正确看了很长时间不知道哪里的错误求大神指导
#include<iostream>
using namespace std;
#include<math.h>
#include<stdlib.h>
#define maxsize 20
int n;
typedef struct node
{ int *data;
int length;
int listsize;
}sqlist;
void createsqlist(sqlist &L)
{ L.data=(int*)malloc(sizeof(int)*maxsize);
if(!L.data)
exit(1);
}
void initsqlist(sqlist &L)
{int i;
int *p;
p=L.data;
cout<<"enter your list's number:"<<"\t";
cin>>n;
cout<<"enter your list's elements:"<<"\t";
for(i=0;i<n;i++,p++)
cin>>*p;
}
void outputsqlist(sqlist &L)
{int *q;
q=L.data;
    cout<<"your sqlist is:"<<endl;
for(int i=0;i<n;i++,q++)
cout<<*q<<"\t";
}
void insertsqlist(sqlist &L)
{int c,j;
cout<<"enter you want insert data:"<<"\t";
cin>>c;
if(c<=L.data[0])
j=0;
else
if(c>=L.data[n-1])
j=n;
else
for(int i=0;c>L.data[i];i++)
j=i;
for(;j<n;n--)
L.data[n]=L.data[n-1];
L.data[j]=c;
cout<<L.data[1];
cout<<endl;
n=n+1;
cout<<"inserted list is :"<<endl;
for(int h=0;h<n;h++)
cout<<L.data[h]<<"\t";
cout<<endl;
}
int main()
{sqlist L1;
createsqlist(L1);
initsqlist(L1);
outputsqlist(L1);
insertsqlist(L1);
return 0;
}





搜索更多相关主题的帖子: elements include number 线性表 元素 
2015-11-10 22:36
winner1995
Rank: 2
等 级:论坛游民
帖 子:64
专家分:41
注 册:2015-10-11
得分:0 
回复 3楼 li459461891谢了 我会看的不过刚才也勉强做出来一个

#include<stdio.h>
#include<iostream.h>
#include <stdlib.h>
#define maxsize 20
int n;
typedef struct
{int * data;
int length;
int listsize;
}sqlist;
void creatsqlist(sqlist &list)
{list.data=(int*)malloc(sizeof(int)*maxsize);
if(!list.data)
exit(1);
list.length=0;
list.listsize=maxsize;
}
void initsqlist(sqlist &list)
{int i;
  int * p;
cout<<"plese enta you wish number"<<endl;
cin>>n;
p=list.data;
cout<<"please enta your data in order"<<endl;
for(i=0;i<n;i++,p++)
{cin>>*p;
list.length++;}
cout<<"your list is"<<endl;
for(i=0;i<n;i++)
cout<<list.data[i]<<"\t";
cout<<endl;
}
void insert(sqlist &list)
{
    int a,b;
int k=0;

cout<<"please enter the insertdata's number"<<endl;
cin>>b;
if(b>list.data[list.length-1])
a=list.length+1;
else
{while(b>list.data[k])
k=k+1;
a=k+1;}
cout<<a<<endl;
{int j;
for(j=list.length;j>=a;j--)
list.data[j]=list.data[j-1];
list.data[a-1]=b;
list.length++;
}
cout<<"the newinsert list is"<<endl;
for(int i=0;i<=n;i++)
cout<<list.data[i]<<"\t";
cout<<endl;
}
int main()
{sqlist list1;
creatsqlist(list1);
initsqlist(list1);
insert(list1);

return 0;
}

唯望君安~
2015-11-11 12:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-459079-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.707766 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved