Data structures Doubly Linked List

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 1

  • @busharshaqoor567
    @busharshaqoor567  11 місяців тому

    #include
    using namespace std;
    class MyCharListPtr {
    private:
    struct ListNode
    {
    char item;
    ListNode* previous;
    ListNode* next;
    };
    ListNode* getPointerToNode(int nodeNo) {//2
    if (empty()) { cout next = NULL;
    head->previous = NULL;
    tail = head;
    }
    else {
    slot->previous = tail;
    tail->next = slot;
    tail = slot;
    tail->next =NULL;
    }
    Size++;
    }
    void push_front(char ch) {
    ListNode* slot = new ListNode;
    slot->item = ch;
    if (empty()) {
    head = slot;
    head->next = NULL;
    head->previous = NULL;
    tail = head;
    }
    else {
    slot->next = head;
    head->previous = slot;
    head = slot;
    head->previous = NULL;
    }
    Size++;
    }
    void pop_back() {
    if (empty()) {
    cout next = NULL;
    oldtail->previous = NULL;
    delete oldtail;
    Size--;
    }
    void pop_front() {
    if (empty()) {
    cout previous = NULL;
    oldhead->next = NULL;
    delete oldhead;
    Size--;
    }
    void insert(int nodeNo, char ch) {
    ListNode* slot = new ListNode;
    slot->item = ch;
    if (nodeNo == 0)
    push_front(ch);
    else {
    prev = getPointerToNode(nodeNo - 1);//1
    cur = prev->next;
    prev->next = slot;
    slot->next = cur;
    cur->previous = slot;
    slot->previous = prev;
    if (cur == NULL)
    tail = slot;
    Size++;
    }
    }
    void erase(int nodeNo) {
    if (empty()) {
    cout next;
    prev->next = nextnode;
    nextnode->previous = prev;
    cur->next = cur->previous = NULL;
    delete cur;
    Size--;
    }
    }
    char& front() {
    if (empty()) {
    cout item;
    }
    bool empty() const {
    if (head == NULL)
    return true;
    else
    return false;
    }
    int size() const {return Size;}
    void clear(){
    while (!empty())
    pop_front();
    }
    void display(){
    cout next;
    }
    cout next;
    }
    return *this;
    }
    ~MyCharListPtr(){
    while (!empty())
    pop_front();}
    };
    int main() {
    MyCharListPtr z;
    z.push_back('A');
    z.push_back('B');
    z.push_back('C');
    z.push_front('E');
    z.pop_back();
    z.pop_front();
    z.push_back('C');
    z.push_back('E');
    z.insert(2, 'F');
    z.erase(3);
    }