双向链表的一个简单的例子

以前写的 最近找出来 加上了逆置的功能

#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; }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值