Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- typedef struct node Node;
- Node* head;
- Node* create_node(int item, Node *next)
- {
- Node* new_node=new Node();
- if(new_node==NULL)
- cout<<"Error"<<endl;
- else
- {
- new_node->data=item;
- new_node->next=next;
- }
- return new_node;
- }
- Node* prepend(int item,Node* head)
- {
- Node* new_node=create_node(item,head);
- return new_node;
- }
- Node* append(int item,Node *head)
- {
- Node *new_node=create_node(item,NULL);
- if(head==NULL)
- return new_node;
- else
- {
- Node* current=new Node();
- current=head;
- while(current->next!=NULL)
- current=current->next;
- current->next=new_node;
- }
- return head;
- }
- Node* insertn(int item,Node* head,int key)
- {
- Node* new_node=create_node(item,NULL);
- if(head==NULL)
- cout<<"Error"<<endl;
- else
- {
- Node *current=new Node();
- current=head;
- while(current!=NULL&¤t->data!=key)
- current=current->next;
- if(current!=NULL)
- {
- new_node->next=current->next;
- current->next=new_node;
- }
- else
- cout<<"Value not found"<<endl;
- }
- return head;
- }
- void display(Node *head)
- {
- cout<<"Linked list:"<<endl;
- while(head!=NULL)
- {
- cout<<head->data<<" ";
- head=head->next;
- }
- cout<<endl;
- }
- Node* remove_head(Node* head)
- {
- head=head->next;
- return head;
- }
- Node* remove_tail(Node* head)
- {
- Node *current=new Node();
- Node *prev=new Node();
- current=head;
- while(current->next!=NULL)
- {
- prev=current;
- current=current->next;
- }
- prev->next=NULL;
- return head;
- }
- Node* remove_value(Node* head,int key)
- {
- Node* temp=new Node();
- temp=head;
- Node* prev=new Node();
- if(temp!=NULL&&temp->data==key)
- {
- head=temp->next;
- return head;
- }
- while(temp!=NULL && temp->data!=key)
- {
- prev=temp;
- temp=temp->next;
- }
- if(temp==NULL)
- {
- cout<<"Value not present"<<endl;
- return head;
- }
- prev->next=temp->next;
- return head;
- }
- Node* reverse_list(Node* head)
- {
- Node* prev=NULL;
- Node* current=head;
- while(current!=NULL)
- {
- Node* temp=current->next;
- current->next=prev;
- prev=current;
- current=temp;
- }
- head=prev;
- return head;
- }
- void print(Node*head)
- {
- if(head==NULL)
- return;
- cout<<head->data<<" ";
- print(head->next);
- }
- void print_reverse(Node*head)
- {
- if(head==NULL)
- return;
- print_reverse(head->next);
- cout<<head->data<<" ";
- }
- void reverse_rec(Node* p)
- {
- if(p->next==NULL)
- {
- head=p;
- return;
- }
- reverse_rec(p->next);
- Node* q=p->next;
- q->next=p;
- p->next=NULL;
- }
- int main()
- {
- head=NULL;
- head=prepend(10,head);
- head=insertn(20,head,10);
- head=insertn(30,head,20);
- head=insertn(40,head,30);
- head=insertn(50,head,40);
- head=insertn(60,head,50);
- head=insertn(70,head,60);
- head=remove_head(head);
- display(head);
- cout<<"Using recursion reversed"<<endl;
- reverse_rec(head);
- display(head);
- }
Add Comment
Please, Sign In to add comment