Showing posts with label header linked list. Show all posts
Showing posts with label header linked list. 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

Alternate Node Deletion in Singly Linked List in C/C++ Implementation

#include<iostream>
using namespace std;

struct node
{
int info;
node *link;
};

void insertFirst(node **start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
*start=temp;
cout<<"First:\n";
}

void insertLast(node *start,int num)
{
node *temp=new node;
temp->info=num;
temp->link=NULL;
while(start->link!=NULL)
{
start=start->link;
}
start->link=temp;
}

void deleteAlternate(node *start)
{
node *currnext=start->link;
node *nexttonext=start->link->link;
node *curr=start;
while(curr!=NULL &&curr->link!=NULL && curr->link->link!=NULL)
{
nexttonext=curr->link->link;
delete(curr->link);
curr->link=nexttonext;
curr=curr->link;
}
}




void disp(node *start)
{
while(start!=NULL)
{
cout<<" "<<start->info;
start=start->link;
}
cout<<"\n";
}





main()
{
node *start=NULL;
insertFirst(&start,1);
insertLast(start,2);
insertLast(start,3);
insertLast(start,4);
insertLast(start,5);
        cout<<"Before Deleting Linked List\n:";
disp(start);
deleteAlternate(start);
        cout<<"After Deleting Alternate Node:\n";
disp(start);
}

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