Skip to content

Instantly share code, notes, and snippets.

@dumbbellcode
Last active April 24, 2019 18:51
Show Gist options
  • Save dumbbellcode/9e5dce8bffdfdf82af7dbd7d45ffaaee to your computer and use it in GitHub Desktop.
Save dumbbellcode/9e5dce8bffdfdf82af7dbd7d45ffaaee to your computer and use it in GitHub Desktop.
Everything with linked lists(C++ insertion,printing,deleting elements,reversing,printing using recursion,reversing using recursion,etc in linked lists.)
//linked lists node insertion,printing,deletion and reversing
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node* head;
void insert(int,int);
void print();
void insert(int x,int y) //x is data and y is position
{
node* temp1=new node(); //make new node
temp1->data=x; //enter its data
temp1->next=NULL; //next is null
if(y==1){
temp1->next=head; //if node has to be inserted in first position ,it comes btw head and first node
head=temp1;
return;
}
node* temp2=head;
for(int i=1;i<y-1;i++) //insertion at yth position //our linked list's first node has index1 , head has index zero.
{ temp2=temp2->next;}
temp1->next=temp2->next;
temp2->next=temp1;
}
void print()
{ node* temp1=head;
while(temp1!=NULL){ cout<<temp1->data;temp1=temp1->next;}
cout<<endl;
}
void del(int n) //delete node at position n
{ node*temp1=head;
if(n==1){head=temp1->next;//head now points to second node
delete temp1;
return;}
for(int i=1;i<n-1;i++){temp1=temp1->next;}
//temp1 points to n-1 th node
node* temp2=temp1->next; //temp2 points nth node
temp1->next=temp2->next;//n-1 th node now points to n+1 th node
free(temp2); //frees the memory temp2 points i.e delete temp2
}
void rev() //reverse the linked list
{ node*temp1=head;
node*temp2;node*x;
temp2=temp1->next;
temp1->next=NULL;
while(1){
x=temp2->next;
temp2->next=temp1;
temp1=temp2;
temp2=x;
if(x==NULL){ head=temp1;break;}
}
//cout<<"list has been reversed\n";
}
void recurprint(node *x) // printing elements using recursion
{
if(x==NULL) {cout<<" recurprint done\n" ;return;}
cout<<x->data;
recurprint(x->next);
//here first the element is printed and then recursive call is done
}
void reverseprint(node *x)
{
if(x==NULL){cout<<"reverseprint below,now this routine empties first from stack\n";return;}
reverseprint(x->next);
cout<<x->data;
// here first we are recursive calling and then then printing the elments
// hence elements are printed when the stack starts emptying
}
void recureverse(node *x)//reverse linked list using recursion
{ if(x->next==NULL){head=x;cout<<"\nrecureverse below\n";return;}
recureverse(x->next);
node*q = x->next;
q->next=x;
x->next=NULL;
//just like reverseprint this works when linked list starts emptying
}
int main()
{ head=NULL;
// cout << "Hello World" << endl;
insert(1,1); //1
insert(5,2); //15
insert(2,1); //215
insert(7,2); //2715
print();
del(3); print(); //275
rev();
print();//572
insert(8,4);
rev();
print();
recurprint(head);
reverseprint(head);
recureverse(head);
print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment