Multiply Linked List Cpp Program Basics to algorithms part 6

Cpp Program for Multiply Linked List

Basics to algorithms part 6


Multiply Linked List as we have discussed Earlier , in Linked List part 2 is a linked list with multiple pointers each with a different arrangement, for more details about multiply linked list please refer to Linked List Part 2



Program for multiply Linked List:

//
// MultipltLinkedList.cpp
// Multiply Linked List
// Created by Kartik Datar on 25/02/18.
// Copyright © 2018 www.introtoalgo.com. All rights reserved.
//
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Example
{
string id,nm;
struct Example *nextid, *nextnm;
};
struct Example *oneid, *onenm, *check, *newr, *storeold;
void insert()
{
newr=new(struct Example);
cout<<"Enter an Id no. : ";
cin>>newr->id;
cout<<"Enter name : ";
cin>>newr->nm;
newr->nextid=NULL;
newr->nextnm=NULL;
}
void sortid()
{
storeold=NULL;
if(oneid==NULL)
{
oneid=newr;
}
else
{
check=oneid;
while(check != NULL)
{
if(check->id > newr->id)
{
if(storeold==NULL)
{
newr->nextid=check;
oneid=newr;
break;
}
else
{
storeold->nextid=newr;
newr->nextid=check;
break;
}
}
else if(check->nextid == NULL)
{
check->nextid=newr;
break;
}
else
{
storeold=check;
check=check->nextid;
}
}
}
}
void sortnm()
{
storeold=NULL;
if(onenm==NULL)
{
onenm=newr;
}
else
{
check=onenm;
while(check != NULL)
{
if(check->nm > newr->nm)
{
if(storeold==NULL)
{
newr->nextnm=check;
onenm=newr;
break;
}
else
{
storeold->nextnm=newr;
newr->nextnm=check;
break;
}
}
else if(check->nextnm == NULL)
{
check->nextnm=newr;
break;
}
else
{
storeold=check;
check=check->nextnm;
}
}
}
}
void display()
{
int a;
cout<<"Decide to print by ID or name : ";
cout<<"\n 1 for id and 2 for name : ";
cin>>a;
if(a==1)
{
check=oneid;
while(check!=NULL)
{
cout<<"\n ID is : "<<check->id;
cout<<"\n Name is : "<<check->nm;
check=check->nextid;
}
}
if(a==2)
{
check=onenm;
while(check!=NULL)
{
cout<<"\n ID is : "<<check->id;
cout<<"\n Name is : "<<check->nm;
check=check->nextnm;
}
}
else if(a!=2 && a!=1)
{
cout<<"Not valid input. Data cannot be displayed.";
}
}
int main()
{
oneid=onenm=NULL;
string b="",c="";
while(b!="0")
{
insert();
sortid();
sortnm();
cout<<"\nInsert again?? \n If not enter 0 : ";
cin>>b;
}
while(c!="0")
{
display();
cout<<"\nDisplay again?? \n If not enter 0 : ";
cin>>c;
}
}






Written by, Sarvesh Bhatnagar.
Code By, Kartik Datar

Popular Posts