#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType char
#define int Status
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L){
L.elem =(ElemType *)malloc(LIST_INIT_SIZE *sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}//初始化
Status Build(SqList &L) //建立表
{
int i,n;
printf("請輸入元素個數n和n個元素\n");
scanf("%d",&n);
if(n>LIST_INIT_SIZE)//如果n大于當前空間
{
L.elem=(ElemType *)realloc(L.elem,(n+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.listsize=n+LISTINCREMENT;
}
for(i=0;i<n;i++)
scanf("%d",L.elem+i);
L.length=n;
return OK;
}//建立表
Status ListInsert_Sq(SqList &L,int i,ElemType e){
char *newbase,*q,*p;
if(i<1||i>L.length+1)return ERROR;
if(L.length>=L.listsize){
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}//插入操作
Status ListDelete_Sq(SqList&L,int i,ElemType &e){
char *p,*q;
if((i<1)||(i>L.length))return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L.length;
return OK;
}//洗掉操作
Status DestroyList(SqList &L){
if (L.elem) free(L.elem);
printf("完成鏈表記憶體銷毀\n");
getch();
system("cls");
}//銷毀操作
Status ClearList(SqList &L){
if (!L.elem)
return ERROR;
L.length=0;
return OK;
}//重置操作
Status ListEmpty(SqList L){
if(L.elem>0)
printf("順序表為空\n");
else
printf("順序表不為空\n");
}//判斷表空
Status ListLength(SqList L){
int i;
for(i=0;i<L.length;i++)
printf("%d ",i);
}//測量表長
Status GetElem(SqList L,int i,ElemType&e){
if(i<0||i>L.length)
return ERROR;
e=L.elem[i-1];
printf("第i個元素是:%s",e);
}//查找第i個位置的元素并回傳值
Status LocateElem(SqList L,ElemType e){
int i = 0;
while(i<=L.length){
if(L.elem[i] == e)
break;
else
i++; }
if(i<=L.length) return i;
return 0;
}//查找元素位置若無回傳0
Status Print(SqList &L){
int i;
for(i=0;i<L.length;i++)
printf("%d ",*(L.elem+i));
}//輸出順序表
void menu(){
printf("\n\n\n");
printf("\t\t************************************************\n");
printf("\t\t************************************************\n");
printf("\t\t* *\n");
printf("\t\t* *\n");
printf("\t\t* 1、銷毀順序表 *\n");
printf("\t\t* 2、判斷表空 *\n");
printf("\t\t* 3、測量表長 *\n");
printf("\t\t* 4、插入元素 *\n");
printf("\t\t* 5、洗掉元素 *\n");
printf("\t\t* 6、清空序串列 *\n");
printf("\t\t* 7、取順序表指定位置的元素 *\n");
printf("\t\t* 8、輸出順序表 *\n");
printf("\t\t* 9、查找元素所在位置 *\n");
printf("\t\t* 10、查看選單 *\n");
printf("\t\t* 0、退出程式 *\n");
printf("\t\t* *\n");
printf("\t\t************************************************\n");
printf("\t\t*************按 任 意 鍵 進 入 系 統************\n");
printf("\t\t************************************************\n");
getch();
system("cls");
}
void Welcome(){
printf("\n\n\n\n\n\n");
printf("\t\t************************************************\n");
printf("\t\t************************************************\n");
printf("\t\t* *\n");
printf("\t\t* 線性表的順序操作 *\n");
printf("\t\t* *\n");
printf("\t\t************************************************\n");
printf("\t\t*************按 任 意 鍵 進 入 系 統************\n");
printf("\t\t************************************************\n");
getch();
system("cls");
}
int main(){
int i,x,cmd;
SqList L;
ElemType e;
system("color 1D");
Welcome();
menu();
Build(&L);
for (;;)
{
printf("請輸入選擇操作的編號(編號為0~10,10為程式選單)\n");
scanf("%d", &cmd);
switch (cmd)
{
case 1:{
DestroyList( &L);
break;
}
case 2:{
ListEmpty( L);
break;
}
case 3:{
ListLength( L);
break;
}
case 4:{
printf("請輸入要插入的位置與插入元素:");
scanf("%d",&i);
scanf("與%s",&e);
ListInsert_Sq( &L,i, e);
break;
}
case 5:{
printf("請輸入要洗掉的位置:");
scanf("%d",&i);
ListDelete_Sq( &L, i, &e);
break;
}
case 6:{
ClearList( &L);
break;
}
case 7:{
printf("請輸入所取元素的指定位置:");
scanf("%d",i);
GetElem( L, i,&e);
break;
}
case 8:{
Print(&L);
break;
}
case 9:{
printf("請輸入要查找的元素:");
scanf("%s",&e);
LocateElem(L,e);
break;
}
case 10:{
menu();
break;
}
case 0:
{
return 0;//退出程式
}
default:{
printf("請輸入正確序號\n");
break;
}
}
}
return 0;
}
報錯部分c:\users\administrator\desktop\編程實驗\線性表1.cpp(13) : error C2146: syntax error : missing ';' before identifier 'length'
c:\users\administrator\desktop\編程實驗\線性表1.cpp(13) : error C2501: 'Status' : missing storage-class or type specifiers
c:\users\administrator\desktop\編程實驗\線性表1.cpp(13) : error C2501: 'length' : missing storage-class or type specifiers
c:\users\administrator\desktop\編程實驗\線性表1.cpp(14) : error C2146: syntax error : missing ';' before identifier 'listsize'
c:\users\administrator\desktop\編程實驗\線性表1.cpp(14) : error C2501: 'Status' : missing storage-class or type specifiers
c:\users\administrator\desktop\編程實驗\線性表1.cpp(14) : error C2501: 'listsize' : missing storage-class or type specifiers
c:\users\administrator\desktop\編程實驗\線性表1.cpp(16) : error C2146: syntax error : missing ';' before identifier 'InitList_Sq'
c:\users\administrator\desktop\編程實驗\線性表1.cpp(16) : error C2501: 'Status' : missing storage-class or type specifiers
c:\users\administrator\desktop\編程實驗\線性表1.cpp(16) : fatal error C1004: unexpected end of file found
執行 cl.exe 時出錯.
萌新求老哥門指明下改那里怎么改可以運行
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/53493.html
標籤:數據庫及相關技術
上一篇:QT Creator下運行cpp檔案出現不能將引數1從"nullptr"轉換為"const QSharedPointer
