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