#include<iostream>
using namespace std;
#include<stdlib.h>
#include <malloc.h>
#include<stdio.h>
#define ok 1
#define error 0
#define List_Init_Size 10
#define CREMENT 10
typedef struct {
int *data;
int length; //数据长度
int Listsize;//容量
}Sqlist;
char *Initlist(Sqlist &L)
{
L.data = (int *)malloc(List_Init_Size*sizeof(int));
if (!L.data)
return "error";
L.length = 0;
L.Listsize = List_Init_Size;
return "ok";
}
void Print(Sqlist L)
{
int n = 1;
cout << "数据表如下:" << endl;//n和数据长度比较
while (n <= L.length)
{
cout << *L.data<<" ";
L.data++;
n++;
}
cout << endl;
}
char *Dellist(Sqlist &L)
{
int n, k = 0;
Sqlist p;
p = L;
cout << "删除第几个数据:";
cin >> n; cout << endl;
if (n <= 0 || n > L.length)
return "error\n";
while (k < L.length)
{
if (k >= n)
*(p.data-1) = *p.data;
p.data++;
k++;
}
--L.length;
return "ok\n";
}
char *Dellist2(Sqlist &L, int e)
{
Sqlist p; int k=0,j,flag=0;
p= L;
for (k; k < p.length; k++)
{
if (p.data[k] == e)
{
for (j = k; j <= L.length-1; j++)
{
p.data[j] =p.data[j+1];
}
L.length--;
k--;
flag = 1;
}
}
if (flag)return "ok";
else return "error";
}
char *Input(Sqlist &L)
{
Sqlist p;
int n,i=1;
p = L;
cout << "输入几个数据:"; cin >> n; cout << endl;
if (n < 1||n>L.Listsize)return "error";
while (i<= n)
{
cout << "请输入第"<<i<<"个数据:";
cin >> *p.data; cout << endl;
p.data++;
L.length++;
i++;
}
return "ok\n";
}
char *Insertlist(Sqlist &L,int e,int n)//插入数据e,n第几个数据后插入
{
Sqlist p;
p = L;
int k = 0;
if (p.length >= p.Listsize)
{
L.data = (int *)realloc(L.data, (List_Init_Size + CREMENT)*sizeof(int));
L.Listsize = List_Init_Size + CREMENT;
}
cout << *p.data<<endl;
p.data += p.length;
while (k<p.length-n) //p移动到n后一位
{
*p.data = *(p.data - 1);
p.data--;
++k;
}
*p.data = e;
L.length++;
return "ok\n";
}
int main()
{
Sqlist L; int e, n,choice;
do{
cout << "1.录入数据" << endl;
cout << "2.插入数据" << endl;
cout << "3.按位置删除数据" << endl;
cout << "4.按数值删除数据" << endl;
cout << "5.输出数据" << endl;
cout << "0.退出程序" << endl;
cout << "输入操作编号:";
cin >> choice; cout << endl;
switch (choice)
{
case 1:Initlist(L); Input(L); break;
case 2:cout << "输入插入的数据:"; cin >> e; cout << endl;
cout << "想在第几个数据后插入:"; cin >> n; cout << endl;
Insertlist(L, e, n); break;
case 3:Dellist(L); break;
case 4:cout << "输入要删除的数值:"; cin >> e; cout << endl;
Dellist2(L, e); Print(L); break;
case 5:Print(L); break;
case 0:break;
default:break;
}
}while(choice!=0);
system("pause");
}
using namespace std;
#include<stdlib.h>
#include <malloc.h>
#include<stdio.h>
#define ok 1
#define error 0
#define List_Init_Size 10
#define CREMENT 10
typedef struct {
int *data;
int length; //数据长度
int Listsize;//容量
}Sqlist;
char *Initlist(Sqlist &L)
{
L.data = (int *)malloc(List_Init_Size*sizeof(int));
if (!L.data)
return "error";
L.length = 0;
L.Listsize = List_Init_Size;
return "ok";
}
void Print(Sqlist L)
{
int n = 1;
cout << "数据表如下:" << endl;//n和数据长度比较
while (n <= L.length)
{
cout << *L.data<<" ";
L.data++;
n++;
}
cout << endl;
}
char *Dellist(Sqlist &L)
{
int n, k = 0;
Sqlist p;
p = L;
cout << "删除第几个数据:";
cin >> n; cout << endl;
if (n <= 0 || n > L.length)
return "error\n";
while (k < L.length)
{
if (k >= n)
*(p.data-1) = *p.data;
p.data++;
k++;
}
--L.length;
return "ok\n";
}
char *Dellist2(Sqlist &L, int e)
{
Sqlist p; int k=0,j,flag=0;
p= L;
for (k; k < p.length; k++)
{
if (p.data[k] == e)
{
for (j = k; j <= L.length-1; j++)
{
p.data[j] =p.data[j+1];
}
L.length--;
k--;
flag = 1;
}
}
if (flag)return "ok";
else return "error";
}
char *Input(Sqlist &L)
{
Sqlist p;
int n,i=1;
p = L;
cout << "输入几个数据:"; cin >> n; cout << endl;
if (n < 1||n>L.Listsize)return "error";
while (i<= n)
{
cout << "请输入第"<<i<<"个数据:";
cin >> *p.data; cout << endl;
p.data++;
L.length++;
i++;
}
return "ok\n";
}
char *Insertlist(Sqlist &L,int e,int n)//插入数据e,n第几个数据后插入
{
Sqlist p;
p = L;
int k = 0;
if (p.length >= p.Listsize)
{
L.data = (int *)realloc(L.data, (List_Init_Size + CREMENT)*sizeof(int));
L.Listsize = List_Init_Size + CREMENT;
}
cout << *p.data<<endl;
p.data += p.length;
while (k<p.length-n) //p移动到n后一位
{
*p.data = *(p.data - 1);
p.data--;
++k;
}
*p.data = e;
L.length++;
return "ok\n";
}
int main()
{
Sqlist L; int e, n,choice;
do{
cout << "1.录入数据" << endl;
cout << "2.插入数据" << endl;
cout << "3.按位置删除数据" << endl;
cout << "4.按数值删除数据" << endl;
cout << "5.输出数据" << endl;
cout << "0.退出程序" << endl;
cout << "输入操作编号:";
cin >> choice; cout << endl;
switch (choice)
{
case 1:Initlist(L); Input(L); break;
case 2:cout << "输入插入的数据:"; cin >> e; cout << endl;
cout << "想在第几个数据后插入:"; cin >> n; cout << endl;
Insertlist(L, e, n); break;
case 3:Dellist(L); break;
case 4:cout << "输入要删除的数值:"; cin >> e; cout << endl;
Dellist2(L, e); Print(L); break;
case 5:Print(L); break;
case 0:break;
default:break;
}
}while(choice!=0);
system("pause");
}