Showing posts with label header. Show all posts
Showing posts with label header. Show all posts

Thursday, 30 November 2017

Circular Linked List Implementation Data Structure in C/ C++

#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
struct node
{
int info;
node *link;
};
void insert(struct node **start,int num)
{
cout<<"First";

node *temp=new node;
temp->info=num;

temp->link=NULL;

(*start)=temp;

}
void insertEnd(node *start,int num)
{
cout<<"\nLast:";
node *temp;
while(start->link!=NULL)
{
start=start->link;
}
temp=new node;
temp->info=num;
temp->link=NULL;
start->link=temp;

}

void makeCircular(node *start)
{
node *temp=start;
while(start->link!=NULL)
{

    start=start->link;
}
if(start->link==NULL)
{
start->link=temp;
}
}
void disp1(node *start)
{
node *temp=start;
while(temp->link!=start)
{
cout<<" "<<temp->info;
    temp=temp->link;
}
cout<<" "<<temp->info;
}


main()
{
struct node *start=NULL;

int num,x,y;
cout<<"In Main";
insert(&start,1);
insertEnd(start,5);
insertEnd(start,3);
insertEnd(start,4);
insertEnd(start,6);
insertEnd(start,7);
insertEnd(start,8);
insertEnd(start,9);
insertEnd(start,10);

makeCircular(start);
disp1(start);

}

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




Amazon1Ads