双链表的建立、测长、打印、删除、插入

来源:互联网 发布:中国石油大学网络教育 编辑:程序博客网 时间:2024/06/06 06:34

C++实现
主要结构与上一篇的单链表相同

#include <iostream>using namespace std;struct node{    int data;    struct node *pre;       //前驱指针    struct node *next;     //后继指针};int n = 0;  //节点个数//链表的创建,尾插法struct node *create(struct node *head){    //p为待插入的节点,q始终指向链表的最后一个节点    struct node *p, *q;    p = q = new node;    cin>>p->data;    p->pre = NULL;    p->next = NULL;    cout<<"输入大于0的值,小于等于0则结束,值的存放地址为:p_addr = "<<p<<endl;    //当输入值<=0时结束输入    while(p->data > 0)    {        if(head == NULL)            head = p;        else        {            q->next = p;            p->pre = q;        }        q = p;        n++;        p = new node;        cin>>p->data;        p->pre = NULL;        p->next = NULL;        cout<<"输入大于0的值,小于等于0则结束,值的存放地址为:p_addr = "<<p<<endl;    }    delete(p);    p = NULL;    q->next = NULL;    cout<<"输入结束"<<endl;    return head;}//链表打印void print(struct node *head){    struct node *p;    p = head;    cout<<"链表打印开始!"<<endl;    cout<<"节点个数为: "<<n<<endl;    while(p != NULL)    {        cout<<"data = "<<p->data<<";地址为:"<<p<<endl;        p = p->next;    }    cout<<"链表打印结束!"<<endl;}//单链表删除指定值节点struct node *del(struct node *head, int num){    struct node *p, *q;    p = head;           //p,q指针指向头节点    cout<<"链表删除开始!"<<endl;    if(head != NULL)        //链表不为空    {        if(p->data == num)          //若头节点为目标节点        {            p = head;            head = head->next;            n--;            delete(p);        }        else        {            while(p->next!=NULL && p->next->data != num)        // p->next为目标节点                p = p->next;            if(p->next != NULL)            {                q = p->next;        //q为待删除节点                q->next->pre = p;                p->next = q->next;                n--;                delete(q);            }            else                cout<<"没有找到目标节点!"<<endl;        }    }    else        cout<<"链表为空!"<<endl;    cout<<"链表删除结束!"<<endl;    return head;}//单链表在指定位置插入(从0开始)struct node *ins(struct node *head){    int index, num;         //待插入节点数据及位置    int temp = 0;    struct node *p, *q;         //p节点为待插入节点,q遍历整个链表    p = new node;    cout<<"输入待插入节点数据及位置:"<<endl;    cin>>num;    cin>>index;    p->data = num;    p->pre = NULL;    p->next = NULL;    q = head;    if(index == 0)      //插入位置为头部    {        head->pre = p;        p->next = head;        head = p;        return head;    }    else    {        //q->next节点为待插入的目标位置        while(q->next!=NULL && temp+1 != index)        {            q = q->next;            temp++;        }        //插入位置在链表中间        if(q->next != NULL && temp+1!=index)        {            p->next = q->next;            p->next->pre = p;            q->next = p;            p->pre = q;            n++;        }        //插入位置在链表尾部        else if(temp+1 == index)        {            q->next = p;            p->pre = q;            n++;        }        else            cout<<"指定位置不合法!"<<endl;    }    return head;}int main(){    struct node *head;    head = NULL;    /***********创建****************/    head = create(head);    /***********打印****************/    print(head);    /***********删除****************/    head = del(head, 1);    print(head);    /***********插入****************/    head = ins(head);    print(head);    return 0;}
0 0
原创粉丝点击