Code About Us Tutorial


  Templates
    Template Functions
    Multiple Template Parameters
    Template Specialization
    Class Templates
    Linked List
    Linked List Template
    Binary Tree


   
 

Home > Templates > Linked List

November 28, 2009 9:22 am

   

Linked List

Here is a simple linked list that I have already made for the purpose of demonstration. Copy it and paste it into your compiler and then try converting it into a template class. It is not as hard as you think, but if you have a problem with it post it on the discussion board (forum).

 

 
//      FILE:           LLIST2.CPP
//      PURPOSE:        Demonstrate a linked list implementation of a List
//=====================================
#ifndef BOOK_H
#define BOOK_H

#include <iostream.h>

struct ItemType { // Book friend class CList; friend ostream & operator<< (ostream & os, ItemType item);

char title[30]; char author[30]; long catalog; int year; };

ostream & operator<< (ostream & os, ItemType item) { os << "Title: " << item.title << endl; os << "Author: " << item.author << endl; os << "Catalog No: " << item.catalog << endl; os << "Year: " << item.year << endl; os << endl; return os; } #endif

//===================================== //LLIST.H

#ifndef LLIST_H #define LLIST_H

//#include <iostream.h> #include <conio.h> #include <stdlib.h> #include <string.h>

#define BOOL int #define TRUE 1 #define FALSE 0

const ItemType TEMPITEM = {"", "", 0L, 0};

class CList { friend ostream & operator<< (ostream & os, CList &list); private: // a node is just a data type that has a pointer to itself // many programming languages do not allow such thing struct Node { ItemType _item; Node *_next; }; // the head is the start of the list Node *_head;

// a temp node for outputting etc.. Node *pTemp;

// number of items long _nItems; public: CList(const ItemType& theItem = TEMPITEM ); CList(const CList& rList); BOOL isEmpty(); BOOL isFull(); int nItems(); CList& operator=(const CList& rList); ItemType& operator []( long index ); BOOL append(ItemType item); void traverse(void (*pfunc)(ItemType item)); };

#endif //================================================== //LIST.CPP // Copy Constructor CList::CList(const ItemType& theItem) { // create the head and insert theItem _head = new Node;

// always check to make sure new didnt fail. if( _head ) { _head->_item = theItem; _head->_next = 0; }

_nItems = 0; }

BOOL CList::isEmpty() { return (_head == 0); }

BOOL CList::isFull() { Node *pList; // create a temp list and if it fails // that means there's not enough room for anymore if ((pList = new Node) == 0) return TRUE; else { delete pList; return FALSE; } }

int CList::nItems() { //int count = 0; //Node *pList = _head; //while (pList != 0) { // ++count; // pList = pList -> _next; //}

//return count;

// instead of counting each time, just return the _nItems return _nItems; }

ItemType& CList::operator []( long index ) { long count;

//assert(_head);

// case 1: the first element if( _head ) { if( index <= 0 ) return _head->_item; } // start at the begining count = 0; pTemp = _head;

while( pTemp != 0 && count < index ) { ++count; pTemp = pTemp->_next; // walk forwards }

return pTemp->_item; }

BOOL CList::append(ItemType item) { Node *pNew; pTemp = _head; if (isFull()) return FALSE; pNew = new Node; //allocate new node if (pNew == 0) return FALSE; _nItems++; pNew->_item = item; //insert data in new node pNew->_next = 0; //make new node the end node if(isEmpty()) // Handle special case of first node _head = pNew; //it is a new list, so make new node head of list else { while (pTemp->_next) // find the end of the list (where _next=0) pTemp = pTemp->_next;

pTemp->_next = pNew; // and add the new node at the end } return TRUE; }

void CList::traverse(void (*pfunc)(ItemType item)) { pTemp = _head;

while (pTemp != 0) { pfunc(pTemp->_item); //apply function to data pTemp = pTemp->_next; //walk to next node } }

ostream & operator<< (ostream & os, CList &list) { list.pTemp = list._head; os << "Head: " << list.pTemp->_item << endl;

while (list.pTemp != 0) { os << list.pTemp->_item << endl; list.pTemp = list.pTemp->_next; //walk to next node } return os; }

void display(ItemType item);

int main() { CList bookList; ItemType book; system("cls"); strcpy(book.title, "Jerry Book"); strcpy(book.author, "Jerry"); book.catalog = 12345; book.year = 1918; cout << book; getch();

bookList.append(book);

strcpy(book.title, "Marie Book"); strcpy(book.author, "Marie"); book.catalog = 23456; book.year = 1993; bookList.append(book);

strcpy(book.title, "C++, How To Program"); strcpy(book.author, "Deitel"); book.catalog = 9999; book.year = 1999; bookList.append(book); bookList.traverse(display); cout << bookList; getch(); return EXIT_SUCCESS; }

void display(ItemType item) { cout << "[Displaying]----------------------+" << endl; cout << "Title: " << item.title << endl; cout << "Author: " << item.author << endl; cout << "Catalog No: " << item.catalog << endl; cout << "Year : " << item.year << endl; cout << endl << endl; getch(); }

Please Rate this Code:


    Comments for: Linked List
  There are NO user contributed comments for Linked List.



Add Comment:
Name:
Email:


 «