Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class dlist{
- public:
- int data;
- dlist*right;
- dlist*left;
- dlist(int n){
- this->data=n;
- left=NULL;
- right=NULL;
- }
- };
- void print(dlist*tmp){
- while(tmp){
- cout<<tmp->data<<" ";
- tmp=tmp->right;
- }
- cout<<endl;
- }
- void push(dlist* &node,int n){
- dlist*tmp=new dlist(n);
- tmp->right=node;
- node->left=tmp;
- node=tmp;
- }
- void deletenode(dlist* &node,int x){
- if(!node)return;
- if(node->data==x){
- node=node->right;
- node->left=NULL;
- return;
- }
- dlist*tmp=node;
- while(tmp->right!=NULL && tmp->right->data != x){
- tmp=tmp->right;
- }
- if(tmp->right){
- tmp->right=tmp->right->right;
- if(tmp->right)tmp->right->left=tmp;
- }
- }
- void reverse(dlist* &node){
- if(!node || !node->right)return;
- dlist*curr=node,*prev=NULL;
- while(curr){
- dlist*next=curr->right;
- curr->right=prev;
- curr->left=next;
- prev=curr;
- curr=curr->left;
- }
- node=prev;
- }
- void middle(dlist* node){
- if(!node)cout<<-1<<endl;
- else if(!node->right)cout<<node->data<<endl;
- else{
- dlist*slow=node,*fast=node->right;
- while(fast && fast->right){
- slow=slow->right;
- fast=fast->right->right;
- }
- cout<<slow->data<<" ";
- if(fast)cout<<slow->right->data<<" ";
- cout<<endl;
- }
- }
- int main(){
- dlist*head=new dlist(0);
- push(head,1);
- push(head,2);
- push(head,4);
- push(head,5);
- push(head,6);
- print(head);
- deletenode(head,3);
- print(head);
- deletenode(head,0);
- print(head);
- deletenode(head,6);
- print(head);
- reverse(head);
- print(head);
- middle(head);
- push(head,7);
- print(head);
- middle(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement