C语言:1.建立含n个数据元素的顺序表并输出该表中各元素的值及顺序表的长度。 紧急求 速度

1.建立含n个数据元素的顺序表并输出该表中各元素的值及顺序表的长度。

具体的实验数据,建立一个顺序表L={21,23,14,5,56,17,31},然后在第i个位置插入元素68。

2.建立一个带头结点的单链表,结点的值域为整型数据。要求将用户输入的数据按尾插入法来建立相应单链表。

1、

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct Lnode{
int *data;
int length;
int maxsize;
struct Lnode *next;
}List;
void creatList(List &L,int n)
{
int count = 0;
L.data = (int*)malloc(sizeof(int)*n);
if (!L.data)
cout << "申请空间失败";
cout << "input the numbers ";
for (int i = 0; i < n; i++)
{
cin >> L.data[i];
count++;
}
L.length = count;
L.maxsize = n;
}
void Insert(List &L, int i, int data)
{
int j;
if (i >= L.maxsize)
cout << "插入位置不对";
if (L.length+1 >= L.maxsize)
{
L.data = (int *)realloc(L.data, L.maxsize + sizeof(int)*L.maxsize);
L.maxsize += L.maxsize;
}
for ( j = L.length-1 ; j >= i - 1; j--)
{
L.data[j + 1] = L.data[j];
}
L.data[j+1] = data;
L.length += 1;
}
void print(List L)
{
cout << "output the List:";
for (int i = 0; i < L.length; i++)
{
cout << L.data[i]<<" ";
}
cout << endl;
}
void main()
{
int m,number,locate;
List L;
cout << "please input the length of List :";
cin >> m;
creatList(L,m);
print(L);
cout << "please input the location  and the number you want:";
cin >>locate >> number;
Insert(L, locate, number);
print(L);
system("pause");

}

追问


有一个错误。。这个是什么意思?

追答

没错啊,我在VS2013里面完美运行的啊,我刚才把我给你的代码复制运行了一遍,没毛病啊,可能是编译器的问题,你在开头加上一个#include "stdafx.h"试试,这些代码我在VS2013运行时没有问题,估计就是编译器的事情,你用的是VC 6.0吧

追问

嗯嗯,好的

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-22
我会写第二个,好像有点长追问

可以告诉我吗?