1005: 单向链表(2)

1005: 单向链表(2)

Description

​ 已知一个有序链表类LinkSortList及main函数的部分代码如下,请完成LinkSortList类的成员函数,得到对应的运行结果,勿改动main函数。

// 有序表类
template <class T>
class LinkSortList
{
public:
    LinkSortList(); // 建立只有头结点的空链表
    ~LinkSortList(); // 析构函数
    void Insert(T x); // 在有序单链表中插入元素x使序列仍有序
    int Length(); // 求表长
    int DeleteOdd(); // 在单链表中删除所有奇数,返回值为奇数个数
    void DispList(); // 遍历有序单链表,按序号依次输出各元素
private:
    Node<T> *first; // 单链表的头指针
};

int main()
{
    LinkSortList<int> sa;
    int x;
    while (1)
    {
        cin >> x;
        if (!x)
            break;
        sa.Insert(x);
    }
    sa.DispList();
    int count = sa.DeleteOdd();
    cout << "Count of deleted odds:" << count << endl;
    sa.DispList();
    return 0;
}

Input

Output

Sample Input

42 5 32 56 34 7 233 1 0

Sample Output

The length:8
The elements:1 5 7 32 34 42 56 233 
Count of deleted odds:4
The length:4
The elements:32 34 42 56 
posted @ 2023-06-06 16:30  杨与S8  阅读(14)  评论(0编辑  收藏  举报