数据结构严蔚敏版本课后题?

发布网友

我来回答

2个回答

热心网友

代码地址

#include <iostream>


using namespace std;


//顺序表的存储结构

#define MAXSIZE 100    //顺序表可能达到的最大长度

typedef int ElemType;  //自定义数据元素类型


typedef struct

{

    ElemType *elem;    //存储空间的基地址

    int length;        //当前长度,数据元素的个数

}SqList;               //顺序表的结构类型为SqList


//初始化,构造一个空的顺序表L,并输入数据

void InitList(SqList &L)

{

    int len;

L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间

    if(!L.elem)

    {

        cout << "Error" << endl;

        return;

    }


    cout << "Please input the length of the linear table:" ;

    cin >> len;

    L.length = len;

    cout << "Please input the elements of the linear table:";

    for(int i=0;i<len;i++)

        cin >> L.elem[i];

}


//删除值为item的元素

void ListDelete(SqList &L,int item)

{

    int j = 0;

    for(int i=0; i<L.length;i++)

    {

        if(L.elem[i] != item)

        {

            L.elem[j] = L.elem[i];

            j++;

        }

    }

    L.length = j;

}


int main()

{

    SqList L;

    int item;

    InitList(L);

    cout << "Please input the value of item is:";

    cin >> item;

    ListDelete(L,item);


    cout << "After Deletion:" << endl;

    for(int i=0;i<L.length;i++)

        cout << L.elem[i] << " ";


    return 0;

}

热心网友

!,??。!。?。??追答9787979439

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com