Professional Documents
Culture Documents
1
(->) This is a dereference operator that is used
exclusively with pointers to objects with members. This
operator serves to access a member of an object to
which we have a reference.
Both will work
pointer->member
(*pointer).member
NO *P = some thing;
struct date {
int month, day, year;
};
int main () {
date *pmd = new(date);
pmd->month = 3;
pmd->day = 15;
pmd->year = 2010;
cout << pmd->month << pmd->day<< pmd-
>year<<endl;
return 0;
}
NODE
A node is user defined structure. A node has
at least two fields data field and link field.
Here the following figure shows a node.
NODE
• Singly-Linked Lists
• Doubly-Linked Lists or Two way Linked List
• Circularly-Linked Lists
•Circularly-Doubly Linked Lists
Single Linked List
Start
Node 2 Node 3 Node 4
Name: Fred Name: Sue Name: Joe Name: Zoe
Age: 34 Age: 27 Age: 48 Age: 30
Height: 1.7 Height: 1.2 Height: 1.4 Height: 1.3
NULL
struct node {
char name[20]; // Name of up to 20 letters
int age; // D.O.B. would be better
float height; // In metres
node *next; // Pointer to next node
};
node *list = NULL;
Adding a node to the list
Note:
When a node is deleted, the space that it
took up should be reclaimed.
Otherwise memory leaks
Deleting the first node in the linked list
temp = start_ptr;
// Make the temporary pointer // identical to the start pointer
node->next->next->data
Navigating (Traversing ) through the list
node *current;
current = start_ptr;
current = current->next;
if (current->next == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->next;
Write a program to maintain a phone book. The phone book is to be
maintained as a linked list.
Your program must accept the following commands. Note that the
pointy brackets, < and > indicate user input and are not typed in the
input.
add <name> # <phone-number>
<name> can be any string not containing the pound ("#")
character. <phone-number> can be any integer. Create an entry
with the corresponding values and add it to your address book.
find <string>
If there is an entry with a name field that contains the given
string, print the entire entry. If not, print "not found".
del <name>
Delete the entry of the phone book corresponding to the name.
For this form, the name must match exactly. If no such name
exists, print a message indicating that there is no one by that
name.
print
Double-Linked List
NODE contains two pointers LPTR left pointer and the RPTR
right pointer. LPTR points to the previous node and RPTR
points to the next node. Please note that whenever there is no
node to point then the link is pointed to NULL.
void print_ave() {
cout << "Name: " << name << endl;
cout << "Average: " << ave << endl;
}
};
30
Using the member function
doubleStudentRecord stu;
stu.print_ave();
31
Classes and Objects
Any class = variables + functions