天天看点

基于c++的单链表,双向链表操作以及环

1:单向链表:生成,头插入,尾插入,某个元素后面插入,删除某元素

#include<stdio.h>
using namespace std;
struct Node{
    int data;
    Node* next;
};
Node* create(){
    Node *h=NULL,*p=NULL,*tmp=NULL;
    int a;
    scanf("%d",&a);
    while(a!=0){
        tmp=new Node();
        tmp->data=a;
        tmp->next=NULL;
        if(h==NULL){
            h=tmp;
            p=h;
        }    
        else{
            p->next=tmp;
            p=tmp;
        }
        scanf("%d",&a);
    }
    return h;
}
void addAfter(Node* head,int data,int afdata){
    Node *p=head;
    while(p!=NULL){
        if(p->data==afdata){
            Node *tmp=new Node();
            tmp->data=data;
            tmp->next=p->next;
            p->next=tmp;     
            break;
        }
        else{
            p=p->next;
        }
    }
}
Node* deleteData(Node* head,int data){
    Node *p=head;
    if(p!=NULL && p->data==data){
        if(p->next!=NULL){
            head=p->next;
            return head;
        }
        else{
        return NULL;   
        }
    }
    while(p->next!=NULL){
        if(p->next->data==data){
            if(p->next->next){
                p->next=p->next->next;     
            }
            else{
                p->next=NULL;
            }
            break;
        }
        else{
            p=p->next;
        }
    }
    return head;
}

void addTail(Node* head,int data){
    Node* p=head;
    while(p->next!=NULL){
        p=p->next;
    }
    Node *tmp=new Node();
    tmp->data=data;
    tmp->next=NULL;
    p->next=tmp;
}
Node* addHead(Node* head,int data){
    Node *tmp=new Node();
    tmp->data=data;
    tmp->next=head;
    head=tmp;
    return head;
}
void show(Node* head){
    Node* p=head;
    while(p){
        printf("%d,",p->data);
        p=p->next;
    }
    printf("\n");

}

int main(){
    Node *head=create();
    show(head);
    addTail(head,9);
    show(head);
    head=addHead(head,1);
    show(head);
    addAfter(head,2,9);
    show(head);
    head=deleteData(head,2);
    show(head);
    return 1;
}
           

:2:双向链表操作::生成,正序/逆序遍历,头插入,尾插入,某个元素后面插入,删除某元素

#include<stdio.h>
using namespace std;
struct Node{
    int data;
    Node* next;
    Node* pre;
};
Node* create(){
    Node *h=NULL,*p=NULL,*tmp=NULL;
    int a;
    scanf("%d",&a);
    while(a!=0){
        tmp=new Node();
        tmp->data=a;
        tmp->next=NULL;
        tmp->pre=NULL;
        if(h==NULL){
            h=tmp;
            p=h;
        }    
        else{
            p->next=tmp;
            tmp->pre=p;
            p=tmp;
        }
        scanf("%d",&a);
    }
    return h;
}
void addAfter(Node* head,int data,int afdata){
    Node *p=head;
    while(p!=NULL){
        if(p->data==afdata){
            Node *tmp=new Node();
            tmp->data=data;
            tmp->next=p->next;
            tmp->pre=p;
            p->next=tmp;     
            break;
        }
        else{
            p=p->next;
        }
    }
}
Node* deleteData(Node* head,int data){
    Node *p=head;
    if(p!=NULL && p->data==data){
        if(p->next!=NULL){
            head=p->next;
            p->pre=NULL;
            return head;
        }
        else{
        return NULL;   
        }
    }
    while(p->next!=NULL){
        if(p->next->data==data){
            if(p->next->next){
                p->next->next->pre=p;
                p->next=p->next->next;     
            }
            else{
                p->next=NULL;
            }
            break;
        }
        else{
            p=p->next;
        }
    }
    return head;
}

void addTail(Node* head,int data){
    Node* p=head;
    while(p->next!=NULL){
        p=p->next;
    }
    Node *tmp=new Node();
    tmp->data=data;
    tmp->next=NULL;
    tmp->pre=p;
    p->next=tmp;
}
Node* addHead(Node* head,int data){
    Node *tmp=new Node();
    tmp->data=data;
    tmp->next=head;
    head->pre=tmp;
    head=tmp;
    return head;
}
void show(Node* head){
    Node* p=head;
    while(p){
        printf("%d,",p->data);
        p=p->next;
    }
    printf("\n");

}
void upshow(Node* head){
    Node* p=head;
    while(p->next){
        p=p->next;
    }
    while(p){
        printf("%d,",p->data);
        p=p->pre;
    }
    printf("\n");

}


int main(){
    Node *head=create();
    show(head);
    upshow(head);
    addTail(head,9);
    show(head);
    head=addHead(head,1);
    show(head);
    addAfter(head,2,9);
    show(head);
    head=deleteData(head,2);
    show(head);
    return 1;
           

3:判断链表是否有环

Node* IsLoop(Node *head)  
{  
    Node*pslow=head,*pfast=head;   
    while(pslow!=NULL && pfast!=NULL)  
    {  
        pslow=pslow->next;  
        pfast=pfast->next->next;  
        if(pslow==pfast)  
           return pslow;  
    }  
    return NULL;  
}
           

4:判断链表环人长度

int  GetRingLen(Node *meeting)  
{  
    Node *pslow=meeting,*pslow=meeting;  
    int len=0;
    while(pslow!=NULL && pfast!=NULL)  
    {  
        pslow=pslow->next;  
        pfast=pfast->next->next;  
        len++;
        if(pslow==pfast)  
           return len;  
    }  
    return 0;  
}
           

继续阅读