Skip to content

Instantly share code, notes, and snippets.

@worker8
Created March 16, 2014 08:03
Show Gist options
  • Select an option

  • Save worker8/9579968 to your computer and use it in GitHub Desktop.

Select an option

Save worker8/9579968 to your computer and use it in GitHub Desktop.
Example of traversing linked list
#include <iostream>
#include <string>
using namespace std;
class Address {}; //empty class about address
class Date{}; //to capture the date
class Person {
private:
string name;
int age;
Address add1; //you can add address instance in person class
Date dob; //you can add Date instance in Person class
public:
//default constructor
Person() {
name="";
age=0;
}
//overloaded constructor
Person(string n, int a) {
name=n;
age = a;
}
//getters
string getName(){return name;}
int getAge() {return age;}
//setters
void setName(string n) {name=n;}
void setAge(int a) {age=a;}
};
class AddressBook {
private:
struct Node {
Person p;
Node *next; //linked list
};
Node *head; //point to first node
public:
AddressBook(); //default constructor
~AddressBook() {cout << "----------\nBye Bye!"<<endl; }
void addNewPerson(Person); //same as appendNode
void displayAllPerson(); //show all the person
Person *search(string); //show all the person
};
//constructor
AddressBook::AddressBook() {
head = NULL;
}
//a methodm to add new person in the address book
void AddressBook::addNewPerson(Person person) {
Node *newNode = new Node;
newNode->p.setName(person.getName());
newNode->p.setAge(person.getAge());
newNode->next = NULL;
if (!head) {
head = newNode;
}
else
{
Node *temp = head; //create a temp pointer as head location
//go to the end of the list
while (temp->next !=NULL) {
temp = temp->next; //advance next location
}
temp->next = newNode;
}
}
//to display all the person details
void AddressBook::displayAllPerson() {
Node *temp = head; //create a temp pointer as head location
while (temp!=NULL) {
cout << temp->p.getName() << " : " << temp->p.getAge() << endl;
temp = temp->next;
}
}
Person *AddressBook::search(string search_term){
cout << "search term:" << search_term << endl;
Node *temp = head; //create a temp pointer as head location
while (temp!=NULL) {
int x = temp->p.getName().compare(search_term);
// cout << temp->p.getName() << " : " << temp->p.getAge() << endl;
if (x == 0){ // equals to 0 means found.
// cout << "WAHHHHH SAME LEH";
return &temp->p;
}
temp = temp->next;
}
return NULL;
}
int main() {
AddressBook book; //instance an address book
//hard code. This should be entered by user
Person p1("Lala", 45);
Person p2("Joe", 18);
Person p3("Kent", 23);
book.addNewPerson(p1);
book.addNewPerson(p2);
book.addNewPerson(p3);
book.displayAllPerson();
string temp;
cout << "Enter name to be searched: ";
cin >> temp;
Person *person_pointer = book.search(temp);
if (person_pointer == NULL) {
cout << "Result: NOT found :(" << endl;
} else {
cout << "Result: Found!" << endl;
cout << "--------------" << endl;
// you can use person in whatever way you like!
cout << "** Found person name: " << person_pointer->getName() <<endl;
cout << "** Found person age: " << person_pointer->getAge() <<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment