請各位大佬幫忙,指點指點小弟,謝謝了(代碼應該一個打不全,我放在評論里了)現在這個是功能檔案
/*************************************************************
順序表的實作之增刪功能 實作檔案
更新于2020年4月13日
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "Seqlist.h"
void SL_Initiate(SqList &L)
// 順序表的初始化,即構造一個空的順序表
{
L.elem = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
L.length=0;
}
void SL_Free(SqList &L)
// 釋放順序表
{
free(L.elem);
}
bool SL_IsEmpty(SqList L)
// 判斷順序表是否空
{
return L.length==0;
}
bool SL_IsFull(SqList L)
// 判斷順序表是否滿
{
return L.length==MAXSIZE;
}
void SL_Create(SqList &L,int n)
// 輸入n個資料元素,創建一個順序表L
{
int i;
L.length=n;
for(i=0; i<n; i++)
scanf("%d", &L.elem[i]);
}
void SL_Print(SqList L)
// 輸出整個順序表
{
if (L.length==0)
{
printf("The slist is empty.\n");
return ;
}
for (int i=0; i<L.length; i++)
printf("%d ", L.elem[i]);
printf("\n");
}
void SL_InsAt(SqList &L, int i, ElemType e)
// 在順序表的第i個位置插入新元素e, 即在元素L.elem[i-1]之前插入
// i的有效范圍[1,L.length+1]
{
// 請在這里補充代碼,完成本關任務
/********** Begin *********/
SqList *a;
a=&L;
int j,k=99;
for(j=MAXSIZE;j>i-1;j--)
{
a->elem[j]=a->elem[k];
k--;
}
a->elem[k+1]=e;
/********** End **********/
}
void SL_DelAt(SqList &L, int i)
// 洗掉順序表L的第i個元素
//i的有效范圍[1,L.length]
{
// 請在這里補充代碼,完成本關任務
/********** Begin *********/
SqList *a;
a=&L;
int k=i,j;
for(j=i-1;j<99;j++)
{
a->elem[j]=a->elem[k];
k++;
}
/********** End **********/
}
void SL_DelValue(SqList &L, ElemType x)
// 洗掉第一個值為x的元素
{
// 請在這里補充代碼,完成本關任務
/********** Begin *********/
SqList *a=&L;
int i,j;
for(i=0;i<MAXSIZE;i++)
{
if(a->elem[i]==x)
{
for(j=i;j<99;j++)
a->elem[j]=a->elem[j+1];
return ;
}
}
/********** End **********/
}
uj5u.com熱心網友回復:
主函式#include <stdio.h>
#include <stdlib.h>
#include "Seqlist.h"
//#include "Seqlist.cpp"
int main()
{
SqList L;int n,i,x;
SL_Initiate(L);
scanf("%d", &n);//輸入元素的個數
SL_Create(L, n);
scanf("%d%d", &i,&x);//輸入待插入的位置和待插入元素的值
SL_InsAt(L, i, x);
scanf("%d", &i);//輸入待洗掉元素的位置
SL_DelAt(L, i);
scanf("%d", &x);//輸入待洗掉元素的值
SL_DelValue(L, x);
SL_Print(L);
SL_Free(L);
}
uj5u.com熱心網友回復:
頭檔案/*************************************************************
順序表的實作 頭檔案
**************************************************************/
#define MAXSIZE 100 //最大長度
typedef int ElemType;
typedef struct {
ElemType *elem; //指向資料元素的起始地址
int length; //線性表的當前長度
}SqList;
//declaration of functions:
// 順序表的初始化,即構造一個空的順序表
void SL_Initiate(SqList &L);
// 釋放順序表
void SL_Free(SqList &L);
// 判斷順序表是否空
bool SL_IsEmpty(SqList L);
// 判斷順序表是否滿
bool SL_IsFull(SqList L);
// 輸入n個資料元素,創建一個順序表L
void SL_Create(SqList &L,int n);
// 輸出整個順序表
void SL_Print(SqList L);
// 獲取順序表L的第i個元素賦給e,i的有效范圍[1,L.length]。
void SL_GetAt(SqList L, int i, ElemType &e);
// 在順序表L中查找第一個值為x的元素,
// 找到則回傳該元素在表中的位置,否則回傳0。
int SL_FindValue(SqList L, ElemType x);
// 在順序表的第i個位置插入新元素e, 即在元素L.elem[i-1]之前插入
//i的有效范圍[1,L.length+1]
void SL_InsAt(SqList &L, int i, ElemType e);
// 洗掉順序表L的第i個元素
//i的有效范圍[1,L->length]
void SL_DelAt(SqList &L, int i);
// 洗掉第一個值為x的元素
void SL_DelValue(SqList &L, ElemType x);
uj5u.com熱心網友回復:
為什么小弟運行程式以后程式最后會多出一個0 當我輸入這串資料它就多出一個06 5 9 11 31 9 88 2 10 7 9
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/269667.html
標籤:C語言
上一篇:多載賦值運算子函式
下一篇:編譯器不認識我的類
