求C语言完整代码下载,一定是C语言,不要是C++啊!
求C语言完整代码下载,一定是C语言,不要是C++啊!
补充:我的邮箱346624712@qq.com请各位高手帮助哈,小弟不胜感激。
追问:编辑代码
求C语言完整代码下载,一定是C语言,不要是C++啊!
补充:我的邮箱346624712@qq.com请各位高手帮助哈,小弟不胜感激。
追问:编辑代码
答案:#include <cstdio>
#include <cstdlib>
#include <cstring>#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0#define MAXSIZE 100
typedef int DataType;
typedef struct
{
DataType data[MAXSIZE];
int length;
} SeqList;int InitList ( SeqList &L )
{ // 初始化线性表L
L.length = 0;
return OK;
}int ListEmpty ( SeqList L )
{ // 检查顺序表是否为空if ( L.length == 0 )
return TRUE;
else
return FALSE;
}
int ListFull ( SeqList L )
{ //检查顺序表是否为满
if ( L.length == MAXSIZE ) {
printf ( "表已满!\n" );
return TRUE;
}
else
return FALSE;
}int ListTraverse ( SeqList L )
{ //输出线性表元素
int i;
printf ( "线性表为:" );
for ( i = 0; i < L.length; i++ )
printf ( "%d ", L.data[i] );
printf ( "\n" );
return OK;
}int ClearList ( SeqList &L )
{ // 清空线性表L
int i ;
for ( i = 0; i < L.length; i++ )
L.data[i] = NULL;
L.length = 0;
printf ( "线性表已清空!\n" );
return OK;
}int GetLength ( SeqList L )
{ // 求线性表L的长度
return ( L.length );
}int IsEmpty ( SeqList L )
{ // 判断线性表L是否为空
if ( L.length == 0 )
return TRUE;
else
return FALSE;
}int ListInsert ( SeqList &L, int i, int e )
{ //在线性表L中第i个数据元素之前插入数据元素eif ( ListFull ( L ) )
return ERROR;
if ( i < 1 || i > L.length + 1 )
return ERROR;
int j;
for ( j = L.length - 1; j >= i - 1; j-- )
L.data[j+1] = L.data[j];
L.data[i-1] = e;
L.length++;
return OK;
}int ListDelete ( SeqList &L, int i, int &e )
{ // 将线性表L中第i个数据元素删除
if ( IsEmpty ( L ) )
return ERROR;
if ( i < 1 || i > L.length )
return ERROR;
e = L.data[i-1];
int j;
for ( j = i; j <= L.length - 1; j++ )
L.data[j-1] = L.data[j];
L.length--;
return OK;
}int LocateELem ( SeqList L, int e )
{ //从顺序表中查找与给定元素值相同的元素在顺序表中的位置int i;
if ( IsEmpty ( L ) ) {
printf ( "表为空!\n" );
return ERROR;
}
for ( i = 0; i < L.length; i++ )
if ( L.data[i] == e )
return i + 1;
return 0;
}int ReBuild ( SeqList &L )
{
ClearList ( L );
int i, n, e;
printf ( "输入线性表长度:" );
scanf ( "%d", &n );
printf ( "输入数据元素:\n" );
for ( i = 1; i <= n; i++ )
{
scanf ( "%d", &e );
ListInsert ( L, i, e );
}
ListTraverse ( L );
return OK;
}
int ListPrior ( SeqList L, int e )
{ //求顺序表中元素的前驱
int i;
for ( i = 1; i <= L.length; i++ )
if ( L.data[i] == e )
return L.data[i - 1];
return FALSE;
}
int ListNext ( SeqList L, int e )
{ //求顺序表中元素的后继
int i;
for ( i = 0; i < L.length; i++ )
if ( L.data[i] == e )
return L.data[i + 1];
return FALSE;
}
int GetElem ( SeqList L, int i, int &e )
{ // 获取线性表L中的第i个数据元素的内容
if ( i < 1 || i > L.length )
return ERROR;
e = L.data[i-1];
return e;
}
int ListMod ( SeqList &L, int n, int e )
{
L.data[n - 1] = e;
return OK;
}
int compare ( const void * a, const void * b )
{
return ( * ( int* ) a - * ( int* ) b );
}
void SortList ( SeqList &L )
{
qsort ( L.data, L.length, sizeof ( int ), compare );
}
int Handle ( SeqList &L )
{
int n, i, e;
char c = 'y';
while ( c == 'y' ) //选择操作
{
system ( "cls" );
printf ( "←↑→←→←→←→←→←→←→←↑→\n" );
printf ( " 1.插入元素:\n" );
printf ( " 2.删除元素:\n" );
printf ( " 3.修改元素:\n" );
printf ( " 4.查找元素前驱:\n" );
printf ( " 5.查找元素后继: \n" );
printf ( " 6.返回表中某元素:\n" );
printf ( " 7.查找表中某元素位置:\n" );
printf ( " 8.清空当前线性表:\n" );
printf ( " 9.当前线性表长度:\n" );
printf ( "10.当前线性表排序:\n" );
printf ( "11.清空顺序表并重建:\n" );
printf ( "12.输出当前线性表:\n" );
printf ( "←↑→←→←→←→←→←→←→←↑→\n" );
printf ( "请选择需要的操作:" );
scanf ( "%d", &n );
if ( n < 1 || n > 12 )
{
printf ( "输入错误,请重新输入:\n" );
scanf ( "%d", &n