Link List for the beginners of cpp, who want to get the idea about link list before starting OOP. its implemented by struct for best teaching the design standards to beginners.
Link List by Fraz Jamshed
/*==============================================================================
Linear LinkList By Structure : Objective - Design Demonstration
================================================================================
Focused on Reusable functions to demonstrate best concise functionality
of LinkList in combination of effective Menu Operations , designed only
for a user friendly interface. Combination of Both results into a
reusable ,readable and effective code, design and performance wise.
________________________________________________________________________________
WarGame_Winter2009-10 :: WarBird/BCSS07A013_Project_Merecenary_22/3rd_OOP_CPP
==============================================================================*/
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
struct Node;
struct Node{
char *Name;
int ID;
Node *Next;
};
Node * Head = NULL;
//============================================//
// Function Prototypes
//============================================//
void MenuOperation( char );
void AddNode( char *, int );
void AddTo_Front( Node * );
void AddTo_End( Node * );
void AddBefore_InBetween( Node *, Node * );
void AddAfter_InBetween( Node *, Node * );
void AddNode_Menu( Node * );
Node* Allocate();
void SearchMenu();
Node* SearchBy_ID( int );
Node* SearchBy_Name( char* );
void ModifyMenu();
void DeleteMenu();
void DeleteFrom_Start();
void DeleteFrom_End();
void DeleteIn_Between( Node * );
void Display();
//==========================================//
// Add Node Methods
//==========================================//
Node* Allocate()
{
Node * Ptr = new Node ;
return Ptr;
}
//===========================================//
void AddNode( char* tName, int tID )
{
Node * Ptr = Allocate();
Ptr->Name = new char[strlen(tName)];
strcpy(Ptr->Name,tName);
Ptr->ID = tID;
Ptr->Next = NULL;
if(Head)
AddNode_Menu(Ptr);
else
Head=Ptr;
}
//======================================//
void AddTo_Front( Node * NewNode )
{
NewNode->Next = Head ;
Head = NewNode ;
}
//=========================================//
void AddTo_End( Node * NewNode )
{
Node * i = Head ;
for( ; i->Next!=NULL; i=i->Next )
{;}
i->Next = NewNode ;
}
//=======================================//
void AddBefore_InBetween( Node * Flag , Node * NewNode )
{
Node * i = Head ;
for( ; i->Next != Flag; i=i->Next )
{;}
i->Next = NewNode ;
NewNode->Next = Flag ;
}
//=========================================//
void AddAfter_InBetween( Node * Flag , Node * NewNode )
{
NewNode->Next=Flag->Next;
Flag->Next = NewNode ;
}
//=========================================//
//=========================================//
// Search Node Methods
//=========================================//
Node* SearchBy_Name( char* tName )
{
Node * i = Head ;
for( ; i!=NULL ; i = i->Next )
if(!strcmp(tName,i->Name))
break;
return i;
}
//========================================//
Node* SearchBy_ID( int tID )
{
Node * i = Head ;
for( ; i!=NULL ; i = i->Next )
if(tID == i->ID)
break;
return i;
}
//==========================================//
//=========================================//
// Delete Node Methods
//=========================================//
void DeleteFrom_Start()
{
if(Head->Next)
{
Node * Temp = Head;
Head = Head -> Next;
delete Temp;
}
else
{
delete Head;
Head = NULL;
}
}
//=========================================//
void DeleteFrom_End()
{
if( !Head->Next )
{
delete Head;
Head = NULL;
}
else
{
Node * i = Head;
Node * j = NULL;
for( ; i->Next!=NULL ; i = i->Next )
{j=i;}
j->Next = NULL;
delete i;
}
}
//========================================//
void DeleteIn_Between( Node * Current )
{
Node * j = NULL;
Node * i = Head;
for( ; i!=Current ; i=i->Next)
{j=i;}
j->Next = i->Next;
delete Current;
}
//========================================//
//======================================//
// Display
//======================================//
void Display()
{
for(Node *i=Head;i!=NULL;i=i->Next)
{cout<<i->Name<<":"<<i->ID<<endl;}
}
//=======================================//
// End of Re-usable functions, below are Menu Operation Launchers...
// End of Functionality
void main()
{
while(1)
{
clrscr();
cout<<"[1] Add\n[2] Search\n[3] Modify\n[4] Display\n[5] Delete\n[0] Exit\n";
char Choice = getch();
switch(Choice)
{
case '1':
case '2':
case '3':
case '4':
case '5':
clrscr();
MenuOperation(Choice);
break;
case '0':
exit(1);
default:
cout<<"\a";
}
}
}
void MenuOperation( char Action )
{
if(Head||Action=='1')
switch( Action )
{
case '1':
{
char tName[25];
int tID;
cout<<"Enter Name:";
gets(tName);
cout<<"Enter Age:";
cin>>tID;
AddNode(tName,tID);
break;
}
case '2':
{
SearchMenu();
break;
}
case '3':
{
ModifyMenu();
break;
}
case '4':
{
clrscr();
Display();
getch();
break;
}
case '5':
{
DeleteMenu();
break;
}
}
else
{
cout<<"Unable to Perfom operation on [Empty List]"<<endl;
getch();
}
}
//======================================//
// Add Menu Operation Launcher
//======================================//
void AddNode_Menu( Node * NewNode )
{
clrscr();
cout<<"[1] Add to Front\n[2] Add to End\n[3] Add By Search_Before\n[4] Add By Search_After\n[0] ..Back"<<endl;
char Choice = getch();
Node* Flag = NULL;
if(Choice!='0')
{
switch(Choice)
{
case '1':
{
AddTo_Front(NewNode);
break;
}
case '2':
{
AddTo_End(NewNode);
break;
}
case '3':
case '4':
{
clrscr();
cout<<"[1] Add Before/After by Searching Name \n[2] Add Before/After by Searching ID\n";
char SChoice = getch();
switch(SChoice)
{
case '1':
{
char tName[25];
cout<<"Enter Name to Search Node : ";
gets(tName);
Flag = SearchBy_Name(tName);
break;
}
case '2':
{
int tID;
cout<<"Enter ID to Search Node : ";
cin>>tID;
Flag = SearchBy_ID(tID);
break;
}
default:
cout<<"\a";
}
if(Choice=='3')
{
if(Flag)
{
if( Flag == Head )
AddTo_Front(NewNode);
else
AddBefore_InBetween(Flag,NewNode);
}
}
else
{
if( Flag == Head || !Flag->Next )
{
NewNode->Next = Flag->Next;
Flag->Next = NewNode;
}
else
AddAfter_InBetween(Flag,NewNode);
}
break;
}
default:
cout<<"\a";
}
cout<<(Flag?"Added":"Couldn't Add");
getch();
}
}
//======================================//
// Search Menu Operation Launcher
//======================================//
void SearchMenu()
{
char Choice = -1;
while(Choice)
{
clrscr();
cout<<"[1] Search by Name\n[2] Search by ID\n[0] ..Back"<<endl;
char Choice = getch();
Node* Flag = NULL;
if(Choice!='0')
{
switch(Choice)
{
case '1':
{
char tName[25];
cout<<"Enter Name to Search: ";
gets(tName);
Flag = SearchBy_Name(tName);
break;
}
case '2':
{
int tID;
cout<<"Enter ID to Search: ";
cin>>tID;
Flag = SearchBy_ID(tID);
break;
}
default:
cout<<"\a";
}
cout<<(Flag?"Found":"Not Found");
getch();
}
else
break;
}
}
//==========================================//
// Modify Menu Operation Launcher
//=========================================//
void ModifyMenu()
{
char Choice = -1;
while(Choice)
{
clrscr();
cout<<"[1] Modify Name\n[2] Modify ID\n[0] ..Back"<<endl;
char Choice = getch();
Node* Flag = NULL;
if(Choice!='0')
{
switch(Choice)
{
case '1':
{
char tName[25];
cout<<"Enter Name to Search then Modify: ";
gets(tName);
Flag = SearchBy_Name(tName);
if(Flag)
{
cout<<"Enter New Name to Modify ["<<tName<<"]: ";
gets(tName);
if(strcmp(Flag->Name,tName))
{
delete Flag->Name;
Flag->Name = new char[strlen(tName)];
}
strcpy(Flag->Name,tName);
}
break;
}
case '2':
{
int tID;
cout<<"Enter ID to Search";
cin>>tID;
Flag = SearchBy_ID(tID);
if(Flag)
{
cout<<"Enter New ID to Modify ["<<tID<<"]: ";
cin>>tID;
Flag->ID=tID;
}
break;
}
default:
cout<<"\a";
}
cout<<(Flag?"Modified":"Nothing Found to Modify");
getch();
}
else
break;
}
}
//=====================================//
// Delete Menu Operation Launcher
//=====================================//
void DeleteMenu()
{
char Choice = -1;
while( Choice && Head )
{
clrscr();
cout<<"[1] Del Node from End\n[2] Delete Node from Front\n[3] Delete By Search_Name\n[4] Delete By Search_ID\n[0] ..Back"<<endl;
char Choice = getch();
Node* Flag = NULL;
if(Choice!='0')
{
switch(Choice)
{
case '1':
{
DeleteFrom_Start();
break;
}
case '2':
{
DeleteFrom_End();
break;
}
case '3':
case '4':
{
switch(Choice)
{
case '3':
{
char tName[25];
cout<<"Enter Name to Search then Delete Node: ";
gets(tName);
Flag = SearchBy_Name(tName);
break;
}
case '4':
{
int tID;
cout<<"Enter ID to Search then Delete Node: ";
cin>>tID;
Flag = SearchBy_ID(tID);
break;
}
}
if(Flag)
{
if( Flag == Head || !Flag->Next )
DeleteFrom_End();
else
DeleteIn_Between(Flag);
}
break;
}
default:
cout<<"\a";
}
cout<<(Flag?"Deleted":"Couldn't Delete");
getch();
}
else
break;
}
}
//========================//
// TERMINATION POINT
//========================//
warbird43 -1 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.