A Sample LinkedList implementation in c++ is demonstrated below. The list maintains persons records with age and name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Person{ | |
int id; | |
string name; | |
Person *next; | |
}; | |
struct List{ | |
Person *head; | |
}; | |
[/sourcecode] | |
All the operations of the linkedlist are here: | |
[sourcecode language="cpp"] | |
#include<iostream> | |
#include<string> | |
#include<conio.h> | |
using namespace std; | |
struct Person{ | |
int id; | |
string name; | |
Person *next; | |
}; | |
struct List{ | |
Person *head; | |
}; | |
//initialize linked list | |
void Initialize(List *personlist); | |
// Insert node in front of linked list | |
void InsertFront(List *personlist); | |
// Insert node at the rear of linked list | |
void InsertRear(List *personlist); | |
// Delete node from front of linked list | |
void DeleteFront(List *personlist); | |
void DeleteRear(List *personlist); | |
// Delete a node from any position of linked list | |
void DeleteAny(List *personlist); | |
// Searching in linked list | |
void SearchPerson(List *personlist); | |
void PrintList(List *personlist); | |
//Deleting all nodes in linked list | |
void EmptyList(List *personlist); | |
// Reversing linked list | |
void Reverse(List *personlist); | |
//sorting linked list | |
void SortPersons(List *personlist); | |
int main() | |
{ | |
List *personlist = NULL; | |
personlist = new List; | |
Initialize(personlist); | |
int choice = 0; | |
do | |
{ | |
cout<<endl<<endl<<"\t\t Press"<<endl; | |
cout << "\t\t [1] key To Add person in front" << endl; | |
cout << "\t\t [2] key To Add person in rear" << endl; | |
cout << "\t\t [3] key To Delete person from front" << endl; | |
cout << "\t\t [4] key To Delete person from rear" << endl; | |
cout << "\t\t [5] key To Print person List" << endl; | |
cout << "\t\t [6] key To Reverse person List" << endl; | |
cout << "\t\t [7] key To Delete all persons" << endl; | |
cout << "\t\t [8] key To Delete a person by Id" << endl; | |
cout << "\t\t [9] key To Search" << endl; | |
cout << "\t\t [10] key To Sort Persons" << endl; | |
cout << "\t\t [11] key To Exit" << endl; | |
cout << "\t\t ";cin >> choice; | |
switch (choice) | |
{ | |
case 1: | |
InsertFront(personlist); | |
PrintList(personlist); | |
break; | |
case 2: | |
InsertRear(personlist); | |
PrintList(personlist); | |
break; | |
case 3: | |
DeleteFront(personlist); | |
break; | |
case 4: | |
DeleteRear(personlist); | |
break; | |
case 5: | |
PrintList(personlist); | |
break; | |
case 6: | |
Reverse(personlist); | |
break; | |
case 7: | |
EmptyList(personlist); | |
break; | |
case 8: | |
DeleteAny(personlist); | |
break; | |
case 9: | |
SearchPerson(personlist); | |
break; | |
case 10: | |
SortPersons(personlist); | |
break; | |
case 11: | |
exit(0); | |
break; | |
default: | |
cout<<"Wrong choice"<<endl; | |
break; | |
} | |
} | |
while(true); | |
} | |
void Initialize(List *person) | |
{ | |
person->head = NULL; | |
} | |
void PrintList(List *personlist) | |
{ | |
Person * temp; | |
temp = personlist->head; | |
cout <<endl<<"Total Persons in the list: "<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
int i = 0; | |
if (temp == NULL) | |
{ | |
cout<<"List is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
while (temp != NULL) | |
{ | |
i++; | |
cout <<"P"<<i<<")\t Id: "<< temp->id <<" - Name: "<< temp->name<<endl; | |
temp = temp->next; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
} | |
void Reverse(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
return; | |
} | |
Person *person = personlist->head; | |
Person *person_n; | |
Person *temp; | |
temp = person; | |
person = person->next; | |
person_n = person; | |
while (person_n->next != NULL) | |
{ | |
person_n = person_n->next; | |
person->next = temp; | |
temp = person; | |
person = person_n; | |
} | |
person_n->next = temp; | |
Person *person1 = personlist->head; | |
person1->next = NULL; | |
personlist->head = person_n; | |
} | |
void InsertFront(List *personlist) | |
{ | |
int id; | |
string name; | |
cout <<"Entere id: "; | |
cin >> id; | |
cout <<"Entere Name: "; | |
cin >> name; | |
Person *newPerson = new Person(); | |
newPerson->id = id; | |
newPerson->name = name; | |
if (personlist->head == NULL) | |
{ | |
personlist->head = newPerson; | |
newPerson->next = NULL; | |
} | |
else | |
{ | |
newPerson->next = personlist->head; | |
personlist->head = newPerson; | |
} | |
} | |
void InsertRear(List *personlist) | |
{ | |
int id; | |
string name; | |
cout <<"Enter id: "; | |
cin >> id; | |
cout <<"Enter Name: "; | |
cin >> name; | |
Person *newPerson = new Person(); | |
newPerson->id = id; | |
newPerson->name = name; | |
if (personlist->head == NULL) | |
{ | |
personlist->head = newPerson; | |
newPerson->next = NULL; | |
} | |
else | |
{ | |
Person *temp = personlist->head; | |
while (temp->next != NULL) | |
{ | |
temp = temp->next; | |
} | |
temp->next = newPerson; | |
newPerson->next = NULL; | |
} | |
} | |
void DeleteFront(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
else | |
{ | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
delete person; | |
personlist->head = NULL; | |
} | |
else | |
{ | |
personlist->head = person->next; | |
delete person; | |
} | |
cout<<"Success: Person is deleted"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
} | |
void DeleteRear(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
else | |
{ | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
delete person; | |
personlist->head = NULL; | |
} | |
else | |
{ | |
Person *tmp = person; | |
while (person->next != NULL) | |
{ | |
tmp = person; | |
person = person->next; | |
} | |
delete person; | |
tmp->next = NULL; | |
} | |
cout<<"Success: Person is deleted"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
} | |
void DeleteAny(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
else | |
{ | |
int ID; | |
bool IsDel = false; | |
cout <<"Enter person id to delete:\t"; | |
cin>>ID; | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
if (person->id == ID) | |
{ | |
delete person; | |
personlist->head = NULL; | |
IsDel = true; | |
} | |
else | |
{ | |
IsDel = false; | |
} | |
} | |
else if (person->next != NULL) | |
{ | |
if (person->id == ID) | |
{ | |
personlist->head = person->next; | |
delete person; | |
IsDel = true; | |
} | |
else | |
{ | |
IsDel = false; | |
} | |
} | |
if (!IsDel && person->next != NULL) | |
{ | |
Person *tmp = person; | |
person = person->next; | |
while (person->next != NULL) | |
{ | |
if (person->id == ID) | |
{ | |
tmp->next = person->next; | |
delete person; | |
IsDel = true; | |
break; | |
} | |
tmp = person; | |
person = person->next; | |
} | |
if (person->id == ID) | |
{ | |
delete person; | |
tmp->next = NULL; | |
IsDel = true; | |
} | |
} | |
if (IsDel) | |
{ | |
cout<<"Success: Person is deleted"<<endl; | |
} | |
else | |
{ | |
cout<<"Sorry: Could not find person"<<endl; | |
} | |
cout<<"------------------------------------------------"<<endl; | |
} | |
} | |
void EmptyList(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
else | |
{ | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
delete person; | |
personlist->head = NULL; | |
} | |
else | |
{ | |
Person *tmp = person; | |
while (person->next != NULL) | |
{ | |
tmp = person; | |
person = person->next; | |
delete tmp; | |
} | |
personlist->head = NULL; | |
} | |
cout<<"Success: List is deleted"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
} | |
} | |
void SearchPerson(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
return; | |
} | |
int ID; | |
bool IsFound = false; | |
cout <<"Enter ID of the person to search:"; | |
cin>> ID; | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
if (person->id == ID) | |
{ | |
cout <<"Person Found:"<<endl; | |
cout<<person->name<<endl; | |
IsFound = true; | |
} | |
else | |
{ | |
IsFound = false; | |
} | |
} | |
else | |
{ | |
while (person != NULL) | |
{ | |
if (person->id == ID) | |
{ | |
cout <<"Person Found:"<<endl; | |
cout<<person->name<<endl; | |
IsFound = true; | |
break; | |
} | |
else | |
{ | |
IsFound = false; | |
} | |
person = person->next; | |
} | |
if (!IsFound) | |
{ | |
cout<<"Sorry: Person not found"<<endl; | |
} | |
} | |
cout<<"------------------------------------------------"<<endl; | |
} | |
void SortPersons(List *personlist) | |
{ | |
cout<<"------------------------------------------------"<<endl; | |
if (personlist->head == NULL) | |
{ | |
cout<<"Sorry: Person list is empty"<<endl; | |
cout<<"------------------------------------------------"<<endl; | |
return; | |
} | |
Person *person = personlist->head; | |
if (person->next == NULL) | |
{ | |
return; | |
} | |
Person *person_n = person; | |
int temp_id; | |
string temp_name; | |
while (person != NULL) | |
{ | |
person_n = person->next; | |
while (person_n != NULL) | |
{ | |
if (person->id < person_n->id ) | |
{ | |
temp_id = person->id; | |
person->id = person_n->id; | |
person_n->id = temp_id; | |
temp_name = person->name; | |
person->name = person_n->name; | |
person_n->name = temp_name; | |
} | |
person_n = person_n->next; | |
} | |
person = person->next; | |
} | |
} |
No comments:
Post a Comment