以前写的 最近找出来 加上了逆置的功能
#include<iostream>
#include<string>
#define N 10
using namespace std;
struct ptnode
{
char name[20];
ptnode *llink,*rlink;
};
ptnode *create(int n);
ptnode *search(ptnode *head,char *p);
void insert(ptnode *head,char *p,char *inp);
void print(ptnode *head);
void Delete(ptnode *ptr);
void Contray_Dul(ptnode *head);//逆置
int main()
{
int n;
char stuname[20];
char insertname[20];
ptnode *head,*searchpoint;
cout<<"输入链表长度:";
cin>>n;
head=create(n);
print(head);
cout<<"输入你要查找的名字:";
cin>>stuname;
searchpoint=search(head,stuname);
cout<<"你想要查找的名字是:"<<searchpoint->name<<endl;
Delete(searchpoint);
print(head);
cout<<"你想在哪个名字前插入:";
cin>>stuname;
cout<<"输入你想插入的名字:";
cin>>insertname;
insert(head,stuname,insertname);
print(head);
cout<<"_____逆置表_____"<<endl;
Contray_Dul(head);
print(head);
cout<<endl;
return 0;
}
ptnode *create(int n)
{
ptnode *head,*ptr,*newnode;
int i;
if((head=new ptnode)==NULL)
{
cout<<"无空间申请"<<endl;
return NULL;
}
head->name[0]='\0';
head->llink=NULL;
head->rlink=NULL;
ptr=head;
for(i=0;i<n;i++)
{
newnode=new ptnode;
ptr->rlink=newnode;//连接新节点
cout<<"输入第"<<i+1<<"个人的名字:";
cin>>newnode->name;
newnode->llink=ptr;//定义新节点的连接关系
newnode->rlink=NULL;
ptr=newnode;
}
head->llink=newnode;
newnode->rlink=head;
return head;
}
ptnode *search(ptnode *head,char *p)
{
ptnode *ptr;
ptr=head->rlink;
while(ptr!=head)//以是否回到头结点为结束条件
{
if(strcmp(ptr->name,p)==0)
return ptr;
else
ptr=ptr->rlink;
}
cout<<"你要找的名字不存在"<<endl;
return NULL;
}
void insert(ptnode *head,char *p,char *inp)
{
ptnode *ptr,*newnode;
ptr=search(head,p);
if(ptr==NULL)
{
cout<<"找不到指定插入点"<<endl;
return ;
}
newnode=new ptnode;
strcpy(newnode->name,inp);
newnode->llink=ptr->llink;
newnode->rlink=ptr;
(ptr->llink)->rlink=newnode;
ptr->llink=newnode;
}
void print(ptnode *head)
{
ptnode *p;
p=head->rlink;
cout<<"现在的数据为:";
while(p!=head)
{
cout<<p->name<<" ";
p=p->rlink;
}
cout<<endl;
}
void Delete(ptnode *ptr)
{
ptr->rlink->llink=ptr->llink;
ptr->llink->rlink=ptr->rlink;
delete ptr;
}
void Contray_Dul(ptnode *head)
{
ptnode *p=head->rlink,*q;
while(p!=head)
{
q=p->rlink;
p->rlink=p->llink;
p->llink=q;
p=q;
}
q=head->rlink;
head->rlink=head->llink;
head->llink=q;
}