Wednesday 29 November 2017

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




No comments:

Post a Comment

Amazon1Ads