Social Icons

banner image

double link list types and types of inertion mode and dynamic memory anagemnt in c++

// Dobly Link List At First
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*tail=NULL,*nptr,*ptr;

 void insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=head;
nptr->flink=ptr;
nptr->plink=NULL;
ptr->plink=nptr;
head=nptr;
}
}

 void traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
traverse();
}
========================================================================
// Dobly Link List At Last
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*tail=NULL,*nptr,*ptr;

insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=tail;
nptr->plink=ptr;
nptr->flink=NULL;
ptr->flink=nptr;
tail=nptr;
}
}

traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
traverse();
}
========================================================================
// Dobly Link List AT Any Place
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*tail=NULL,*nptr,*ptr;

insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=head;
nptr->flink=ptr;
nptr->plink=NULL;
ptr->plink=nptr;
head=nptr;
}
}

insertm()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
ptr=head;
while(ptr!=NULL)
{
if(ptr->info==5)
{
break;
}
else
{
ptr=ptr->flink;
}
}
nptr->flink=ptr->flink;
nptr->plink=ptr;
ptr->flink=nptr;
nptr->flink->plink=nptr;
}

delete1()
{
ptr=tail;
tail=ptr->plink;
tail->flink=NULL;
}

traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
      insertm();
traverse();
delete1();
}
========================================================================
// Dobly Link List AT Any Place before
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *plink,*flink;
}*head=NULL,*prev,*tail=NULL,*nptr,*ptr;

insert()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
if (head==NULL)
{
head=nptr;
tail=nptr;
nptr->plink=NULL;
nptr->flink=NULL;
}
else
{
ptr=head;
nptr->flink=ptr;
nptr->plink=NULL;
ptr->plink=nptr;
head=nptr;
}
}

insertm()
{
nptr=new node;
int item;
cout<<"Enter the Value";
cin>>item;
nptr->info=item;
ptr=head;
while(ptr!=NULL)
{
prev=ptr;
if(ptr->info==5)
{
break;
}
else
{
ptr=ptr->flink;
}
}
nptr->plink=ptr->plink;
nptr->flink=ptr;
ptr->plink=nptr;
nptr->plink->flink=nptr;
}

deletel()
{
ptr=tail;
tail=ptr->plink;
tail->flink=NULL;
}

traverse()
{
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->info<<endl;
ptr=ptr->flink;
}
}

int main()
{
int n,i;
cout<<"Enter the no of element you want to enter";
cin>>n;
for(i=1;i<=n;i++)
{
insert();
}
      insertm();
traverse();
}
========================================================================pointers in double link list
#include <stdio.h>
#include <stdlib.h>
struct node
{
  int data;
  struct node *next;
  struct node *prev;
};
void deleteNode(struct node **head_ref, struct node *del)
{
  if(*head_ref == NULL || del == NULL)
    return;

   if(*head_ref == del)
    *head_ref = del->next;

  if(del->next != NULL)
    del->next->prev = del->prev;

  if(del->prev != NULL)
    del->prev->next = del->next;     

  free(del);
  return;
}     

void push(struct node** head_ref, int new_data)
{
  struct node* new_node =
       (struct node*) malloc(sizeof(struct node));

  new_node->data  = new_data;

  new_node->prev = NULL;

  new_node->next = (*head_ref);    

  if((*head_ref) !=  NULL)
   (*head_ref)->prev = new_node ;    

   (*head_ref)    = new_node;
}

void printList(struct node *node)
{
  while(node!=NULL)
  {
   printf("%d ", node->data);
   node = node->next;
  }
}
int main()
{
  struct node* head = NULL;
  push(&head, 2);
  push(&head, 4);
  push(&head, 8);
  push(&head, 10);

  printf("\n Original Linked list ");
  printList(head);
  deleteNode(&head, head);  
  deleteNode(&head, head->next);  
  deleteNode(&head, head->next);  
  printf("\n Modified Linked list ");
  printList(head);           

  getchar();

}
double link list types and types of inertion mode and dynamic memory anagemnt in c++ double link list types and types of inertion mode and dynamic memory anagemnt in c++ Reviewed by Shobhit Goel on November 07, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.