Showing posts with label specified by user. Show all posts
Showing posts with label specified by user. Show all posts

Wednesday, 29 November 2017

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);
}


Amazon1Ads