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

来源:互联网 发布:淘宝上的轮毂能买吗 编辑:程序博客网 时间:2024/05/21 21:37
链接地址: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);}

0 0