大连理工大学C语言题目(十七)

87 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1.链表-双链表操作 【习题描述】 习题要求:

   双链表的定义、插入、删除、查找操作,根据习题中的TODO说明进行代码编写

用例:

假如输入为:1 2 3 3 3 0 9 3

则输出为:

输入链表,以0为结尾:1 2 3 3 3

1 2 3 3 3 9

1 2 9

#include <iostream>
using namespace std;

template <class T>
class DLLNode {
public:
    T data;
    DLLNode<T>* next;
    DLLNode<T>* prev;

    DLLNode(const T info, DLLNode<T>* prevVal = NULL, DLLNode<T>* nextVal = NULL)
    {
        data = info;
        prev = prevVal;
        next = nextVal;
    }
};

template <class T>
class DLLinkList {
public:
    DLLNode<T>*head, *tail;
    DLLinkList()
    {
        head = tail = NULL;
    }

    /*
	TODO: 在双链表的表尾插入值为num的DLLNode结点,注意考虑空链表场景的插入操作
	 */
    void Insert(const T& num)
    {
        DLLNode<T>*p=new DLLNode<T>(num);
        if (head&&tail){
            tail->next=p;
            p->prev=tail;
            tail=p;
        } else{
            head=p;
            tail=p;
        }
    }

    //构建双链表
    void createList()
    {
        cout << "输入链表,以0为结尾:";
        T i;
        cin >> i;
        while (i != 0 && i != '0') {
            Insert(i);
            cin >> i;
        }
    }

    /*
	TODO:查找值为value的结点,返回第一个value值的结点,若不存在则返回NULL
	*/
    DLLNode<T>* findValue(const T& value)
    {
        DLLNode<T>*p=head;
        while (p){
            if (p->data==value)
                return p;
            p=p->next;
        }
        return NULL;
    }

    /*
	TODO:删除所有值为value的结点,如果未找到,则打印cout << "不存在值为value的结点" << endl;并返回。
	 */
    void deleteValue(const T& value)
    {
        DLLNode<T>*p=head;
        bool flag= true;
        while (p){
            if (p->data==value){
                if (p->prev&&p->next){
                    p->prev->next=p->next;
                    p->next->prev=p->prev;
                } else if (p->prev){
                    tail=p->prev;
                    p->prev->next= nullptr;
                } else if (p->next){
                    head=p->next;
                    p->next->prev= nullptr;
                } else{
                    head= nullptr;
                    tail= nullptr;
                }
                flag= false;
            }
            p=p->next;
        }
        if (flag){
            cout << "不存在值为value的结点" << endl;
        }
    }
    //打印整个链表
    void printList()
    {
        DLLNode<T>* p = head;
        while (p != NULL) {
            cout << p->data << " ";
            p = p->next;
        }
        cout << endl;
    }
};

int main()
{
    int value1, value2;
    DLLinkList<int> DLL;
    DLL.createList();
    DLL.printList();
    cin >> value1 >> value2;
    DLL.Insert(value1); //插入value1
    DLL.printList();
    DLL.deleteValue(value2); //删除value2
    DLL.printList();
}

2.最小堆的初始化、插入、根结点删除(具体函数实现在下一节)

#include <iostream>
using namespace std;
class MinHeap{
    int*heap;
    int maxSize,currentSize;
public:
    MinHeap(const int*array,int maxArraySize,int currentArraySize):maxSize(maxArraySize),currentSize(currentArraySize){
        heap=new int[maxSize]();
        for (int i = 0; i < currentSize; ++i) {
            heap[i]=array[i];
        }
        int temp;
        for (int i = currentSize/2-1; i >= 0; i--) {
            for (int j = i; j <= currentSize/2-1; j++) {
                if (2*j+2<currentSize){
                    if (heap[j]>heap[2*j+1]||heap[j]>heap[2*j+2]){
                        if (heap[2*j+1]>heap[2*j+2]){
                            temp=heap[j];
                            heap[j]=heap[2*j+2];
                            heap[2*j+2]=temp;
                        } else{
                            temp=heap[j];
                            heap[j]=heap[2*j+1];
                            heap[2*j+1]=temp;
                        }
                    }
                } else{
                    if (heap[j]>heap[2*j+1]){
                        temp=heap[j];
                        heap[j]=heap[2*j+1];
                        heap[2*j+1]=temp;
                    }
                }
            }
        }
    }

int main() {
    int a[10]={20,12,35,15,10,80,30,17,2,1};
    MinHeap*minHeap=new MinHeap(a,100,10);
    minHeap->PrintHeap();
    minHeap->InsertData(0);
    minHeap->PrintHeap();
    minHeap->DeleteTopData();
    minHeap->PrintHeap();
    return 0;
}