[c]代码库
#include<iostream>
#include<stdio.h>
#include<string.h>
#define max 100
int s[max];//顺序表
void creates(int *ss,int length)//初始化顺序表
{
int value;
printf("请输入%d个数:\n",length);
for(int i = 0; i < length; i++)
{
scanf("%d",&value);
ss[i] = value;
}
}
int searchdex(int *ss, int length, int dex)//查找下标dex个元素 不合法返回-1
{
if(dex < 0 || dex >= length)
{
printf("查找的下标不合法!");
return -1;
}
else
{
return ss[dex];
}
}
int searchnum(int *str,int length,int num)//返回下标 如果没有查到返回-1
{
int cnt = 0;
while(str[cnt] != num && cnt < length)
{
cnt++;
}
if(cnt == length) return -1;
else return cnt;
}
int insert(int *str, int *length,int dex,int num)//擦插入一个元素
{
int cnt ;
if(dex > *length || dex < 0) return -1;
else
{
for(cnt = *length; cnt > dex ; dex--)
str[cnt] = str[cnt-1];
str[dex] = num;
}
*length = *length + 1;
return 1;
}
int del(int *str, int *length)//删除一个数 删除一个数是x的元素 或者是下标为dex的数
{
int ch;
printf("请输入删除的模式: 1 删除一个下标 2 删除一个数 ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("请输入删除的下标:");
int dex;
scanf("%d",&dex);
for(int i = dex; i < *length; i++)
str[i] = str[i+1];
*length = *length +1;
return 1;
}
case 2:
{
printf("请输入删除的数:");
int num;
scanf("%d",&num);
int dex = searchnum(str,*length, num);
for(int i = dex; i < *length; i++)
str[i] = str[i+1];
*length = *length +1;
return 1;
}
}
return 1;
}
void show(int s[], int length )//输出数组
{
printf("数组里的元素为:");
for(int i = 0 ; i < length-1; i++)
printf("%d->",s[i]);
printf("%d\n",s[length-1]);
scanf("\n");
}
int main()
{
int length;
printf("输入线性表的长度:");
scanf("%d:",&length);
creates(s,length);
show(s,length);
return 0;
}
by: 发表于:2017-08-10 09:26:31 顶(0) | 踩(0) 回复
??
回复评论