双链表的创建,求长,插入,删除,打印,释放(循环和非循环)

链接地址:http://blog.csdn.net/stpeace/article/details/8112462
#include<iostream>

using namespace std;

typedef struct Node
{
        int data;
        struct Node *prior; 
        struct Node *next;
}Node,*DList;//DList用来指向一个链表,Node *定义的函数用来返回其中一个节点的地址

DList  createDList1()//输入数据创建循环链表
{
        int num;
        Node *head,*p1,*p2;//p1指向新创建的节点,p2指向新创建之前的一个节点
        head=new head;
        p1=p2=head->prior=head->next=head;

        cin>>num;
        while(0!=num)
        {
              p1=new Node;
              p1->data=num;

              p2->next=p1;//不能是head->next
              p1->next=head;
              p1->prior=p2;
              head->prior=p1;
              p2=p1;//不可少
              cin>>num;
        }
        return head;
}

void createDList(Node *&L,int a[],int n)//创建双链表(利用数组)
{
        Node *s,*r;
        int i;
        L=new Node;
        L->next=NULL;
        r=L;
        for(i=0;i<n;i++)
        {
               s=new Node;
               s->data=a[i];
               r->next=s;//在此之前,r代表第一个节点(头结点),s为第二个节点
               s->prior=r;//prior指向它前一个
                r=s;
        }
             r->next=NULL;
}

DList searchNode(Node *C,int x)//查找指定的数据,返回节点
{
      Node *p=C->next;//C为头结点,里面没有数据
       while(p!=NULL)
       {
              if(p->data==x)
              {
                    break;//结束整个循环,continue结束单个循环,if中的条件成立后,将不执行p=p->next,是continue则会执行
              }
              p=p->next;
       }
       return p;
}

int getDListLength(DList p)//循环双链表求长(和双链表求长不一样),不带头结点
{
       int length=0;
       Node *head=p//普通双链表不需要这样定义
       while(head!=p->next)//普通双链表的判断条件是NULL!=p->next
        {
             length++;
             p=p->next;
        }
         return length;
}

Node *getlocation(DList p,int location)//循环双链表的定位,找到某一个位置返回其指针
{
     //为了程序的健壮,这里还要对location进行判断
       Node *head=p;
       int i;
       for(i=0;head!=p->next&&i<location;i++)
       {
                p=p->next;
       }
       return p;
}

void insertNode(DList p,int location,int element)//循环链表的插入,插在指定位置的后面
{
       Node *q=getlocation(p,location);
       Node *s=new Node;//新建一个节点插入
       s->data=element;
  
       s->prior=q;
       s->next=q->next;
       q->next->prior=s;//q->next表示之前q之前的后一个节点,这段代码表示q之前的后一个节点的prior指向
       q->next=s;
}

void delNode(DList p,int location)
{
      Node *q=getlocation(p,location);//删除这个q
      q->prior->next=q->next;//q->next表示q之前的那个节点
      q->next->prior=q->prior;//q->next表示q之后的那个节点
      delete p;
}

void release(DList p)//释放循环双链表
{
        Node *head=p;
        if(head==p->next)
             delete p; 
        else
         {
             release(p->next);
             delete p;
         }
}

void print(DList p)//打印环状
{
       Node *head=p;//指针初始化,此时head表示头结点的地址
       while(head!=p->next)//循环的,最后又指向头结点
        {
               cout<<p->next->data<<endl;
               p=p->next;
        }
}

void print1(DList p)//打印非循环双链表
{
       while(NULL!=p->next)
        {
             cout<<p->next->data<<endl;
             p=p->next;
        }
}

int main()
{
       DList head,head1;
       int a[5]={1,2,3,4,5};
       createDList(head,a,5);
       print1(head);

       DList head2=createDList();
       print(head2);

       int location=2;
       int element=5;
       insertNode(head2,location,element);
       print(head2);

       int location1=2;
       delNode(head2,location);
       print(head2);
 
       release(head2);

}

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盼盼编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值