无头节点的双向循环链表

#include <bits/stdc++.h>
using namespace std;
struct Node{
    int data;
    Node *next,*pre;
};
Node* CreatList(){
    Node *head=new Node,*p=head;   
    int data;
    if(cin>>data){
        head->data=data;
        head->next=head->pre=head;
    }
    while(cin>>data){
        Node *q=new Node;
        q->data=data;
        q->pre=p;
        p->next=q;
        p=q;
    }
    p->next=head;
    head->pre=p;
    return head;
}
void DeleteList(Node* head, int x){
    Node *p=head,*q;
    for(int i=1;i<x;i++) p=p->next;
    q=p->next;
    p->next=q->next;
    q->next->pre=p;
    delete q;
}
void show(Node* head){
    Node* p=head;
    if(!p) return;
    cout<<p->data<<' ';
    p=p->next;
    while(p!=head){
        cout<<p->data<<' ';
        p=p->next;
    }
}
void reshow(Node* head){
    Node* p=head;
    if(!p) return;
    cout<<p->data<<' ';
    p=p->pre;
    while(p!=head){
        cout<<p->data<<' ';
        p=p->pre;
    }
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    Node *head=CreatList();
    DeleteList(head,1);
    show(head);
    cout<<endl;
    reshow(head);
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容