C++: Data Structures: Lists - Example 1a
This is an example of a singly linked linear list. It is in three files,
list_node.h, main.cpp, and list_node.cpp. This illustrates the general idea
of linked lists, but it should be written in an object-oriented style,
which will be done in a later example.
//--- File list_node.h - Linked List example -- Fred Swartz 2001-12-13
#ifndef LIST_NOTE_H
#define LIST_NODE_H
struct list_node {
list_node *next;
int data;
};
//=============================================== prototypes
void printlist(list_node* start);
list_node* readlistLIFO(istream& input);
list_node* readlist(istream& input);
void clear(list_node *somelist);
#endif
This main program simply reads, writes, and deletes a list.
//--- file main.cpp - Linked List example -- Fred Swartz 2001-12-13
#include <iostream>
using namespace std;
#include "list_node.h"
//=============================================== main
void main() {
list_node *list1;
list1 = readlist(cin); // read list elements
printlist(list1); // print list elements
clear(list1); // delete list elements
}//end main
Also see
list_node.cpp defines some list functions.