用单向链表构造队列.cpp

/*用单向链表构造队列的类,实现队列的插入/删除/查看等基本功能
 * 队列的特点:先进先出(FIFO)
 */
#include <iostream>
using namespace std;

template <typename T>
class Queue {
    struct Node {
        Node(const T& d, Node* n) : data(d), next(n) { }
        T data;
        Node* next;
    };
public:
    Queue() : header(NULL), len(0) { }
    //析构函数,保证删除每一个节点
    ~Queue()
    {
        Node* p = header;
        while (p != NULL) {
            Node* t = p;
            p = p->next;
            delete t;
        }
    }
//入队操作,每次入队,将数据加入队尾
    void push(const T& value)
    {
        header = new Node(value, header);
        len++;
    }
//出队操作,每次出队即删除第一个元素
    void pop()
    {
        if (header == NULL) {
            return ;
        }

        Node* prev = NULL;
        Node* curr = header;

        while (curr->next != NULL) {
            prev = curr;
            curr = curr->next;
        }
        if (prev != NULL) {
            prev->next = NULL;
        } else {
            header = NULL;
        }
        delete curr;
        len--;
    }
//查看队首(因为链表用尾插法,链表的尾是队列的头)
    T& front()
    {
        Node* p = header;
        while (p->next != NULL) {
            p = p->next;
        }
        return p->data;
    }
//查看队尾(因为链表用尾插法,链表的头是队列的尾)
    T& back()
    {
        return header->data;
    }
//查看队列长度
    int size()
    {
        return len;
    }
//判断队列是否为空,是返回true(1)
    bool empty()
    {
        return len == 0;
    }

private:

    Node* header;
    int len;
};


int main()
{
//对模板采取int作为例子
    Queue<int> q;
    q.push(5);
    q.push(3);
    q.push(7);

    cout << "front : " << q.front() << endl;
    cout << "back  : " << q.back() << endl;
    cout << "size  : " << q.size() << endl;

    q.pop();
    cout << "----------------" << endl;

    cout << "front : " << q.front() << endl;
    cout << "back  : " << q.back() << endl;

    cout << "size  : " << q.size() << endl;
    cout << "empty : " << q.empty() << endl;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是遍历链表的.h和.cpp文件的代码,可以使用switch case语句输出: 链表头文件 LinkedList.h: ```c++ #ifndef LINKEDLIST_H #define LINKEDLIST_H #include <iostream> struct Node { int data; Node* next; }; class LinkedList { public: LinkedList(); ~LinkedList(); void insert(int value); void remove(int value); void print(); private: Node* head; }; #endif // LINKEDLIST_H ``` 链表源文件 LinkedList.cpp: ```c++ #include "LinkedList.h" LinkedList::LinkedList() { head = nullptr; } LinkedList::~LinkedList() { Node* current = head; while (current != nullptr) { Node* next = current->next; delete current; current = next; } } void LinkedList::insert(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = head; head = newNode; } void LinkedList::remove(int value) { Node* current = head; Node* previous = nullptr; while (current != nullptr) { if (current->data == value) { if (previous == nullptr) { head = current->next; } else { previous->next = current->next; } delete current; return; } previous = current; current = current->next; } } void LinkedList::print() { Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ``` 使用switch case输出链表.h和.cpp文件的代码: ```c++ #include <iostream> #include <fstream> #include <string> int main() { std::ifstream hFile("LinkedList.h"); std::ifstream cppFile("LinkedList.cpp"); std::string line; int fileIndex = 0; while (getline(hFile, line)) { switch (fileIndex) { case 0: std::cout << "// 链表头文件 LinkedList.h:" << std::endl; break; case 1: std::cout << "// 链表源文件 LinkedList.cpp:" << std::endl; break; } std::cout << line << std::endl; if (line.find("#endif") != std::string::npos) { ++fileIndex; } } while (getline(cppFile, line)) { switch (fileIndex) { case 1: std::cout << "// 链表头文件 LinkedList.h:" << std::endl; break; case 2: std::cout << "// 链表源文件 LinkedList.cpp:" << std::endl; break; } std::cout << line << std::endl; if (line.find("void LinkedList::print()") != std::string::npos) { ++fileIndex; } } hFile.close(); cppFile.close(); return 0; } ``` 其中,fileIndex变量表示当前正在处理的文件的索引。当读取到`.h`文件的`#endif`预处理指令时,fileIndex加1,表示开始读取`.cpp`文件;当读取到`.cpp`文件的`void LinkedList::print()`函数定义时,fileIndex再次加1,表示`.cpp`文件处理完毕。在switch case语句中,根据fileIndex的值输出对应的注释和代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值