Double linked list In fact, there's nothing more than a front chain.
Definition of double linked list
Copy Code code as follows:
struct Dnode
{
int data;
struct Dnode *next;
struct Dnode *pre;
};
Definition of a single linked list
Copy Code code as follows:
View Plaincopy
struct Dnode
{
int data;
struct Dnode *next;
};
Others can be like a blog about the same
Copy Code code as follows:
#ifndef Head_h
#define Head_h
#include <iostream>
using namespace Std;
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
#include <string>
#include <algorithm>
#include <list>
#include <queue>
#include <vector>
#include <deque>
#include <stack>
#include <bitset>
#include <set>
#include <map>
#endif
struct Dnode
{
int data;
struct Dnode *next;
struct Dnode *pre;
};
Dnode *creat ()
{
Dnode *head,*p,*s;
Head= (Dnode *) malloc (sizeof (Dnode));
P=head;
int temp;
while (cin>>temp&&temp)
{
s= (Dnode *) malloc (sizeof (Dnode));
s->data=temp;
p->next=s;
s->pre=p;
P=s;
}
head=head->next;
p->next=null;
head->pre=null;
return (head);
}
Dnode *insert (dnode *&head,int num)
{
Dnode *p,*s;
s= (Dnode *) malloc (sizeof (Dnode));
s->data=num;
P=head;
while (Null!=p->next&&num>p->data)
{
p=p->next;
}
if (Num<=p->data)
{
if (Null==p->pre)
{
s->next=head;
head->pre=s;
Head=s;
head->pre=null;
}
Else
{
s->pre=p->pre;
p->pre->next=s;
s->next=p;
p->pre=s;
}
}
Else
{
p->next=s;
s->pre=p;
s->next=null;
}
return (head);
}
Dnode *del (dnode *&head,int num)
{
Dnode *p;
P=head;
while (Null!=p->next&&num!=p->data)
{
p=p->next;
}
if (Num==p->data)
{
if (Null==p->pre)
{
head=p->next;
p->next->pre=head;
Free (p);
}
else if (null==p->next)
{
p->pre->next=null;
Free (p);
}
Else
{
p->pre->next=p->next;
p->next->pre=p->pre;
Free (p);
}
}
Else
{
cout<<num<< "Cound not to be found" <<endl;
}
return head;
}
void Display (Dnode *head)
{
Dnode *p;
P=head;
while (NULL!=P)
{
cout<< (p->data) << "";
p=p->next;
}
cout<<endl;
}
Copy Code code as follows:
#include "head.h"
int main ()
{
Dnode *head;
Head=creat ();
Display (head);
#ifndef DEBUG
cout<< "Please input a num to insert:";
#endif
int insert_num;
while (cin>>insert_num&&insert_num)
{
Insert (head,insert_num);
Display (head);
}
#ifndef DEBUG
cout<< "Please input a number to delete:";
#endif
int delete_num;
while (cin>>delete_num&&delete_num)
{
Del (head,delete_num);
Display (head);
}
Return (exit_success);
}