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

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

#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
    评论
我们可以使用双向链表来创建一个学生成绩数据库。每个节点代表一个学生,包含学生的姓名、学号、课程成绩等信息。每个节点还包含两个指针,分别指向前一个节点和后一个节点。 以下是一个示例代码: ```python class Node: def __init__(self, name, id, score): self.name = name self.id = id self.score = score self.prev = None self.next = None class ScoreDB: def __init__(self): self.head = None self.tail = None def add(self, name, id, score): node = Node(name, id, score) if self.head is None: self.head = node self.tail = node else: node.prev = self.tail self.tail.next = node self.tail = node def remove(self, id): node = self.find(id) if node is None: return if node.prev is None: self.head = node.next else: node.prev.next = node.next if node.next is None: self.tail = node.prev else: node.next.prev = node.prev def find(self, id): node = self.head while node is not None and node.id != id: node = node.next return node def print_all(self): node = self.head while node is not None: print(node.name, node.id, node.score) node = node.next ``` 我们可以使用add方法来添加学生信息,使用remove方法来删除学生信息,使用find方法来查找学生信息,使用print_all方法来打印所有学生信息。 例如: ```python db = ScoreDB() db.add("Alice", 1, 90) db.add("Bob", 2, 80) db.add("Charlie", 3, 70) db.print_all() # Output: # Alice 1 90 # Bob 2 80 # Charlie 3 70 db.remove(2) db.print_all() # Output: # Alice 1 90 # Charlie 3 70 node = db.find(1) print(node.name, node.id, node.score) # Output: # Alice 1 90 ``` 这个例子中,我们首先创建一个ScoreDB对象,并使用add方法添加三个学生信息。然后使用print_all方法打印所有学生信息。接着使用remove方法删除学号为2的学生信息,再次使用print_all方法打印所有学生信息。最后使用find方法查找学号为1的学生信息,并打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值