• 双链表基本操作


    #include <cstdlib>
    #include <iostream>
    #include <stdio.h>
    #include <conio.h> 
    /*c++实现双链表的基本操作*/
    using namespace std;
    typedef struct student
    {
            int data;
            struct student *pre;
            struct student *next;
    }dnode;
    //创立链表 
    dnode *creat()
    {
          dnode *head,*p,*s;
          int x,cycle=1;
          head=(dnode *)malloc(sizeof(dnode));
          p=head;
          while(cycle)
          {
                      cout<<"please input the data!"<<endl;
                      cin>>x;
                      if(x!=0)
                      {
                              s=(dnode *)malloc(sizeof(dnode));
                              s->data=x;
                              p->next=s;
                              s->pre=p;
                              p=s;
                      }
                      else
                      cycle=0;
          }
          head=head->next;
          head->pre=NULL;
          p->next=NULL;
          return head;
    }
    //打印链表 
    void print(dnode *head)
    {
         dnode *p,*s;
         p=head;
         cout<<"正序打印双链表:"<<endl; 
         while(p->next!=NULL)
         {
               cout<<p->data<<" ";
               p=p->next;
         }
         cout<<p->data<<endl;
         cout<<"逆序打印双链表:"<<endl;
         while(p->pre!=NULL)
         {
                cout<<p->data<<" ";
                p=p->pre;
         }               
         cout<<p->data<<endl; 
         
    }
    //双链表插入节点
    dnode *insert(dnode *head,int num)
    {
          dnode *p0,*p1;
          p1=head;
          p0=(dnode *)malloc(sizeof(dnode));
          p0->data=num;
          while(p0->data>p1->data&&p1->next!=NULL)
          {
                p1=p1->next;
          }
          if(p0->data<=p1->data)
          {
                 if(head==p1)
                 {
                             p0->next=p1;
                             p1->pre=p0;
                             head=p0;
                 }
                 else
                 {
                     p1->pre->next=p0;
                     p0->next=p1;
                     p0->pre=p1->pre;
                     p1->pre=p0;
                 }
          }
          else
          {
              p1->next=p0;
              p0->pre=p1;
              p0->next=NULL;
          }
          return head;
    } 
    //删除指定的节点
    dnode *del(dnode *head,int num)
    {
          dnode *p1,*p2;
          p1=head;
          while(num!=p1->data&&p1->next!=NULL)
           p1=p1->next;
          if(num==p1->data)
          {
                          if(p1==head)
                          {
                                      head=head->next;
                                      head->pre=NULL;
                                      free(p1);
                          } 
                          else if(p1->next==NULL)
                          {
                                      p1->pre->next=NULL;
                                      free(p1);
                          }
                          else
                          {
                                      p1->next->pre=p1->pre;
                                      p1->pre->next=p1->next;
                                      free(p1);           
                          }
          }
          else
          {
              cout<<"没有指定的节点"<<endl;
          }
          return head;
    } 
    int main(int argc, char *argv[])
    {
        int x;
        dnode *head=creat();
        print(head);
        cout<<"输入要插入的数字:"<<endl;
        cin>>x;
        head=insert(head,x);
        print(head);
        cout<<"输入要删除的数字:"<<endl;
        cin>>x;
        head=del(head,x);
        print(head);
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

  • 相关阅读:
    【力扣 077】451. 根据字符出现频率排序
    【力扣 072】1696. 跳跃游戏 VI
    【力扣 075】409. 最长回文串
    【力扣 078】387. 字符串中的第一个唯一字符
    日志切面接口和方法demo
    fastjson不出网代码利用
    随机生成指定长度的字符串
    C#内插字符串,格式化字符串
    C#,如何分别在x64和x86平台上加载同一个名称的原生dll
    软件体系结构——习题
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835336.html
Copyright © 2020-2023  润新知