Making a Linked List in C++ - c++

This is a code to make a linked list with 2 values- one user input and another 7.
#include<iostream>
#include<cstdlib>
using namespace std;
class node{
public:
node();
~node();
void printList();
void insert_front(int);
void delete_front();
private:
int data;
node *head;
node *next;
};
node::node()
{
head=NULL;
}
node::~node( ){//destructor
cout <<"destructor called";
while( head!= NULL) delete_front() ;
}
void node::delete_front(){
node *h=head;
if(head==NULL)
{
cout<< "Empty List.\n";
return;
}
head = head->next;
delete(h);
}
void node::printList()
{
node *h=head;
cout<< "Printing the list";
while(h!=NULL)
{
cout<< h->data;
cout<< '\n';
h->next= h->next->next;
}
}
void node::insert_front(int value){
node *temp = new node;
temp->data=value;
temp -> next = NULL;
if (head != NULL){
temp->next =head;
}
head= temp;
}
int main()
{
node ListX;
cout<< "enter integer";
int as;
cin>> as;
ListX.insert_front(as);
ListX.insert_front(7);
ListX.printList();
ListX.~node( );//call destructor to free objects
return 0;
}
Please tell the error in this as it shows an error while compiling it online on http://www.compileonline.com/compile_cpp_online.php and even on my laptop.

h->next= h->next->next;
What are you trying to achieve here ?
Change while loop in void node::printList() to:
while(h!=NULL)
{
cout<< h->data;
cout<< '\n';
h= h->next;
}

Related

When I am trying to enter a node between doubly circular linked list it gives wrong output

Here I created a doubly circular linked list and try to add a node between the list but it is giving wrong output
I first created a doubly circular linked list and then displayed it and then I added a node between them and then again displayed it
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode=new node(val);
if(head==0)
head=tail=newnode;
else
{
tail->next=newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
temp=temp->next;
cout<<" "<<temp->data;
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
newnode->next=temp->next;
newnode->prev=temp;
temp->next->prev=newnode;
temp->next=newnode;
}
};
int main()
{
linkedlist l1;
int s,val,val1,pos;
cin>>s;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val1;
l1.addin(val,pos); //calling addin function
l1.disp();
}
Input
3
1 2 3
1
0
Expected output
1 2 3
1 2 0 3
Current output
1 2 3 1
I don't know what mistake I am doing and also I am beginner so any tip is also too helpful for me
You are printing the head node instead of the tail node due to the extra temp=temp->next; after the loop in the disp() function.
You are adding val instead of val1 after the loop in the main() function.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// remove this
// temp=temp->next;
cout<<" "<<temp->data;
}
cin>>pos;
cin>>val1;
// add val1, not val
//l1.addin(val,pos); //calling addin function
l1.addin(val1,pos); //calling addin function
l1.disp();
in main function
you mistakenly add val instead of val1
cin>>val1;
l1.addin(val1,pos);
then in disp() function, remove temp=temp->next which is outside the loop.
void disp() //display function
{
node *temp=head;
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
// temp=temp->next;
cout<<" "<<temp->data;
}
and finally in addin() function, change pos to pos-1
for(int i=0;i<pos-1;i++)
{
temp=temp->next;
}
that's all.
#MikeCAT was spot on, you are using val1 and passing val. However, I think the while loop in disp will skip the last element and not print it. and the logic in addin are not going to add the new element in the new place. Here is my version of your code
#include<iostream>
using namespace std;
class node //node class
{
public:
int data;
node *next;
node *prev;
node(int a)
{
data=a;
next=nullptr;
prev=nullptr;
}
};
class linkedlist //linkedlist class
{
node *head,*tail;
public:
linkedlist()
{
head=nullptr;
tail=nullptr;
}
void addnode(int val) //creating node function
{
node *newnode;
newnode = new node(val);
if(head == NULL)
head = tail = newnode;
else
{
tail->next = newnode;
head->prev=newnode;
newnode->prev=tail;
newnode->next=head;
tail=newnode;
}
}
void disp() //display function
{
node *temp = head;
if (temp == NULL)
{
cout << "List is empty " << endl;
return;
}
do
{
cout << temp->data << endl;
temp = temp->next;
} while (temp != head);
}
void addin(int val,int pos) //addin function
{
node *newnode=new node(val);
node *temp=head;
for(int i=0;i<pos;i++)
{
temp=temp->next;
}
//newnode->next=temp->next;
newnode->next=temp;
newnode->prev=temp->prev;
temp = temp->prev;
temp->next = newnode;
}
};
int main()
{
linkedlist l1;
int s,val,pos;
cin>>s;
cout << "val of size is " << s << endl;
for(int i=0;i<s;i++)
{
cin>>val;
l1.addnode(val);
}
l1.disp(); //display function
cout<<"\n\n";
cin>>pos;
cin>>val;
l1.addin(val,pos); //calling addin function
l1.disp();
}

I am getting an segmentation fault error in line head=NULL

I didn't understand why i am getting error in line head=NULL I am just pointing head towards NULL
I have also tried creating a new node for head and then pointing towards NULL but still it doesn't work
Code show here
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *tail=NULL;
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
while(temp!=NULL&&k!=0)
{
if(temp->next->data<temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
I expect that whenever the line head=NULL runs the previous linked list should get delete and I should be able to enter elements in the linked list from the beginning
First of all: C++ likes "nullptr" (since C++11) for this, instead of "NULL".
=> e.g. https://en.cppreference.com/w/cpp/types/NULL vs. https://en.cppreference.com/w/cpp/language/nullptr
Second: The exact compiler output could help.
Third: The list will not be deleted if you throw away the address of it's location. maybe you should rather create a class and/or add destructor for your node. If you want to implement LL by yourself
Or have a look at std::list -> https://en.cppreference.com/w/cpp/container/list. I use this rather often.
Or if you like to add boost dependency: https://www.boost.org/doc/libs/1_35_0/doc/html/intrusive/list.html more fancy but you'll add an external library
Then: The Segfault (on my PC) happens in if(temp->next->datanext->next->data) I guess you can never know if temp->next->next->data exists... maybe not even if temp->next->data => if temp->next points to NULL (or nullptr) it is not a valid instance of node to dereference
try:
`
#include <bits/stdc++.h>
#define HERE() printf("HERE: %d\n", __LINE__)
using namespace std;
struct node
{
int data;
node *next;
};
node *head=NULL;
node *tail=NULL;
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void deletek(int n,int k)
{
if(head==NULL)
return;
node *temp;
temp=head;
while(temp->data<temp->next->data)
{
head=temp->next;
temp=temp->next;
k--;
}
HERE();
while(temp!=NULL&&k!=0)
{
HERE();
int d1 = temp->next->data;
HERE();
int d2 = temp->next->next->data;
if(temp->next->data < temp->next->next->data)
{
temp->next=temp->next->next;
k--;
}
temp=temp->next;
}
}
void display()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main() {
int t;
cout << "input t:" << endl;
cin>>t;
int n,k;
for(int i=0;i<t;i++)
{
cout << "input n/k" << endl;
cin>>n>>k;
int arr[n];
for(int j=0;j<n;j++)
{
cout << "input arr[" << j <<"]" << endl;
cin>>arr[j];
createnode(arr[j]);
}
deletek(n,k);
display();
head=NULL;
}
}
`

Deletion of specific node from singly Linked list with the help of data given

I am trying to delete the node of specific data. For this, I am using deleteNode function but not able to delete. Please see the code:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node* next;
///constructor for initializing the data-
node(int d)
{
data=d;
next=NULL;
}
};
void addAtEnd(node* &head,int data)
{
if(head==NULL)
{
head=new node(data);
return ;
}
node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
node* n =new node(data);
n->data=data;
temp->next=n;
n->next=NULL;
return;
}
AddAtTail(node* head,int d)
{
node* ptr=head;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
node *n=new node(d);
ptr->next=n;
n->next=NULL;
}
AddAtPosition(node* head,int p,int d)
{
///2 3 4 6 7 8 -1
///4th Position-
node*ptr=head;
int jump=1;
while(jump<=p-1)
{
jump++;
ptr=ptr->next;
}
node*n=new node(d);
n->next=ptr->next;
ptr->next=n;
}
/// Delete First node
void deleteFirst(node *&head)
{
head=head->next;
}
///Delete last node;
void deleteLast(node* head)
{
node* ptr=head;
while(ptr->next->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=NULL;
}
**///Delete Specific Node :-**
Here the function starts for deleting the node. I am trying to delete the node that has data 3 I am taking data as input from main function.
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{
ptr=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
void takeInput(node*& head)
{
int d;
cin>>d;
while(d!=-1)
{
addAtEnd(head,d);
cin>>d;
}
}
void print(node* head)
{
while(head!=NULL)
{
cout<<head->data<<"=>";
head=head->next;
}
}
AddAtFront(node* &head,int d)
{
///create new node;
node*n=new node(d);
n->next=head;
head=n;
}
int main()
{
node* head(NULL);
takeInput(head);
print(head);
cout<<endl<<endl<<endl<<"---------- Here The Insertion Process starts at different Positions -----------"<<endl;
cout<<endl<<"Adding at Tail"<<endl;
AddAtTail(head,9);
print(head);
cout<<endl<<"Adding at Position p"<<endl;
int p,d;
cout<<"Enter Position and data :"<<endl;
cin>>p>>d;
AddAtPosition(head,p,d);
print(head);
cout<<endl<<"Adding at Front"<<endl;
cout<<"Enter data to add at front : "<<endl;
cin>>d;
AddAtFront(head,d);
print(head);
cout<<endl<<endl<<endl;
cout<<endl<<"-------------------- NOW LETS PERFORM DELETION ------------------"<<endl;
cout<<endl<<"Deleting first node :"<<endl;
deleteFirst(head);
print(head);
cout<<endl;
cout<<endl<<"Deleting Last node :"<<endl;
deleteLast(head);
print(head);
cout<<endl;
cout<<"deleting specific node"<<endl;
cout<<"Enter data to delete"<<endl;
cin>>d;
deleteData(head,d);
print(head);
cout<<endl;
return 0;
}
Please see DeleteNode function in which I am trying to delete the node.
Why node is not deleting? Here is the function:
**///Delete Specific Node i.e- data :-**
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{
ptr=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
But Node is not deleting.
Your delete... functions are not actually deleting anything. You are just manipulating pointers but are leaking the actual node objects. And you are not taking into account the possibility that the node being deleted is the head node, which requires updating the head to point at the next node.
Also, your functions will crash on an empty list, and deleteLast will crash on a list with fewer than 2 nodes.
And deleteData is not enumerating nodes correctly.
Try something more like this instead:
#include <iostream>
using namespace std;
class node {
public:
int data;
node* next;
///constructor for initializing the data-
node(int d) {
data=d;
next=NULL;
}
};
node* findLast(node *head, node **before) {
if (before) *before = NULL;
node *last = head;
if (last) {
while (last->next) {
if (before) *before = last;
last = last->next;
}
}
return last;
}
node* addAtFront(node* &head, int data) {
node* n = new node(data);
n->next = head;
head = n;
return n;
}
node* addAtEnd(node* &head, int data) {
node *last = findLast(head, NULL);
node* n = new node(data);
if (last) {
last->next = n;
} else {
head = n;
}
return n;
}
node* addAtPosition(node* &head, int p, int d) {
if ((!head) || (p <= 0)) {
return addAtFront(head, d);
}
node *ptr = head;
node *temp;
do {
temp = ptr;
ptr = ptr->next;
}
while ((ptr) && (--p > 0));
node *n = new node(d);
n->next = temp->next;
temp->next = n;
return n;
}
/// Delete First node
void deleteFirst(node* &head) {
if (head) {
node *ptr = head;
head = head->next;
delete ptr;
}
}
///Delete last node
void deleteLast(node* &head) {
node *beforeLast;
node *last = findLast(head, &beforeLast);
if (last) {
if (beforeLast) {
beforeLast->next = NULL;
}
if (head == last) {
head = NULL;
}
delete last;
}
}
///Delete Specific Node
void deleteData(node* &head, int d) {
node *before = NULL;
node *ptr = head;
while (ptr) {
if (ptr->data == d) {
if (before) {
before->next = ptr->next;
}
if (head == ptr) {
head = head->next;
}
delete ptr;
return;
}
before = ptr;
ptr = ptr->next;
}
}
void takeInput(node* &head) {
int d;
if (!((cin >> d) && (d != -1))) return;
node *last = findLast(head, NULL);
node *n = new node(d);
if (last) {
last->next = n;
} else {
head = n;
}
last = n;
while ((cin >> d) && (d != -1)) {
n = new node(d);
last->next = n;
last = n;
}
}
void print(node* head) {
while (head) {
cout << head->data << "=>";
head = head->next;
}
}
int main() {
node* head = NULL;
takeInput(head);
print(head);
cout << endl;
cout << endl << endl;
cout << "---------- Here The Insertion Process starts at different Positions -----------" << endl << endl;
cout << "Adding at End" << endl;
addToEnd(head, 9);
print(head);
cout << endl;
cout << "Adding at Position p" << endl;
int p, d;
cout << "Enter Position and data :" << endl;
if (cin >> p >> d) {
addAtPosition(head, p, d);
print(head);
cout << endl;
}
cout << "Adding at Front" << endl;
cout << "Enter data to add at front : " << endl;
if (cin >> d) {
addAtFront(head, d);
print(head);
cout << endl;
}
cout << endl << endl << endl;
cout << "-------------------- NOW LETS PERFORM DELETION ------------------" << endl << endl;
cout << "Deleting first node :" << endl;
deleteFirst(head);
print(head);
cout << endl;
cout << endl << "Deleting Last node :" << endl;
deleteLast(head);
print(head);
cout << endl;
cout << "deleting specific node" << endl;
cout << "Enter data to delete" << endl;
if (cin >> d) {
deleteData(head, d);
print(head);
cout << endl;
}
cout << "deleting remaining nodes" << endl;
node *ptr = head;
while (ptr) {
node *temp = ptr;
ptr = ptr->next;
delete temp;
}
return 0;
}
That being said, you really should be using std::list (double linked) or std::forward_list (single linked) instead. Let the STL do the hard work for you.
In your delete node function, you are changing the value of the local variable, ptr. But you are not changing the "next" pointer of the node pointing to the one you want to delete. So it stays in the list. The value of your local pointer becomes irrelevant as soon as you leave the function.
This line is the problem: ptr=ptr->next->next;
You need to change its "next" value. Something like (I haven't compiled and tested this; it is a suggestion to point you in the right direction.)
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{ /* pointer->next points to the node you want to delete.
point it instead to the one beyond */
ptr->next=ptr->next->next;
return;
}
ptr=ptr->next;
}
}
You will then have a memory leak, as you are not freeing the memory to the node you are deleting.
You'd need something like:
void deleteData(node* head,int d)
{
node*ptr=head;
while(ptr->next!=NULL)
{
if(ptr->next->data==d)
{ /* pointer->next points to the node you want to delete.
point it instead to the one beyond,
freeing the memory of the deleted node */
node * pNodeToDelete = ptr->next;
ptr->next=ptr->next->next;
free(pNodeToDelete)
return;
}
ptr=ptr->next;
}
}

Doubly linked list .Logical error

Here's a program which takes student name and registration number from user and stores it in a doubly linkedlist. And I'm inserting nodes at the end . But there is a logical error in while loop. It takes Name and Reg. Number but then stops.Please Help
class dlist{
struct node{
string name;
string regno;
node *next;
node *prev;
};
public:
node *head,*tail;
void insert(string,string);
void search(string);
};
void dlist::insert(string nn, string r)
{
node *ptr,*newNode;
tail=head;
newNode= new node;
newNode->name=nn;
newNode->regno=r;
newNode->next=NULL;
newNode->prev=NULL;
if(!head)
{
head=newNode;
tail=head;
}
else
{
ptr = head;
while(ptr)
ptr=ptr->next;
newNode->next=tail;
newNode->prev=ptr;
ptr->next-newNode;
}
}
void dlist::search(string n){
node *ptr;
ptr=head;
while(ptr->next!=NULL)
{
if(ptr->name==n)
cout<<"Name found..." <<ptr->name<<endl;
ptr=ptr->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
dlist dl;
string nn;
string n;
string r;
for(int i=0;i<5;i++)
{
cout<<"Enter student's name: ";
cin>>nn;
cout<<"Enter student's Registration Number: ";
cin>>r;
dl.insert(nn,r);
}
cout<<"Enter a name to search: ";
cin>>n;
dl.search(n);
}
You are having problems using the tail pointer. I've improved your code below and added comments explaining what I did. If you have any more questions about what I did, ask in the comments and I'll be happy to clarify.
#include <tchar.h>
#include <string>
#include <iostream>
class dlist {
struct node {
string name;
string regno;
node* next;
node* prev;
};
public:
node* head, * tail;
void insert(string, string);
void search(string);
/**
* You're using classes, so go ahead and provide a definition for the
* constructor you're using. This one will initialize head and tail to
* be null
*/
dlist() : head(NULL), tail(NULL) {}
/**
* It's important to delete any memory you allocate so that you don't create a memory
* leak.
*/
virtual ~dlist() {
while (head) {
node* deleteMe = head;
head = head->next;
delete deleteMe;
}
}
};
void dlist::insert(string nn, string r) {
// there is no good reason to set tail=head at this time.
// and you don't need a ptr, that's why we have tail.
node* newNode = new node;
newNode->name = nn;
newNode->regno = r;
// no need to set newNode->prev now, as that will be taken care of later
newNode->next = NULL;
// if we don't have any nodes yet, we need to set head = newNode
// and make head point to tail, which is also head.
if (!head) {
head = newNode;
tail = head;
head->next = tail;
tail->prev = head;
// head points to itself both ways
} else {
// we already have some nodes, so go ahead and
// set newNode to go right after tail, then set tail = newNode
// this will work even when head == tail
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void dlist::search(string n) {
// it's nice to assign variables when you declare them, when you can
node* ptr = head;
// basically the same as what you had, but we are checking in the case
// that tail has the node we want, which your code did not do.
while (ptr) {
if (ptr->name == n) {
cout << "Name found... " << ptr->name << endl;
}
ptr = ptr->next;
}
}
int _tmain(int argc, _TCHAR* argv[]) {
dlist dl;
string nn;
string n;
string r;
for (int i = 0; i < 5; i++) {
cout << "Enter student's name: ";
cin >> nn;
cout << nn << endl;
cout << "Enter student's Registration Number: ";
cin >> r;
cout << r << endl;
dl.insert(nn, r);
}
cout << "Enter a name to search: ";
cin >> n;
dl.search(n);
return 0;
}

Inserting data in a singly link list by specifying the nth node position

So the logic goes like this:
Suppose the link list consists of (6,7,8) as data and I pass insert(1,5),so the list will be as (5,6,7,8).
Similarly on insert(3,2) link list is (6,7,2,8).
I tried compiling the below code but it gives me an error stating-
Undefined reference to main by '-start'
I tried debugging,even searching for answers but found no help.Kindly suggest a solution.Any further suggestions and bug fixes shall be welcomed.
(I have used codepad for compiling)
#include<iostream>
using namespace std;
class Link_no
{
struct node
{
int data;
node *next;
};
void insert(int n,int d,node *head)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print(node *start)
{
node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
int main()
{
node *head=NULL;
Link_no o1;
o1.insert(1,5,head);
o1.insert(2,7,head);
o1.insert(1,9,head);
o1.print(head);
return 0;
}
}
C++ isnt java, the main does not belong inside a class. The compiler complains because there is no int main() in your code only a int Link_no::main() but that is not the entry point of the program.
Take out int main() from class Link_no. Take out struct node from class Link_no. It should compile.
The following compiles
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
temp1=head;
{
for(int i=0;i<n-1;i++)
{
temp1=temp1->next;
}
temp->next=temp1;
temp1=temp;
}
}
void print()
{
node *temp=head;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.insert(2,7);
o1.insert(1,9);
o1.print();
return 0;
}
It does not completely do what you want yet only prints out 5 and 9 as data so you need to debug some more.
Edit:
I suggest you take a paper and pen and manually try to do what you're doing in your else since there is something going wrong there.
If you can't find it out on your own the following works for me, I haven't tried testing for extreme cases yet.
#include<iostream>
using namespace std;
class Link_no
{
private:
struct node
{
int data;
node *next;
};
node *head;
public:
Link_no(){
head = nullptr;
}
void insert(int n,int d)
{
node *temp=new node();
temp->data=d;
temp->next=NULL;
node *temp1;
if(n==1)
{
temp->next=head;
head=temp;
return;
}
else
{
cout << "foo" << endl;
temp1=head;
for(int i=1;i<n-1;i++)
{
temp1=temp1->next;
}
node *temp2 = temp1->next;
temp1->next = temp;
temp->next=temp2;
}
}
void print()
{
node *temp=head;
cout << "link" << endl;
while(temp!=NULL)
{
cout << "data is " << temp->data<<endl;
temp=temp->next;
}
}
};
int main()
{
Link_no o1;
o1.insert(1,5);
o1.print();
o1.insert(2,7);
o1.print();
o1.insert(1,9);
o1.insert(2,6);
o1.print();
return 0;
}