Untitled
raw download clone
TEXT
views 6
,
size 7289 b
#include <iostream>
#include <string.h>
using namespace std;


class node {
    friend class linklist;
public:

    node(int Data) {                /*constructor:create a new node*/
        data = Data;
        next = nullptr;
    }

private:
    int data;
    node* next;
};

class linklist {
public:
    linklist() {
        head = nullptr;
    }
    
    int Judge(int K) {
        node* temp = head;
        if (temp!=nullptr) {
            while (temp->data != K) {
                temp = temp->next;
                if (temp==nullptr) {
                   break;
                }
            }
            if (temp!=nullptr) {
                return 1;
            }
        }
        return 0;
    }
    
    node* traverse(int K) {
        node* temp = head;
        if (temp != nullptr) {
            while (temp->data != K) {
                temp = temp->next;
                if (temp == nullptr) {
                    break;
                }
            }
            if (temp != nullptr) {
                return temp;
            }
        }
        return nullptr;
    }

    node* traverse(int K,int *time) {
        node* temp = head;
        if (temp!=nullptr) {
            while (temp->data != K) {
                temp = temp->next;
                (*time)++;
                if (temp==nullptr) {
                    break;
                }
            }
            if (temp != nullptr) {              
                    return temp;              
            }
        }
        return nullptr;
    }

    void Print() {
        node* temp=head;
        while (temp!=nullptr) {
            printf("%d ",temp->data);
            temp = temp->next;
        } 
        printf("\n");
    }
     
    void InsertFront(int NewData) {
        if (Judge(NewData)) {
            return;
        }
        node* A=new node(NewData);
        A->next = head;
        head = A;
    }

    void InsertBack(int NewData) {
        if (Judge(NewData)) {
            return;
        }
        node* A = new node(NewData);
        if (head == nullptr) { head = A; }
        else {
            node* temp = head;
            while (temp->next!=nullptr) {
                temp = temp->next;
            }
            temp->next = A;
        }
       }
    

    void InsertAfter(int K,int NewData) {
        if (Judge(NewData)) {
            return;
        }
        node* temp=traverse(K);
        if(temp!=nullptr){
            node* t=temp->next; 
            temp->next=new node(NewData); 
            temp=temp->next; 
            temp->next=t;
        } 
    }
    void InsertBefore(int K,int NewData) {
        if (Judge(NewData)) {
            return;
        }
        node* temp = head;
        if (temp->data!=K) {
            node* t = temp->next;
            if (t!=nullptr) {
                while (t->data != K) {
                    t = t->next;
                    temp = temp->next;
                    if (t == nullptr) {
                        break;
                    }
                }
                if (t != nullptr) {
                    temp->next = new node(NewData);
                    temp = temp->next;
                    temp->next = t;
                }
            }
        }
        else {
            InsertFront(NewData);
        }   
    }
    void DeleteFront() {
        if (head!=nullptr) {
            node* temp = head->next;
            if (temp!=nullptr) {
                delete head;
                head = temp;
            }
            else {
                head = nullptr;
            }
        }
    }
    void DeleteBack() {
        node* temp = head;
        if (head!=nullptr) {
            if (head->next!=nullptr) {
                node* t = temp->next;
                while (t->next != nullptr) {
                    t = t->next;
                    temp = temp->next;
                }
                temp->next = nullptr;
                delete t;
            }
            else {
                head = nullptr;
            }
        }
    }
    void Delete(int K) {
        node* temp=head;    
        if (temp!=nullptr) {
            node* t = temp->next;
            if (temp->data == K) {
                delete temp;
                head = t; 
            }
            else {               
                while (t->data != K && t != nullptr) {
                    temp = t;
                    t = t->next;
                }
                if (t != nullptr) {
                    temp->next= t->next;
                    delete t;
                }
            }
        }
    }
    void Reverse(int K,int J) {
        int time[2] = { 0 };
        node* k = traverse(K,&time[0]);
        node* j = traverse(J,&time[1]);
        node* temp = head;
        if (k!=nullptr&&j!=nullptr&&time[0]<time[1]) {
            node* prev = k->next;
            if (k!=head) {
                while (temp->next != k) {
                    temp = temp->next;                     
                }
                temp->next = j;
            }
            else {
                head = j;
            }
            k->next = j->next;
            while(prev!=j){          
                temp = prev->next;
                prev->next = k;         
                k = prev;
                prev = temp;           
             } 
             j->next = k;
        } 
    }

    void Clear() {
        node* temp=head;
        if (head!=nullptr) {
            node* t = temp->next;
            while (t != nullptr) {
                delete temp;
                temp = t;
                t = t->next;
            }
            delete temp;
        }
        head = nullptr;
    }

private:
    node* head;
};


int main()
{                      
    linklist A;
    int scan1,scan2;
    char choose[15];

    while (scanf("%s", choose)!=EOF) {    

        if (strcmp(choose, "InsertFront") == 0) {
            scanf_s("%d", &scan1);
            A.InsertFront(scan1);
        }
        else if (strcmp(choose, "InsertBack") == 0) {
            scanf("%d", &scan1);
            A.InsertBack(scan1);
        }
        else if (strcmp(choose, "InsertAfter") == 0) {
            scanf("%d%d", &scan1, &scan2);
            A.InsertAfter(scan1, scan2);
        }
        else if (strcmp(choose, "InsertBefore") == 0) {
            scanf("%d%d", &scan1, &scan2);
            A.InsertBefore(scan1, scan2);
        }
        else if (strcmp(choose, "Reverse") == 0) {
            scanf("%d%d", &scan1, &scan2);
            A.Reverse(scan1, scan2);
        }
        else if (strcmp(choose, "Delete") == 0) {
            scanf("%d", &scan1);
            A.Delete(scan1);
        }
        else if (strcmp(choose, "DeleteFront") == 0) {
            A.DeleteFront();
        }
        else if (strcmp(choose, "DeleteBack") == 0) {
            A.DeleteBack();
        }
        else if (strcmp(choose, "Clear") == 0) {
            A.Clear();
        }
        else if (strcmp(choose, "Print") == 0) {
            A.Print();
        }
    }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.