当前位置:网站首页>Bidirectional linked list - all operations

Bidirectional linked list - all operations

2022-07-06 16:13:00 Handsome black cat Sheriff

initialization , Tail insertion ( Head insert similar , It is equivalent to the last insertion of the previous digit ), Traverse , Insert , Delete , Sentenced to empty

#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct dnode{									// Define data types 
    int data;
    struct dnode *prior,*next;							// Double linked list has two pointers !
}dnode,*dlinklist;
bool initdlinklist(dlinklist &l)							// Initializing a double linked list 
{
    l=(dnode *)malloc(sizeof(dnode));
    if(l==NULL)
        return 0;
    l->prior=NULL;							
    l->next=NULL;										// Both the head pointer and the tail pointer are set null	
    return 1;
}
bool empty(dlinklist l)									// Judge that the linked list is not empty 
{
    if(l->next==NULL)
        return 1;
    else
        return 0;
}
bool insertdlinklist(dlinklist &l,int i,int e)				// The insert 
{
    int j=0;
    dnode *p,*s;
    p=(dnode *)malloc(sizeof(dnode));
    p=l;
    while(p->next!=NULL&&j<i-1)
    {
        j++;
        p=p->next;
    }
    if(p==NULL||j!=i-1)										// Robust code 
        return 0;
    s=(dnode *)malloc(sizeof(dnode));
    s->data=e;
    s->next=p->next;
    if(p->next!=NULL)
        p->next->prior=s;
    p->next=s;
    s->prior=p;
    return 1;
}
bool deletedlinklist(dlinklist &l,int i,int &e)				// Delete operation 
{
    int j=0;
    dnode *p;
    p=(dnode *)malloc(sizeof(dnode));
    p=l;
    while(p->next!=NULL&&j<i)
    {
        j++;
        p=p->next;
    }
    if(p==NULL||j!=i)											// Robust code 
        return 0;
    e=p->data;
    if(p->prior!=NULL)
        p->prior->next=p->next;
    if(p->next!=NULL)											// Judge whether it is the last 
        p->next->prior=p->prior;
    free(p);
    return 1;
}
dlinklist dlinklist_tailinsert(dlinklist &l)						// Tail insertion 
{
    int x;
    dnode *p,*s;
    p=(dnode *)malloc(sizeof(dnode));
    p=l;
    cin>>x;
    while(x!=0)
    {
        s=(dnode *)malloc(sizeof(dnode));
        s->data=x;
        s->next=NULL;
        p->next=s;
        s->prior=p;
        p=s;
        cin>>x;
    }
    return l;
}
void lookdlinklist(dlinklist l)								// Traverse 
{
    dnode *p;
    int j=1;
    p=(dnode *)malloc(sizeof(dnode));
    p=l->next;
    while(p!=NULL)
    {
        cout<<"number "<<j<<" is "<<p->data<<" , ";
        j++;
        p=p->next;
    }
    cout<<endl;
}
int main() {
    dlinklist l;
    if(initdlinklist(l))
        cout<<"initdlinklist succeed"<<endl;
	if(empty(l))
	    cout<<"dlinklist is empty!"<<endl;
	else cout<<"dlinklist not empty!"<<endl;
	dlinklist_tailinsert(l);
	if(empty(l))
	    cout<<"empty"<<endl;
	else cout<<"not empty"<<endl;
	lookdlinklist(l);
	if(insertdlinklist(l,2,88)&&(insertdlinklist(l,4,99)))
	    cout<<"insertdlinklist succeed\n";
	else cout<<"insertdlinklist faild\n";
	lookdlinklist(l);
	int e=0;
	if(deletedlinklist(l,2,e))
	    cout<<"deletedlinklist succeed,delete element is "<<e<<endl;
	else cout<<"delete error"<<endl;
	lookdlinklist(l);
	cout  << "over\n";
	return 0;
}
原网站

版权声明
本文为[Handsome black cat Sheriff]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060920013391.html