doublly linked list
insert at start
inert at mid
insert at end
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
node *pre;
};
class list
{
node *head;
node *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void insertatend(int value)
{
node *n=new node;
n->data=value;
n->next=NULL;
n->pre=NULL;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
tail->next=n;
n->pre=tail;
tail=n;
}
}
void display()
{
if(head==NULL)
{
cout<<"empty"<<endl;
}
else
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
}
void displayreverse()
{
if(head==NULL)
{
cout<<"empty"<<endl;
}
else
{
node *temp=tail;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->pre;
}
}
}
void insertatstart(int value)
{
node *n=new node;
n->data=value;
n->pre=NULL;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
n->next=head;
head->pre=n;
head=n;
}
}
void insertatmid(int value)
{
node *n=new node;
n->data=value;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
node *slow=head;
node *fast=head;
while(fast!=tail && fast->next!=tail)
{
fast=fast->next->next;
slow =slow->next;
}
n->next=slow->next;
slow->next->pre=n;
slow->next=n;
n->pre=slow;
}
}
};
int main()
{
list l;
l.insertatend(12);
l.insertatend(43);
l.insertatend(65);
l.insertatstart(136);
l.insertatstart(13);
l.insertatmid(17);
l.display();
cout<<"now reverse display "<<endl;
l.displayreverse();
}
Different trick's , html code, c++ code, World Current Affairs MCQs , World Current Affairs MCQs , General Knowledge MCQs, ptcl user trick's, tech trick's. All about General Knowledge in the Websit. and mcqs pastpapers all pakistan university e.t.c UOS, PU. PDF free online book's at this website. .
Showing posts with label insert at end. Show all posts
Showing posts with label insert at end. Show all posts
Sunday, October 27, 2019
Singlly linked list.insert at start.insert at mid,insert at end
Singlly linked list
insert at start
insert at mid
insert at end
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
node *head;
node *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void insertatend(int value)
{
node *n=new node;
n->data=value;
n->next=NULL;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
tail->next=n;
tail=n;
}
}
void display()
{
if(head==NULL)
{
cout<<"empty"<<endl;
}
else
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
}
void insertatstart(int value)
{
node *n=new node;
n->data=value;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
n->next=head;
head=n;
}
}
void insertatmid(int value)
{
node *n=new node;
n->data=value;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
node *slow=head;
node *fast=head;
while(fast!=tail && fast->next!=tail)
{
fast=fast->next->next;
slow =slow->next;
}
n->next=slow->next;
slow->next=n;
}
}
};
int main()
{
list l;
l.insertatend(12);
l.insertatend(43);
l.insertatend(65);
l.insertatstart(136);
l.insertatstart(13);
l.insertatmid(17);
l.display();
}
insert at start
insert at mid
insert at end
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
node *head;
node *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void insertatend(int value)
{
node *n=new node;
n->data=value;
n->next=NULL;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
tail->next=n;
tail=n;
}
}
void display()
{
if(head==NULL)
{
cout<<"empty"<<endl;
}
else
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
}
void insertatstart(int value)
{
node *n=new node;
n->data=value;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
n->next=head;
head=n;
}
}
void insertatmid(int value)
{
node *n=new node;
n->data=value;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
node *slow=head;
node *fast=head;
while(fast!=tail && fast->next!=tail)
{
fast=fast->next->next;
slow =slow->next;
}
n->next=slow->next;
slow->next=n;
}
}
};
int main()
{
list l;
l.insertatend(12);
l.insertatend(43);
l.insertatend(65);
l.insertatstart(136);
l.insertatstart(13);
l.insertatmid(17);
l.display();
}
Subscribe to:
Posts (Atom)
