Operation and implementation of bidirectional cyclic chain list ...
Online about this very much, because of their previous classes did not learn the data structure, and now to recognize the data structure, the following is written on the basis of C + + bidirectional cyclic linked list of the creation and some operations and implementation (under the VC through), no template,
Also useless class, so it is more suitable to have a little C + + language basic beginners, but the portability is not enough. If there are any bugs, please point out.
Or if you have any questions, you can contact me.
Made by Virgil (2009.2.8)
msn:hangyu_628@hotmail.com)
#include <iostream>
#include <cstdlib>
using namespace Std;
int n=10;
struct Node
{
Char name[20];
Node *llink,*rlink;
};
node* Create (int n)
{
Node *h,*p,*s; H: Header node, P: Next node, S: Current node
int i;
if ((h=new Node) ==null)
{
cout<< "Allocate memory failed ..." <<endl;
}
h->name[0]=0;
h->llink=null;
h->rlink=null;
P=h;
for (I=0;i!=n;++i)
{
if ((s=new Node) ==null)
{
cout<< "Allocate memory failed ..." <<endl;
}
p->rlink=s;
cout<< "Please enter the first" <<i+1<< "person's name:";
cin>>s->name;
s->llink=p;
s->rlink=null;
P=s;
}
s->rlink=h;
h->llink=s;
return h;
}
node* Search (node* h,const char* name)
{
Node *p=h->rlink;
for (int i=0;i!=n;++i)
{
if (strcmp (p->name,name) ==0)
{
return p;
}
p=p->rlink;
}
return p;
}
void Insert (Node *p)
{
Node *s=new node;
cout<< "Please enter the name you want to insert:";
cin>>s->name;
Node *r=p->rlink; Node schematic p->s->r (S is inserted node).
p->rlink=s;
s->llink=p;
r->llink=s;
s->rlink=r;
++n;
}
void Delete (Node *p)
{
Node *l=p->llink; Node schematic l->p->r (p for the node to be deleted)
Node *r=p->rlink;
l->rlink=r;
r->llink=l;
Delete p;
--n;
}
void Display (Node *h)
{
Node *p;
p=h->rlink;
for (int i=0;i!=n;++i)
{
cout<< "<<i+1<<" person's name: "<<p->name<<endl;
Delete p;
p=p->rlink;
}
}
int main ()
{
Node *head,*psearch;
int number=n;
Char strname[20];
Head=create (number);
cout<<endl;
cout<< "The structure you created is as follows:" <<endl;
Display (head);
cout<<endl;
Find and insert ...
cout<< "Please enter the name of the person you are looking for:";
cin>>strname;
Psearch=search (Head,strname);
cout<< "The name of the person you are looking for is:" <<pSearch->name<<endl;
cout<<endl;
Insert (Psearch);
cout<< "The result of the insertion is as follows:" <<endl;
Display (head);
cout<<endl;
Find and remove ...
cout<< "Enter the node you want to delete:";
cin>>strname;
Psearch=search (Head,strname);
Delete (Psearch);
cout<< "The result of the deletion is as follows:" <<endl;
Display (head);
cout<<endl;
return 0;
}