Loop through linked list based on structure - C++ Data Type

C++ examples for Data Type:struct

Description

Loop through linked list based on structure

Demo Code

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Employee// w  ww.j  a  va 2  s  . c o m
{
   string name;
   string phoneNo;
   Employee *nextaddr;
};
void display(Employee *);  // function prototype
int main()
{
   Employee t1 = {"A","(555) 898-2392"};
   Employee t2 = {"D","(555) 882-8104"};
   Employee t3 = {"L","(555) 818-4581"};
   Employee *first;    // create a pointer to a structure
   first = &t1;        // store t1's address in first
   t1.nextaddr = &t2;  // store t2's address in t1.nextaddr
   t2.nextaddr = &t3;  // store t3's address in t2.nextaddr
   t3.nextaddr = NULL; // store the NULL address in t3.nextaddr
   display(first);     // send the address of the first structure
   return 0;
}
void display(Employee *contents)  // contents is a pointer to a
{                               // structure of type Employee
while (contents != NULL)        // display until end of linked list
{
   cout << endl << setiosflags(ios::left)<< setw(30) << contents->name << setw(20) << contents->phoneNo ;
   contents = contents->nextaddr;    // get next address
}
cout << endl;
return;
}

Result


Related Tutorials