|
|
|
Linked List Examples - Using a Pointer in a Struct
1. Create a Linked List
% cat struct2.cpp
// linked list using structs
#include <iostream>
#include <iomanip>
using namespace std;
#include<string>
struct Music_rec
{
string comp;
string title;
int id;
Music_rec *nextaddr;
};
void display(Music_rec *); // function
prototype
int main()
{
Music_rec t1 = {"Bach","Air on a G String", 389};
Music_rec t2 = {"Mozart", "The Magic Flute", 786};
Music_rec t3 = {"Sibelius", "Finlandia", 985};
Music_rec *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(Music_rec *contents) // contents is a pointer to a structure
{
// of type Music_rec
while (contents != NULL) // display until end of linked
list
{
cout << '\n' << setiosflags(ios::left)
<< setw(12) <<
contents->comp
<< setw(30) <<
contents->title
<< setw(6) <<
contents->id;
contents = contents->nextaddr;
// get next address
}
cout << endl;
return;
}
% g++ struct2.cpp
% a.out
Bach Air on a G
String
389
Mozart The Magic
Flute
786
Sibelius
Finlandia
985
2. Insert and Delete Operations
% cat struct3.cpp
// linked list using structs
#include <iostream>
#include <iomanip>
using namespace std;
struct Music_rec
{
string comp;
string title;
int id;
Music_rec *nextaddr;
};
void display(Music_rec *); // function
prototype
int main()
{
Music_rec t1 = {"Bach","Air on a G String", 389};
Music_rec t2 = {"Mozart", "The Magic Flute", 786};
Music_rec t3 = {"Sibelius", "Finlandia", 985};
Music_rec *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
// insert a new record
cout << "\nInsert a record\n";
Music_rec t4 = { "Stravinsky", "The Rite of Spring", 123};
t1.nextaddr = &t4;
t4.nextaddr = &t2;
display(first);
// Delete a record
cout << "\nDelete a record\n";
t4.nextaddr = &t3;
display(first);
return 0;
}
void display(Music_rec *contents) // contents is a pointer to a structure
{
// of type Music_rec
while (contents != NULL) // display until end of linked
list
{
cout << '\n' << setiosflags(ios::left)
<< setw(12) <<
contents->comp
<< setw(30) <<
contents->title
<< setw(6) <<
contents->id;
contents = contents->nextaddr;
// get next address
}
cout << endl;
return;
}
% g++ struct3.cpp
% a.out
Bach Air on a G
String
389
Mozart The Magic
Flute
786
Sibelius
Finlandia
985
Insert a record
Bach Air on a G
String
389
Stravinsky The Rite of Spring
123
Mozart The Magic
Flute
786
Sibelius
Finlandia
985
Delete a record
Bach Air on a G
String
389
Stravinsky The Rite of Spring
123
Sibelius
Finlandia
985
|
|