#include
#include
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;//順序表測試用
const int MaxSize=100; //100只是示例性的數據,可以根據實際問題具體定義
const int Increasement=10;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SeqList;
Status InitList(SeqList &a,int n=MaxSize)
{//初始化線性表
if(n<=0)
n=MaxSize;
a.elem =(ElemType *)malloc(n*sizeof(ElemType));
if(a.elem ==NULL)
exit(ERROR);
a.length =0;
a.listsize =n;
return OK;
}
Status Deletek(SeqList &a,int i,int k) {//從順序存儲結構的線性表a中刪除第i個元素起的k個元素??//注意i的編號從0開始??
int j;
if(ia.length-1||ka.length-i) return ERROR; for(j=0;j<=k;j++)
a.elem[j+i]=a.elem[j+i+k]; a.length=a.length-k; return OK;
}
void main()
{
SeqList La;
ElemType data;
printf("輸入順序表La的元素值:\n");
for(int i=1;i<=10;i++)
{
scanf("%d",&data);
}
InitList(La,10);
Deletek(La,3,5);
}好像main函數有問題,但我不會改
要先InitList然後輸入元素,刪除元素的時候後面的依次移位上來
#include<iostream>
using namespace std;
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;//順序表測試用
const int MaxSize=100; //100只是示例性的數據,可以根據實際問題具體定義
const int Increasement=10;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SeqList;
Status InitList(SeqList &a,int n=MaxSize)
{//初始化線性表
if(n<=0)
n=MaxSize;
a.elem =(ElemType *)malloc(n*sizeof(ElemType));
if(a.elem ==NULL)
exit(ERROR);
a.length =0;
a.listsize =n;
return OK;
}
Status Deletek(SeqList &a,int i,int k) {//從順序存儲結構的線性表a中刪除第i個元素起的k個元素??//注意i的編號從0開始??
int j;
if(i>=a.length-1||k>=a.length-i)
return ERROR;
for(j=i+k;j<=a.length;j++)
{
a.elem[i++] = a.elem[j];
}
a.length=a.length-k;
return OK;
}
void main()
{
SeqList La;
ElemType data;
InitList(La,10);
printf("輸入順序表La的元素值:\n");
for(int i=1;i<=10;i++)
{
scanf("%d",&data);
La.elem[i - 1] = data;
La.length++;
}
Deletek(La,3,5);
printf("刪除後元素值:\n");
for(int i=0;i<La.length;i++)
{
printf("%d ", La.elem[i]);
}
printf("\n");
}