Showing posts with label insertion linked list. Show all posts
Showing posts with label insertion linked list. Show all posts

Monday, 8 October 2018

Insertion, Deletion ,Searching at First ,Last , At Given Position in Linked List Implementation in C++

#include<bits/stdc++.h>
using namespace std;
struct node
{
node *link;
int data;
}*start=NULL;
// INSERTION INTO LINKED LIST
void insertNode(int data)
{
node *temp=new node;
temp->link=NULL;
temp->data=data;
if(start==NULL)
{
start=temp;
return;
}
else
{
node *ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
}
void insertInBetween(int data,int search)
{
node *temp=new node;
temp->data=data;
temp->link=NULL;
node *ptr=start;
while(ptr->link!=NULL and ptr->data!=search)
ptr=ptr->link;
temp->link=ptr->link;
ptr->link=temp;
}
void insertFirst(int data)
{
node *temp=new node;
temp->data=data;
temp->link=start;
start=temp;
}
void insertInBetweenBefore(int data,int search)
{
node *prev=NULL,*ptr=start,*temp=new node;
temp->data=data;
while(ptr->link!=NULL and ptr->data!=search)
{
prev=ptr;
ptr=ptr->link;
}
temp->link=prev->link;
prev->link=temp;
}

// ?? VARIOUS LINKED LIST DELETION OPERATIONS ?? //
void deleteFirst()
{
node *ptr=start;
if(ptr->link==NULL)
start=NULL;
else
start=ptr->link;

delete(ptr);
}
void deleteAtPosition(int pos)
{
node *ptr=start;
node *prev=NULL;
int count=0;
while(ptr->link!=NULL and count!=pos)
{
prev=ptr;
count+=1;
ptr=ptr->link;
}
prev->link=ptr->link;
delete(ptr);
}

void deleteLast()
{
node *ptr=start,*prev=NULL;
while(ptr->link!=NULL)
{
prev=ptr;
ptr=ptr->link;
}
prev->link=NULL;
delete(ptr);
}

void disp()
{
node *ptr=start;
while(ptr!=NULL)
{
cout<<" -->"<<ptr->data;
ptr=ptr->link;
}
}

int main()
{
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
cout<<"BEFORE\n";
disp();
cout<<"\nINSERTION AFTER 30\n";
insertInBetween(35,30);
disp();
cout<<"\nINSERTION AT FIRST NODE\n";
insertFirst(5);
disp();
cout<<"\nINSERTION BEFORE 30\n";
insertInBetweenBefore(25,30);
disp();
cout<<"\nDELETING FIRST ELEMENT\n";
deleteFirst();
disp();
cout<<"\nDELETING AT GIVEN POSITION =3:\n";
deleteAtPosition(3);
disp();
cout<<"\nDELETING LAST ELEMENT:\n";
deleteLast();
disp();


}

Thursday, 30 November 2017

Implementation of Tower Of Hanoi recursive method in C++

 
#include<bits/stdc++.h>
using namespace std;

void towerOfHanoi(int n, char from, char to, char aux)
{
    if (n == 1)
    {
        printf("n Move disk 1 from rod %c to rod %c", from, to);
        return;
    }
    towerOfHanoi(n-1, from, aux, to);
    printf("n Move disk %d from rod %c to rod %c", n, from, to);
    towerOfHanoi(n-1, aux, to, from);
}
 
int main()
{
    int n = 4; // Number of disks
    towerOfHanoi(n, 'A', 'C', 'B');  // A, B and C are names of rods
    return 0;
}

Circular Linked List Implementation Data Structure in C/ C++

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *link;
};
void insert(struct node **start,int num)
{
cout<<"First";

node *temp=new node;
temp->info=num;

temp->link=NULL;

(*start)=temp;

}
void insertEnd(node *start,int num)
{
cout<<"\nLast:";
node *temp;
while(start->link!=NULL)
{
start=start->link;
}
temp=new node;
temp->info=num;
temp->link=NULL;
start->link=temp;

}

void makeCircular(node *start)
{
node *temp=start;
while(start->link!=NULL)
{

    start=start->link;
}
if(start->link==NULL)
{
start->link=temp;
}
}
void disp1(node *start)
{
node *temp=start;
while(temp->link!=start)
{
cout<<" "<<temp->info;
    temp=temp->link;
}
cout<<" "<<temp->info;
}


main()
{
struct node *start=NULL;

int num,x,y;
cout<<"In Main";
insert(&start,1);
insertEnd(start,5);
insertEnd(start,3);
insertEnd(start,4);
insertEnd(start,6);
insertEnd(start,7);
insertEnd(start,8);
insertEnd(start,9);
insertEnd(start,10);

makeCircular(start);
disp1(start);

}

Wednesday, 29 November 2017

Alternate Node Deletion in Singly Linked List in C/C++ Implementation

#include<iostream>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}

void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}

void deleteAlternate(node *start)
{
node *currnext=start->link;
node *nexttonext=start->link->link;
node *curr=start;
while(curr!=NULL &&curr->link!=NULL && curr->link->link!=NULL)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
curr=curr->link;
}
}




void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\n";
}





main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
        cout<<"Before Deleting Linked List\n:";
disp(start);
deleteAlternate(start);
        cout<<"After Deleting Alternate Node:\n";
disp(start);
}

Insertion into Linked List after an element specified by user in C++/C

#include<iostream>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}

void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}

void insertAtPos(node *start,int num,int num1)
{
node *temp=new node;
temp->info=num1;
temp->link=NULL;
node *prev=start;
while(start!=NULL)
{
if(start->info==num)
break;
else
prev=start;
start=start->link;
}
temp->link=start->link;
start->link=temp;

}

void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
}




main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
insertAtPos(start,2,6);
disp(start);
}


Header Linked List - Implementation in C++/C

#include<bits/stdc++.h>

using namespace std;

struct node
{
int info;
node *link;
};
struct header
{
node *next;
int total=0;
};

void insertFirst(node **start,int data,header *head)
{
node *temp=new node;
temp->info=data;
temp->link=NULL;
*start=temp;
head->next=temp;
head->total+=1;
}
void insertLast(node *start,header *head,int data)
{
node *temp=new node;

while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
temp->link=NULL;
temp->info=data;
head->total+=1;
}

void display(node *start,header *head)
{
cout<<"Elements are:\n";
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\nTotal Number Of Elements Are:"<<head->total;
}

void deleteAlternate(node *start,header *head)
{
cout<<"\nIn Delete Function:\n";
node *nexttonext;
node *curr=start;
while(curr!=NULL&&curr->link!=NULL)
{


if(curr->link->info%2!=0)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
head->total-=1;
curr=curr->link;
}


}


}
main()
{
int i=0;
char ch='y';
node *start=NULL;
header *head =new header;
cout<<"First Element Entered:\n";
while(ch=='y'||ch=='Y')
{

if(i==0)
insertFirst(&start,i,head),i++;
else
insertLast(start,head,i),i++;

cout<<"Do You Want More Record:";
cin>>ch;
}
display(start,head);
deleteAlternate(start,head);
display(start,head);
}




Amazon1Ads