Jamesyoung36 2017-04-03 15:18 采纳率: 21.1%
浏览 1023

关于线性表的问题,为什么什么都输不出来,求指点

 #include<iostream>
using namespace std;

#define size 20
#define stepsize 10

struct array
{
    int* spot;//数组的名字
    int curlen;//数组的当前长度
    int arraylen;//数组存储空间的长度
};

void initarray(array* p)//数组的初始化
{
    p->spot = new int[size];
    p->curlen = 0;
    p->arraylen = size;
}


void output(array p)
{
    cout << "当前数组的长度为:" << p.curlen << endl;
    cout << "当前数组的内存空间为:" << p.arraylen << endl;
    if (p.curlen != 0)
    {
        cout << "该数组的内容为:";
        for (int i = 0; i < p.curlen; i++)
        {
            cout <<p. spot[i] << ",";
        }
        cout << endl;
    }
}

void insert(array* p,int pos,int value)
{
    if ((pos<0) || (pos>p->curlen))
    {
        cout << "插入的位置有错误" << endl;
    }
    else
    if ((p->curlen + 1) < p->arraylen)//判断是否越界
    {
        int* temp = new int[p->arraylen + stepsize];
        for (int l = 0; l < p->curlen; l++)
        {
            temp[l] = p->spot[l];
        }
        delete[]p->spot;
    }
    else
    {
        int i, j;
        j = p->curlen+1;
        i = j - 1;
        for (int k = 0; k < p->curlen - pos + 1; k++,i--,j--)
        {
            p->spot[j] = p->spot[i];
        }
        p->spot[i] = value;
        p->curlen++;
    }
}
int main()
{
    array myarray;
    initarray(&myarray);
    output(myarray);
    for (int i = 0; i < 5; i++)
    {
        insert(&myarray, 0, i);
    }
    output(myarray);

    cout << endl;
    return 0;
}
  • 写回答

2条回答 默认 最新

  • devmiao 2017-04-03 15:49
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能